Fixed width, variable height in JPanel with stream

I have an annoying problem with Javas layout managers. I have the following situation: on panel A, there are two other panels B with absolute layout and C with FlowLayout. B is very tuned and has a fixed size set via setPreferredSize . C should have the same fixed width as B, but otherwise have a variable height, depending on how many components are added to the stream. As a result, A should have a fixed width and A.height + B.height as height - at least that is what I want.

However, I get that the width of Panel A is not fixed at all (even if I set its preferred size), and the content in Panel C does not automatically wrap, but displays in a long line. Of course, this also makes B wider than it should be.

What can I do to fix this? Is there any better layout, or do I need to emulate this all using an absolute layout?

 import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JPanel; public class Test extends JPanel { public Test () { this.setLayout( new BoxLayout( this, BoxLayout.Y_AXIS ) ); JPanel top = new JPanel( null ); top.setBackground( Color.GREEN ); top.setPreferredSize( new Dimension( 200, 20 ) ); JPanel flowPanel = new JPanel( new FlowLayout( FlowLayout.LEFT, 2, 2 ) ); this.add( top ); this.add( flowPanel ); flowPanel.add( new JButton( "x" ) ); flowPanel.add( new JButton( "x" ) ); flowPanel.add( new JButton( "x" ) ); flowPanel.add( new JButton( "x" ) ); flowPanel.add( new JButton( "x" ) ); flowPanel.add( new JButton( "x" ) ); flowPanel.add( new JButton( "x" ) ); flowPanel.add( new JButton( "x" ) ); flowPanel.add( new JButton( "x" ) ); flowPanel.add( new JButton( "x" ) ); flowPanel.add( new JButton( "x" ) ); flowPanel.add( new JButton( "x" ) ); } } 
+7
java layout-manager swing fixed-width
source share
4 answers

Wrap Layout should help.

+9
source share

The best way to provide the kind of advanced view configuration you want is to replace FlowLayout powerful GridBagLayoutManager . It is worth noting that the restrictions expressed by GridBagConstraints are usually much easier to understand when using free subclasses such as GBC .

Finally, as always, you should consider viewing the Swing tutorial .

0
source share
  • You cannot add children to the frame directly. Always add to contentPane this.getContentPage().add(...)

  • To make your layout, place a JPanel with a GridBagLayout on the content page and make this panel fill the content page.

  • Give the columns a GridBagLayout 2.

Now you can’t disable horizontal window resizing, so you should find another way to handle the extra space. I suggest specifying a fixed width in the first column (set fill=NONE ), and let panel C fill the remaining space.

If you set the size of panel B to the required size for all sizes (minimum, maximum, preferred), it should no longer be resized.

Tip. If the Swing layout does not work, paste it into another layout.

0
source share

Everything that you entered in the LINE_END or LINE_START part of BorderLayout will maintain a constant width: only the height will be changed if the container is resized

NORTH and SOUTH are variable widths

CENTER is a variable of width and height

LINE_START and LINE_END are just a variable height

So, I suggest you place your container β€œC” in this view of the BorderLayout area

0
source share

All Articles