java - add jPanels by selecting jCheckBoxes -


as can see layout want add (or show) jpanels selecting checkboxes. panelwindow enclosed in jscrollpane.

 + checkbox1  |            jpanel1  | -------------------------------------  + checkbox2  |            jpanel2  | -------------------------------------  + checkbox3  |            jpanel3  | -------------------------------------       ^                       ^       |                       |  checkboxpanel            panelwindow 

in case when selected checkbox2 , checkbox3, panelwindow shows jpanel2 , jpanel3, position of jpanel1 replaced jpanel2 , position of jpanel2 jpanel3.

 + checkbox2  |            jpanel2  | -------------------------------------  + checkbox3  |            jpanel3  | -------------------------------------        ^                       ^       |                       |  checkboxpanel            panelwindow 

right don't have idea how achieve goal. tried use cardlayout, in case panelwindow switches jpanels.

the goal want achieve selecting several checkboxes show selected jpanels in panelwindow. , if checkboxes doesn't selected, in case panelwindow empty.

thank in advance.

as suggested @stanislavl, created map between jcheckboxes , jpanels itemlistener showing , hiding panels.

public class panelshower extends jframe {      map<jcheckbox, jpanel> boxpanelmap = new hashmap<>();     final int size = 5;      public panelshower() {          jpanel boxespanel = new jpanel();         boxespanel.setlayout(new boxlayout(boxespanel, boxlayout.page_axis));          jpanel panelspanel = new jpanel();         panelspanel.setlayout(new boxlayout(panelspanel, boxlayout.page_axis));          (int = 0; < size; i++) {             jcheckbox checkbox = new jcheckbox("box " + i);             checkbox.additemlistener(new selectionlistener());             boxespanel.add(checkbox);              jpanel panel = new jpanel();             panel.setpreferredsize(new dimension(200, 75));             panel.setbackground(new color( (int) (math.random() * (math.pow(2, 24) - 1) ) ));             panel.add(new jlabel("panel " + i));             panel.setvisible(false);             panelspanel.add(panel);              boxpanelmap.put(checkbox, panel);         }          getcontentpane().add(boxespanel, borderlayout.line_start);         getcontentpane().add(panelspanel, borderlayout.center);          setdefaultcloseoperation(exit_on_close);         pack();         setlocationrelativeto(null);         setvisible(true);     }      private class selectionlistener implements itemlistener {          @override         public void itemstatechanged(itemevent e) {              (boxpanelmap.get(e.getsource())).setvisible(e.getstatechange() == itemevent.selected);             pack();         }     }      public static void main(string args[]) {          new panelshower();     } } 

notes:

  • the pack() call inside itemlistener necessary respecting preferred size set panels.
  • you might want have itemlistener more "type safe" passing jcheckbox in construction:

    private class selectionlistener implements itemlistener {      jcheckbox checkbox;      selectionlistener(jcheckbox checkbox) {          this.checkbox = checkbox;     }      @override     public void itemstatechanged(itemevent e) {          (boxpanelmap.get(checkbox)).setvisible(e.getstatechange() == itemevent.selected);         pack();     } } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -