Sorry, it does not work ...
Who can make this work ???
Thanks for reading my problem.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GridBagWindow extends JFrame {
boolean inAnApplet = true;
final boolean shouldFill = true;
final boolean shouldWeightX = true;
JTable table;
Container contentPane;
GridBagLayout gridbag;
GridBagConstraints c;
public GridBagWindow() {
// new Data
JButton NewDataButton = new JButton("This button should change the layout of the Table");
NewDataButton.setMnemonic(KeyEvent.VK_I);
NewDataButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//************************//
//THIS DOES NOT WORK !!!!
// WHO CAN FIX THIS ?????
//************************//
contentPane.remove(table);
table = new JTable(4, 4);
c.gridx = 0;
c.gridy = 1;
gridbag.setConstraints(table, c);
contentPane.add(table);
table.repaint();
}
});
table = new JTable(5, 5);
table.setRowSelectionAllowed(true);
table.setPreferredScrollableViewportSize(new Dimension(800, 200));
contentPane = getContentPane();
gridbag = new GridBagLayout();
c = new GridBagConstraints();
contentPane.setLayout(gridbag);
if (shouldFill) {
c.fill = GridBagConstraints.HORIZONTAL;
}
if (shouldWeightX) {
c.weightx = 0.5;
}
c.gridx = 0;
c.gridy = 0;
gridbag.setConstraints(NewDataButton, c);
contentPane.add(NewDataButton);
c.gridx = 0;
c.gridy = 1;
gridbag.setConstraints(table, c);
contentPane.add(table);
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this window.
gridbag.setConstraints(scrollPane, c);
getContentPane().add(scrollPane);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (inAnApplet) {
dispose();
} else {
System.exit(0);
}
}
});
}
public static void main(String args[]) {
GridBagWindow window = new GridBagWindow();
window.inAnApplet = false;
window.setTitle("Change the Table layout ...");
window.pack();
window.setVisible(true);
}
}