java - How to scroll a JScrollPane containing JPanels to top when starting program? -
i have problem jscrollpane can't start @ top when program launched. hoping wizard in here might have answers :p
here's working code example:
public class jscrollpaneexample extends jpanel{ public jscrollpaneexample(int rowcount){ setlayout(new borderlayout()); jscrollpane scrollpane = makescrollpane(rowcount); add(scrollpane, borderlayout.page_start); setpreferredsize(new dimension(300,300)); } private jscrollpane makescrollpane(int rowcount) { jscrollpane scrollpane = new jscrollpane(); jpanel pane = new jpanel(); pane.setlayout(new gridlayout(rowcount, 0)); for(int = 0; < rowcount; i++){ if(i != 4) pane.add(maketestfield("test " + (i+1), "test description: " + (i+1), i)); else { pane.add(maketestfield("this test", "this test see if field line break. " + "this test see if field line break. " + "something else entirely different...", i)); } } scrollpane.getviewport().add(pane); scrollpane.setpreferredsize(new dimension(200, 400)); return scrollpane; } protected static jpanel maketestfield(string header, string description, int rowcount){ jpanel testfield = new jpanel(); testfield.setlayout(new borderlayout()); testfield.setborder(borderfactory.createemptyborder(5,5,5,5)); jlabel label = new jlabel(header); font font = new font("serif", font.bold, 18); label.setfont(font); jbutton btn = new jbutton("start"); jtextarea textarea = new jtextarea(description); textarea.seteditable(false); textarea.setbackground(label.getbackground()); textarea.setlinewrap(true); textarea.setwrapstyleword(true); textarea.setborder(borderfactory.createemptyborder(0,0,0,5)); testfield.add(label, borderlayout.page_start); testfield.add(textarea, borderlayout.center); testfield.add(btn, borderlayout.line_end); return testfield; } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { // turn off metal's use of bold fonts uimanager.put("swing.boldmetal", boolean.false); jframe frame = new jframe(); jpanel scrollpane = new jscrollpaneexample(8); frame.add(scrollpane); frame.setvisible(true); frame.setlocationrelativeto(null); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setpreferredsize(new dimension(500,500)); frame.pack(); } }); } }
i have tried quit lot of different methods found while browsing interwebz, such setting viewport of scrollpanel , lot of other stuff, none of success.
to need set value of vertical scroll bar. here code.
public class jscrollpaneexample extends jpanel{ public jscrollpaneexample(int rowcount){ setlayout(new borderlayout()); final jscrollpane scrollpane = makescrollpane(rowcount); swingutilities.invokelater(new runnable() { @override public void run() { scrollpane.getverticalscrollbar().setvalue(0); } }); add(scrollpane, borderlayout.page_start); setpreferredsize(new dimension(300,300)); } private jscrollpane makescrollpane(int rowcount) { final jscrollpane scrollpane = new jscrollpane(); final jpanel pane = new jpanel(); pane.setlayout(new gridlayout(rowcount, 0)); for(int = 0; < rowcount; i++){ if(i != 4) pane.add(maketestfield("test " + (i+1), "test description: " + (i+1), i)); else { pane.add(maketestfield("this test", "this test see if field line break. " + "this test see if field line break. " + "something else entirely different...", i)); } } scrollpane.getviewport().add(pane); scrollpane.setpreferredsize(new dimension(200, 400)); return scrollpane; } protected static jpanel maketestfield(string header, string description, int rowcount){ final jpanel testfield = new jpanel(); testfield.setlayout(new borderlayout()); testfield.setborder(borderfactory.createemptyborder(5,5,5,5)); final jlabel label = new jlabel(header); final font font = new font("serif", font.bold, 18); label.setfont(font); final jbutton btn = new jbutton("start"); final jtextarea textarea = new jtextarea(description); textarea.seteditable(false); textarea.setbackground(label.getbackground()); textarea.setlinewrap(true); textarea.setwrapstyleword(true); textarea.setborder(borderfactory.createemptyborder(0,0,0,5)); testfield.add(label, borderlayout.page_start); testfield.add(textarea, borderlayout.center); testfield.add(btn, borderlayout.line_end); return testfield; } public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { // turn off metal's use of bold fonts uimanager.put("swing.boldmetal", boolean.false); final jframe frame = new jframe(); final jpanel scrollpane = new jscrollpaneexample(8); frame.add(scrollpane); frame.setvisible(true); frame.setlocationrelativeto(null); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setpreferredsize(new dimension(500,500)); frame.pack(); } }); } }
Comments
Post a Comment