java - How to make a JPanel use the scrollbars to fit in large J components? -


i have problem jpanel (contentpane) re-sizes (testlabel) gui components small whenever big fit in panel. have added scrollbars jpanel components still re-size instead of using scrollbars. here class use jpanel scrollbars.

package marspackage;  import java.awt.*; import javax.swing.*;  public class dashtab{      private jlabel testlabel; //test label     private jlabel testlabel2;     private jlabel testlabel3;     private jlabel testlabel4;     private jpanel dashpanel; //panel holds jtabbedpane tab     private jpanel contentpane; // panel holds gui components     private jscrollpane scrollpane; // scrollpane used on contentpane      /*      * constructor      * of gui components should added      * contentpane using gridbaglayout.      */      public dashtab(){     //creating dashpanel holds     dashpanel = new jpanel();     dashpanel.setbackground(color.white);      //creating contentpane holds gui components ,     //uses vertical/horizontal sidebards needed     contentpane = new jpanel();     contentpane.setbackground(color.white);      //giving contentpane gridbaglayout     contentpane.setlayout(new gridbaglayout());     gridbagconstraints g = new gridbagconstraints();     contentpane.setpreferredsize(new dimension(857, 725));      //adding scrollpane content pane , adding 2 dashpanel     scrollpane = new jscrollpane(contentpane);     scrollpane.sethorizontalscrollbarpolicy(jscrollpane.horizontal_scrollbar_always);     scrollpane.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_always);     scrollpane.setbackground(color.white);     dashpanel.add(scrollpane);      /*      * may begin adding gui components point forward.      * remember use gridbaglayout gridbagconstraints using      * g variable.      */      testlabel = new jlabel("testing here 1");     testlabel.setpreferredsize(new dimension(500, 500));     testlabel.setborder(borderfactory.creatematteborder(2, 2, 2, 2, color.gray));     g.gridx = 0;     g.gridy = 0;     contentpane.add(testlabel, g);      testlabel2 = new jlabel("testing here 2");     testlabel2.setpreferredsize(new dimension(250, 200));     testlabel2.setborder(borderfactory.creatematteborder(2, 2, 2, 2, color.gray));     g.gridx = 1;     g.gridy = 0;     contentpane.add(testlabel2, g);      testlabel3 = new jlabel("testing here 3");     testlabel3.setpreferredsize(new dimension(200, 200));      testlabel3.setborder(borderfactory.creatematteborder(2, 2, 2, 2, color.gray));     g.gridx = 1;     g.gridy = 1;     contentpane.add(testlabel3, g);      testlabel4 = new jlabel("testing here 4");     testlabel4.setpreferredsize(new dimension(200, 200));      testlabel4.setborder(borderfactory.creatematteborder(2, 2, 2, 2, color.gray));     g.gridx = 2;     g.gridy = 2;     contentpane.add(testlabel4, g);     }      /**      * getdashtab method returns dashtab object.      * @return dashtab panel object.      */      public jpanel getdashtab(){             return dashpanel;     } } 

this how above code looks like: http://i.imgur.com/lbstfrt.jpg

whenever remove contentpane.setpreferredsize(new dimension(857, 725)); panel stretches out , complete ignores scrollbars making like:

http://i.imgur.com/smusbtm.jpg

contentpane.setpreferredsize(new dimension(857, 725));

don't use setpreferredsize() method set size. job of layout manager, in case, gridbaglayout determine size of panel.

scrollbars appear automatically when preferred size of panel greater size of scrollpane.

gui components small whenever big fit in panel.

the gridbaglayout try respect preferred size of component. if preferred size greater size of panel, components "minimum size" used. in case of jlabel, minimum size space needed entirely display text.

once again, don't try use setpreferedsize() method.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -