How to get vertical scroll to JPanel?

I wrote code in java using swing, so I will have JscrollPane added to JPanel, and then I will add fixed size buttons to JPanel vertically.

JPanel panel=new JPanel(); panel.setBackground(Color.WHITE); int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; JScrollPane jsp=new JScrollPane(panel,v,h); jsp.setPreferredSize(new Dimension(600,600)); jsp.setBounds(150,670,850,200); frame.add(jsp); 

then I add buttons to it at runtime.

  for(i=0;i<n;i++) { button[i]=new JButton(); button[i].setBounds(20,y,120,120); button[i].setSize(120,120); button[i].setToolTipText(file[i].toString()); button[i].setIcon(Icon); panel.add(button[i]); y=y+140; } 

I want to add buttons one below the other ... (i.e. I want a vertical scrollbar)

i.e. Button1

  button2 ' ' 

but the above code gives me buttons in a line (i.e. I get a horizontal scrollbar) i.e. button button1 ...

Another problem is the size of the buttons. Using btn.setSize () does not affect the size at all ...

Can anybody help me?

+4
source share
7 answers

For the panel, you must use the appropriate Layoutmanager, for example GridLayout, Boxlayout or GridBagLayout.
It depends on what else you want to place in the panel.

GridLayout is easier to use IMO:

 JPanel panel = new JPanel(); panel.setLayout(new GridLayout(0, 1)); // any number of rows, 1 column ... panel.add(button[i]); 

BoxLayout is almost as easy:

 JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); ... panel.add(button[i]); 

GridBagLayout is more powerful, allowing more than one column, components spanning more than one cell ... require GridBagConstraints to add elements:

 JPanel panel = new JPanel(); panel.setLayout(new GridBagLayout()); GridBagConstraints constraints = new GridBagConstraints( 0, RELATIVE, // x = 0, y = below previous element 1, 1, // cell width = 1, cell height = 1 0.0, 0.0 // how to distribute space: weightx = 0.0, weighty = 0,0 GridBagConstraints.CENTER, // anchor GridBagConstraints.BOTH, // fill new Insets(0, 0, 0, 0), // cell insets 0, 0); // internal padding ... panel.add(button[i], constraints); 

Take a look at this tutorial: Aligning components inside a container (visual guide is a good starting point)

EDIT :
You can also lay out components manually, that is, specify the location and size of each component in the container. To do this, you must set the LayoutManager to null to remove the default manager.

 JPanel panel = new JPanel(); panel.setLayout(null); ... button[i].setLocation(x, y); button[i].setSize(width, heigth); // OR button[i].setBounds(x, y, width, height); panel.add(button[i]); 
+7
source

You need to define a LayoutManager for your JPanel , which is responsible for adding the Component located to it. The default LayoutManager is FlowLayout , which sets the Component from left to right. To place Component vertically, you must use BoxLayout or GridBagLayout .

+3
source

You must install the LayoutManager for JPanel or use Box (BoxLayout.Y_AXIS).

+1
source

For button size use preferredSize

For your layout task, you need to change the layout manager to the one that performs the vertical layout. To play around goals, you can use BoxLayout as follows:

 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 
+1
source

This is much easier if you allow the layout manager to do his job.

In Swing, a placement manager uses a placement manager to place components over another component (such as a panel).

It is used to avoid the need to calculate the coordinates of all components against each other whenever the container component is resized or a new component is added.

There are different layout managers, here you need BoxLayout .

Using this layout, you do not need to specify either the position of the button or its size. The location manager requests each component and uses this information to place them in the correct position and size.

For example, the next frame

alt text

This (modified version of your) code was created:

 import javax.swing.*; import java.awt.*; public class ScrollTest { private JPanel panel; private Icon[] icons = new Icon[3]; public void main() { panel =new JPanel(); // Use top to bottom layout in a column panel.setLayout( new BoxLayout( panel, BoxLayout.Y_AXIS )); panel.setBackground(Color.WHITE); int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; JScrollPane jsp=new JScrollPane(panel,v,h); jsp.setPreferredSize(new Dimension(600,600)); jsp.setBounds(150,670,850,200); JFrame frame = new JFrame(); frame.add(jsp); // my addition to load sample icons loadImages(); // simulate dynamic buttons addButtons(); frame.pack(); frame.setVisible( true ); } void loadImages() { icons[0] = new ImageIcon( "a.png" ); icons[1] = new ImageIcon( "b.png" ); icons[2] = new ImageIcon( "c.png" ); } void addButtons() { for( int i = 0 ; i < icons.length ; i++ ) { JButton button = new JButton(); Icon icon = icons[i]; button.setIcon( icon ); // Set the button size to be the same as the icon size // The preferred size is used by the layout manager // to know what the component "better" size is. button.setPreferredSize( new Dimension( icon.getIconWidth(), icon.getIconHeight() ) ); // This is IMPORTANT. The maximum size is used bythe layout manager // to know "how big" could this component be. button.setMaximumSize( button.getPreferredSize() ); panel.add( button ); } } public static void main( String ... args ) { new ScrollTest().main(); } 

}

Hope this helps.

+1
source

You can also get vertical scrolling for JPanel with SpringLayout . Perhaps if the size of the vertical panel will be determined by setting the SpringLayout.SOUTH constraint. This can be done as follows:

 JPanel panel = new JPanel(); SpringLayout panelLayout = new SpringLayout(); panel.setLayout(panelLayout); // Adding components to the panel here // ..... // That what defines panel exact size and makes its scrolling possible panelLayout.putConstraint(SpringLayout.SOUTH, panel, 0, SpringLayout.SOUTH, lastComponentOfThePanel); JScrollPane panelScrollPane = new JScrollPane(panel); 

where lastComponentOfThePanel is the component at the bottom of the panel.

Hope this helps someone. In my opinion, SpringLayout is a very powerful layout manager, and sometimes it is very difficult or almost impossible to replace it with a GridBagLayout .

0
source

What about?

 JScrollPane scrollPane = new JScrollPane(yourpanel); container.add(scrollPane); 
0
source

Source: https://habr.com/ru/post/1301677/


All Articles