views:

41

answers:

2

alt text

I want to make a program java desktop . I have 2 table, "Table 1" and "Table 2". When i press "Button 1" in table box is showing "Table 1" and when i press "Button 2" "Table 2" is showing.

Anyone can tell me how to do that? Thx

+5  A: 

You can do that easily with a CardLayout.

Something like the following, when constructing your panel:

myPanel = new JPanel(new CardPanel());
myPanel.add(myPanelContainingTable1, CONSTANT_FOR_BUTTON1);
myPanel.add(myPanelContainingTable2, CONSTANT_FOR_BUTTON2);

in your actionPerformed method, handling the button actions:

CardLayout cl = (CardLayout) myPanel.getLayout();
if (event.getActionCommand().equals(actionCommandForButton1) {
    cl.show(myPanel, CONSTANT_FOR_BUTTON1);
} else if (event.getActionCommand().equals(actionCommandForButton2) {
    cl.show(myPanel, CONSTANT_FOR_BUTTON2);
}
akf
+1  A: 

All Java Swing components have a visible property. So the easiest way is to place both tables at the same location, but only have one visible at a time. Then in your button handlers, you do something like:

void button1_handler() {
  table1.setVisible(true);
  table2.setVisible(false);
}

void button2_handler() {
  table1.setVisible(false);
  table2.setVisible(true);
}
David Sykes
Or, as akf suggested, use a CardLayout
David Sykes