简体   繁体   中英

Can i do this in java desktop application?

替代文字

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

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);
}

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM