java - How to show specific scrollpane in a CardLayout? -
i have list of jpanel
components. want open corresponding panel based on clicking button. while clicking button, pass panel name. in case panel have more components, adding each panel jscrollpane
, adding these scroll panes cardlayout
.
based on example, here code adding panel card layout:
final jpanel cards=new jpanel(new cardlayout()); cards.setpreferredsize(new dimension(600, 550)); (jpanel p:panels) { jscrollpane scrollpanel=new jscrollpane(p); scrollpanel.setname(p.getname()); scrollpanel.sethorizontalscrollbarpolicy(jscrollpane.horizontal_scrollbar_as_needed); scrollpanel.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_as_needed); cards.add(scrollpanel); } cardlayout cardlayout=(cardlayout) cards.getlayout(); cardlayout.show(cards, "name"); control.add(new jbutton(new abstractaction("\u22b2prev") { @override public void actionperformed(actionevent e) { cardlayout cl = (cardlayout) cards.getlayout(); cl.previous(cards); } })); control.add(new jbutton(new abstractaction("next\u22b3") { @override public void actionperformed(actionevent e) { cardlayout cl = (cardlayout) cards.getlayout(); cl.next(cards); } }));
but clicking button shows first panel in card layout.
how solve problem? can use scroll pane name instead of panel name in show method?
yes right.
a scroll pane may added directly card layout:
jpanel mypanel = new jpanel(); jscrollpane myscroll = new jscrollpane(mypanel); this.add(myscroll, "name"); cardlayout cl = (cardlayout) this.getlayout(); cl.show(this, "name");
Comments
Post a Comment