java - How to scroll down a vertical scroll bar on a JScrollPane? -
i use netbeans java/swing , have problem scroll pane. first of have 2 jpanel
components inside scroll panel. when init components make not visible second panel. wanted when click on button, second panel set visible (it works) , vertical scroll bar moves down show second panel.
i use code when click on button:
public void eventoadd() { mivista.getjpanelsegundo().setvisible(true); mivista.getjscrollpane1().getverticalscrollbar().setvalue(280); }
the vertical scroll bar never goes bottom @ first click, @ second attempt seem vertical scroll bar goes bottom. problem?
ps: see lot of questions, there aren't answers specific question.
/* * change license header, choose license headers in project properties. * change template file, choose tools | templates * , open template in editor. */ /** * * @author rober */ public class view extends javax.swing.jframe { /** * creates new form view */ public view() { initcomponents(); jpanelsecond.setvisible(false); } /** * method called within constructor initialize form. * warning: not modify code. content of method * regenerated form editor. */ @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() { jscrollpanefirst = new javax.swing.jscrollpane(); jpanelmain = new javax.swing.jpanel(); jpanelfirst = new javax.swing.jpanel(); jpanelsecond = new javax.swing.jpanel(); jbutton1 = new javax.swing.jbutton(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); jscrollpanefirst.sethorizontalscrollbarpolicy(javax.swing.scrollpaneconstants.horizontal_scrollbar_never); jpanelfirst.setborder(javax.swing.borderfactory.createtitledborder("first")); javax.swing.grouplayout jpanelfirstlayout = new javax.swing.grouplayout(jpanelfirst); jpanelfirst.setlayout(jpanelfirstlayout); jpanelfirstlayout.sethorizontalgroup( jpanelfirstlayout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgap(0, 0, short.max_value) ); jpanelfirstlayout.setverticalgroup( jpanelfirstlayout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgap(0, 61, short.max_value) ); jpanelsecond.setborder(javax.swing.borderfactory.createtitledborder("second")); javax.swing.grouplayout jpanelsecondlayout = new javax.swing.grouplayout(jpanelsecond); jpanelsecond.setlayout(jpanelsecondlayout); jpanelsecondlayout.sethorizontalgroup( jpanelsecondlayout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgap(0, 294, short.max_value) ); jpanelsecondlayout.setverticalgroup( jpanelsecondlayout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgap(0, 63, short.max_value) ); javax.swing.grouplayout jpanelmainlayout = new javax.swing.grouplayout(jpanelmain); jpanelmain.setlayout(jpanelmainlayout); jpanelmainlayout.sethorizontalgroup( jpanelmainlayout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(jpanelfirst, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value) .addcomponent(jpanelsecond, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value) ); jpanelmainlayout.setverticalgroup( jpanelmainlayout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(jpanelmainlayout.createsequentialgroup() .addcomponent(jpanelfirst, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related, javax.swing.grouplayout.default_size, short.max_value) .addcomponent(jpanelsecond, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size) .addcontainergap()) ); jscrollpanefirst.setviewportview(jpanelmain); jbutton1.settext("add"); jbutton1.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { jbutton1actionperformed(evt); } }); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addcomponent(jscrollpanefirst, javax.swing.grouplayout.preferred_size, 280, javax.swing.grouplayout.preferred_size) .addgroup(layout.createsequentialgroup() .addgap(98, 98, 98) .addcomponent(jbutton1, javax.swing.grouplayout.preferred_size, 58, javax.swing.grouplayout.preferred_size))) .addcontainergap(javax.swing.grouplayout.default_size, short.max_value)) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addcomponent(jscrollpanefirst, javax.swing.grouplayout.preferred_size, 113, javax.swing.grouplayout.preferred_size) .addpreferredgap(javax.swing.layoutstyle.componentplacement.related, javax.swing.grouplayout.default_size, short.max_value) .addcomponent(jbutton1)) ); pack(); }// </editor-fold> private void jbutton1actionperformed(java.awt.event.actionevent evt) { jpanelsecond.setvisible(true); jscrollpanefirst.getverticalscrollbar().setvalue(280); } /** * @param args command line arguments */ public static void main(string args[]) { /* set nimbus , feel */ //<editor-fold defaultstate="collapsed" desc=" , feel setting code (optional) "> /* if nimbus (introduced in java se 6) not available, stay default , feel. * details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) { if ("nimbus".equals(info.getname())) { javax.swing.uimanager.setlookandfeel(info.getclassname()); break; } } } catch (classnotfoundexception ex) { java.util.logging.logger.getlogger(view.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(view.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(view.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(view.class.getname()).log(java.util.logging.level.severe, null, ex); } //</editor-fold> /* create , display form */ java.awt.eventqueue.invokelater(new runnable() { public void run() { new view().setvisible(true); } }); } // variables declaration - not modify private javax.swing.jbutton jbutton1; private javax.swing.jpanel jpanelfirst; private javax.swing.jpanel jpanelmain; private javax.swing.jpanel jpanelsecond; private javax.swing.jscrollpane jscrollpanefirst; // end of variables declaration }
as personal preference, find scrollrecttovisible
among reliable means achieve this, need know how use it
private void jbutton1actionperformed(java.awt.event.actionevent evt) { jpanelsecond.setvisible(true); rectangle viewbounds = jscrollpanefirst.getviewportborderbounds(); dimension size = jpanelmain.getpreferredsize(); jpanelmain.scrollrecttovisible(new rectangle(0, size.height, viewbounds.width, viewbounds.height)); //jscrollpanefirst.getverticalscrollbar().setvalue(280); }
Comments
Post a Comment