java - BorderLayout and JScrollPane problems -


i have jframe 3 areas:

  • a scrollpane list of objects
  • a panel labels , textfields
  • a scrollpane panel potentially having multiple labels

when click item on list, textfields on panel filled , labels on second scroll created. have 2 problems code:

  1. for reason scrollpane @ botom of screen not fill whole borderlayout's south area, half of it.
  2. the scrollpane not show when item on list selected.

here tried make example:

private void jmenuitem1actionperformed(java.awt.event.actionevent evt) {                                                jpanel geral = new jpanel();     jpanel lista = new jpanel();     jpanel dados = new jpanel();     jpanel panehist = new jpanel();     jpanel historico = new jpanel();     gridlayout gridlay = new gridlayout(0, 2, 5, 10);     geral.setlayout(gridlay);     dados.setlayout(gridlay);     historico.setlayout(new borderlayout());     lista.setlayout(new borderlayout());     panehist.setlayout(gridlay);      this.setlayout(new borderlayout());     this.add(geral);     geral.add(lista, borderlayout.west);     geral.add(dados, borderlayout.east);     geral.add(historico, borderlayout.south);      defaultlistmodel listmodel = new defaultlistmodel();     listmodel.addelement("just testing");      final jlist list = new jlist(listmodel);     list.setlayoutorientation(jlist.vertical);     list.setvisible(true);     list.setselectionmode(listselectionmodel.single_selection);      jscrollpane scroll = new jscrollpane(list);     scroll.setpreferredsize(new dimension(100, 500));      lista.add(scroll, borderlayout.center);      jtextfield jtf = new jtextfield();     dados.add(new jlabel("test:"));     dados.add(jtf);      list.addlistselectionlistener(new listselectionlistener() {         @override         public void valuechanged(listselectionevent lse) {             jtf.settext("clicked");             panehist.add(new jlabel("texttexttext"));             panehist.add(new jlabel("texttexttext"));         }}     );     jscrollpane scrollhist = new jscrollpane(panehist);     scrollhist.setpreferredsize(new dimension(500, 100));     historico.add(new jlabel("historico:"), borderlayout.north);     historico.add(scrollhist, borderlayout.east);     //list.setcellrenderer(new cellrenderer());      this.validate();     this.repaint(); }        

can't tell doing posted code.

some general comments:

  1. don't use setpreferredsize(). let each component determined preferred size. in case of jlist can use setvisiblerowcount(...) method jlist can calculate reasonable size.

  2. in listselectionlistener, when add/remove components visible gui need revalidate() , repaint() panel.


Comments