java - I need to make my JPanel resize dynamically as new components are being added to it -
i need let users add more text fields jframe once size of frame has exceeded original value scroll pane step in. since cannot add jscrollpane jframe in order enable scrolling decided put jpanel on jframe , pass jpanel object jscrollpane constructor. scrolling works fine until has reached borders of jpanel. thing size of jpanel stays , not stretching dynamically. happens buttons in code using space of jpanel being size of 300x300 want have jpanel stretch once these controls have used original space. please advise.
import java.awt.dimension; import java.awt.flowlayout; import java.awt.gridlayout; import java.awt.rectangle; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.scrollpaneconstants; public class skrol { public static void main(string[] args) { jframe f = new jframe(); f.setlayout(new flowlayout()); f.setdefaultcloseoperation(jframe.exit_on_close); jpanel p = new jpanel(); p.setpreferredsize(new dimension(400,400)); jscrollpane jsp = new jscrollpane(p); jsp.setpreferredsize(new dimension(300,300)); jsp.sethorizontalscrollbarpolicy(scrollpaneconstants.horizontal_scrollbar_as_needed); jsp.setverticalscrollbarpolicy(scrollpaneconstants.vertical_scrollbar_as_needed); for(int i=0;i<100;i++) { jbutton b = new jbutton("button "+i); p.add(b); } f.add(jsp); f.setsize(new dimension(600,600)); f.setlocation(300, 300); f.setvisible(true); } }
i changed layout in jpanel
gridlayout, size of handeld layoutmanager depending on components on panel.
jframe f = new jframe(); f.setlayout(new borderlayout()); f.setdefaultcloseoperation(jframe.exit_on_close); jpanel p = new jpanel(new gridlayout(0, 5)); jscrollpane jsp = new jscrollpane(p); jsp.setpreferredsize(new dimension(300,300)); jsp.sethorizontalscrollbarpolicy(scrollpaneconstants.horizontal_scrollbar_as_needed); jsp.setverticalscrollbarpolicy(scrollpaneconstants.vertical_scrollbar_as_needed); (int = 0; < 100; i++) { jbutton b = new jbutton("button " + i); p.add(b); } f.add(jsp, borderlayout.center); f.setlocation(300, 300); f.setvisible(true); f.pack();
Comments
Post a Comment