简体   繁体   中英

How do I use the scrollbar inside of netbeans editor, Java Swing

I have a long form and I need to scroll it to select a control that is at bottom. How do I do that?

Creating a new Form with Scroll Bars using NetBeans GUI Builder:

  • In the NetBeans Projects panel select the Package within the Project you want to create a JFrame form in;
  • Right-Click on this package name and from the pop-up menus select New JFrame Form . A new JFrame form will be displayed within the GUI Builder of NetBeans;
  • Size this form to what you want and apply the properties you want like Title, DefaultCloseOperation, AlwaysOnTop, Generate Center, etc) in the Properties Pane of the IDE;
  • In the Pallet Pane of the IDE select Scroll Pane (JScrollPane) from the Swing Containers section and drag & drop it into the new JFrame window. Size the Scroll Pane component to be the full size of the JFrame Form;
  • Again, in the Pallet Pane of the IDE select Panel (JPanel) from the Swing Containers section and drag & drop it into the new Scroll Pane component. The JPanel should automatically size up to the size of the Scroll Pane;
  • Now drag & drop all the Swing components you want on your form into this JPanel . Enlarge the JFrame form if you have to to get all the components in then resize your form to what you want when done;
  • You now have a JFrame window with a Scroll Pane.

Creating a new JFrame form with Scroll Bars and displaying an existing JFrame form within that Scroll Pane using NetBeans GUI Builder and a little bit of code:

  • In the NetBeans Projects panel select the Package within the Project you want to create a JFrame form in;

  • Right-Click on this package name and from the pop-up menus select New JFrame Form . A new JFrame form will be displayed within the GUI Builder of NetBeans;

  • Size this form to what you want and apply the properties you want like Title, DefaultCloseOperation, AlwaysOnTop, Generate Center, etc) in the Properties Pane of the IDE;

  • In the Pallet Pane of the IDE select Scroll Pane (JScrollPane) from the Swing Containers section and drag & drop it into the new JFrame window. Size the Scroll Pane component to be the full size of the JFrame Form. Keep the auto-assigned variable name which should be jScrollPane1 ;

  • Switch to source code by selecting the Source tab in the IDE and navigate to the class constructor;

  • Directly after the initComponents(); method call, make a call to the new private method named: moreInitialization() . This new method would look something like this:

     private void moreInitialization() { /* Instantiate the form you want to display within the new form with scroll bars, for example, if the Form class you want to view within Scroll-Bars is named 'MainForm' then... */ MainForm mf = new MainForm(); jScrollPane1.setViewportView(mf.getContentPane()); setSize(mf.getPreferredSize().width + 20, mf.getPreferredSize().height + 20); setLocationRelativeTo(null); }
  • Display the new Scroll-Bar Form from the main() method with:

     java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new jFrame1.setVisible(true); } });
  • Resize the form to see and use the Scroll Bars.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM