How to make this FlowLayout stream in its JSplitPane?

I wrote this sample code to illustrate the problem that I am encountering with my program.

I expect that you can move the JSplitPane slider to the left, over the edge of the buttons, compress this JPanel, and FlowLayout to wrap the buttons in the second line.

Instead, JSplitPane does not allow me to move the slider past the rightmost button on the screen, and if I resize the entire JFrame to force compression, the buttons (I suppose) just run away from the right side of the JPanel, under the slider (I think because I, obviously I don’t see them).

What am I doing wrong?

import java.awt.*; import java.io.*; import java.util.*; import javax.swing.*; public class Driver implements Runnable { public static void main(String[] args) { (new Driver()).run(); } public void run() { try { go(); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } private void go() throws Exception { JFrame jframe = new JFrame("FlowLayoutTest"); JPanel left = new JPanel(); left.setBackground(Color.RED); left.setLayout(new BorderLayout()); JPanel right = new JPanel(); right.setBackground(Color.BLUE); right.setLayout(new BorderLayout()); JSplitPane topmost = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right); jframe.setContentPane(topmost); JPanel bottomleft = new JPanel(); bottomleft.setBackground(Color.GREEN); bottomleft.setLayout(new FlowLayout()); left.add(bottomleft, BorderLayout.PAGE_END); for (int i = 0; i < 10; i++) { bottomleft.add(new JButton("" + i)); } jframe.pack(); jframe.setVisible(true); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } 
0
java layout-manager swing
source share
2 answers

Is there any other layout manager besides FlowLayout that will do what I'm looking for?

Wrap Layout should work.

+5
source share

If you add this

 bottomleft.setMinimumSize(new Dimension(0, 0)); 

before the package (), then it will be paid. BUT, it will not repackage the border layout, so instead of two lines you will get one and the rest will be disabled. Therefore, after resizing, you will have to repack the border layout. If you omit the border layout, it will be rescheduled as you want.

However, you should avoid FlowLayout like a plague. Most of the layouts that ship with Swing are poor, but FlowLayout is one of the worst offenders.

+1
source share

All Articles