Not able to changes cards in Java Swing CardLayout -
i have class named addaccounts
class addaccounts extends jpanel { jpanel panelcont; //panel deck cardlayout cl; public addaccounts() { panelcont=new jpanel(); cl = new cardlayout(); panelcont.setlayout(cl);//set panel layout cardlayout setpreferredsize(new dimension(1013, 513));//set default size /* add panels main window or integrate panels*/ panelcont.add(new panel1(), "1"); panelcont.add(new panel2(),"2"); panelcont.add(new panel3(),"3"); cl.show(panelcont, "1"); add(panelcont); } public void gonext() { cl.show(panelcont, "2"); // cl.nxet(panelcont); system.out.println("method called"); //for debugging purpose } public void showfirstpanel(){ cl.show(panelcont, "1"); } }
and 2 external(separate files) class named panel1 , panel2(). , wanna change cards(from panel1 panel2) when button being pressed in panel1 that's why create above listed method called gonext(), problem i'm not able change cards. here how try call gonext()method in panel1
@override public void actionperformed(actionevent event) { if (event.getsource() == nextbtn) { setmaincategory((string)maincat.getselecteditem()); } } public void setmaincategory(string maincategory){ this.maincategory=maincategory; new addaccounts().gonext(); //system.out.println("from set method: "+this.maincategory); }
everything works fine, system.out.println("method called"); //for debugging purpose executed , printed on console window panels(card)are not changed. please help.. how make work.
@haraldk said absolutely correct. doesn't me, concept trying implement have jframe named mainpanel , in mainpanel have jsplitpanel jsplitpane rootpane = new jsplitpane(jsplitpane.vertical_split, userpanel, maincontentpane);
maincontentpane consists
jtabbedpane maincontentpane = new jtabbedpane(); maincontentpane.setpreferredsize(new dimension(900, 550)); maincontentpane.settabplacement(jtabbedpane.top); maincontentpane.addtab("your database", new userrecords()); maincontentpane.addtab("add accounts", new addaccounts());//here addaccounts jpanel maincontentpane.addtab("update existing data", new editrecords()); maincontentpane.setselectedindex(1);
i place buttons on panel1,panel2 rather placing buttons on parent frame addaccounts. here method gonext belong addaccounts , want access change panel(from 1 2) when button pressed in panel1. cannot call gonext() method directly,since method of addaccounts class not panel1. , new addaccounts().gonext();
doesn't directory structure: mypackage -userpanel(mainpanelframe,userpaneljpanel, maincontentpanetabbedpane) -addaccounts (gonext()) -panel1 (nextbtn) -panel2 (nextbtn)
the problem not cardlayout
code, rather in setmaincategory
method:
new addaccounts().gonext();
here create new instance of addaccounts
every time method invoked. panel flipped on instance (that why see debug output), component never added parent (like jframe
or jpanel
) , never shown. after method exits, resulting component thrown away.
instead, need add 1 single addaccounts
parent component, , in setmaincategory
method, invoke gonext()
on instance.
here's functional program demonstrates tried explain above:
import javax.swing.*; import java.awt.*; import java.awt.event.actionevent; public class cardlayouttest { public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { jframe frame = new jframe("cardlayouttest"); frame.setdefaultcloseoperation(windowconstants.exit_on_close); frame.getcontentpane().add(new addaccounts()); // create single instance here frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } static class addaccounts extends jpanel { jpanel panelcont; //panel deck cardlayout cl; public addaccounts() { panelcont = new jpanel(); cl = new cardlayout(); panelcont.setlayout(cl);//set panel layout cardlayout /* add panels main window or integrate panels*/ panelcont.add(new colorpanel(color.orange), "1"); panelcont.add(new colorpanel(color.gray), "2"); panelcont.add(new colorpanel(color.dark_gray), "3"); cl.show(panelcont, "1"); add(panelcont); add(new jbutton(new abstractaction("first") { @override public void actionperformed(actionevent e) { showfirstpanel(); } })); add(new jbutton(new abstractaction("next") { @override public void actionperformed(actionevent e) { // method similar setmaincategory gonext(); } })); } public void gonext() { // cl.show(panelcont, "2"); cl.next(panelcont); // allows iterating through panels system.out.println("method called"); //for debugging purpose } public void showfirstpanel() { cl.show(panelcont, "1"); } /// dummy class show page flipping works private class colorpanel extends jpanel { public colorpanel(color background) { setopaque(true); setbackground(background); } @override public dimension getpreferredsize() { return new dimension(300, 200); } } } }
Comments
Post a Comment