java - Toggling between JPanels (hide/show) -
i have 2 jpanels sit on top of 1 another. 'top' panel holds many widgets (jbuttons, jtextfields, etc.). 1 of buttons initiate action display number of images.
these images displayed on other jpanel. so, when button clicked, want hide control panel , display images panel. sounds pretty simple.
here code (i've omitted lot of stuff don't think relevant). in constructor, if switch panel visible when app launches, looks fine either way. when click button, should go dark gray control panel blue images panel. except happens dark gray control panel becomes empty white panel. ideas?
public gui() { jframe frame = new jframe(); ... jpanel imagespanel = new imagespanel(); imagespanel.setbackground(color.blue); imagespanel.setvisible(false); frame.getcontentpane().add(imagespanel, borderlayout.center); // make jpanel hold of buttons , text fields jpanel imagespanel = new imagespanel(); controlpanel.setbackground(color.dark_gray); controlpanel.setvisible(true); frame.getcontentpane().add(controlpanel, borderlayout.center); ... jbutton btndisplayimages = new jbutton("display images"); btndisplayimages.setpreferredsize(standardbuttonsize); btndisplayimages.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { imagespanel.setvisible(true); controlpanel.setvisible(false); frame.repaint(); frame.setvisible(true); } }); // button added control panel ... }
use cardlayout. (docs.oracle.com/javase/tutorial/uiswing/layout/card.html)
final string images_panel = "images panel"; final string control_panel = "control panel"; cardlayout cardlayout; jpanel cards; //where components controlled cardlayout initialized: //create "cards". jpanel card1 = new jpanel(); ... jpanel card2 = new jpanel(); ... //create panel contains "cards". cardlayout = new cardlayout(); cards = new jpanel(cardlayout); cards.add(card1, images_panel); cards.add(card2, control_panel); ... //show images panel cardlayout.show(cards,images_panel); ... //show control panel cardlayout.show(cards, control_panel);
Comments
Post a Comment