I'm doing a prototype for showing some basic functions of a painting program. Now, here is how the window looks like:

The window is compound of a panel with a vertical BoxLayout, inside it, there are a custom image control and another panel with a FlowLayout containing all the buttons.
The problem is that I have to specify the height for the buttons panel (in 100px). If I delete that line, the window looks like this:

But, if I only specify the width, and I write a 0 for the height, I get this:

I want the layouts determine the correct height of the buttons panel by themselves. Here is the code that produces the first image:
public void createAndShowGui(){
JFrame.setDefaultLookAndFeelDecorated(true);
setTitle("SimplePaint");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
//Main container layout.
BoxLayout mainLayout = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS);
getContentPane().setLayout(mainLayout);
drawingPanel = new JPanel();
drawingPanel.setLayout(null);
drawingPanel.setPreferredSize(new Dimension(width, height));
TextBox editor = new TextBox();
drawingPanel.add(editor);
editor.setVisible(false);
canvasControl = new DrawableCanvas(editor);
drawingPanel.add(canvasControl);
canvasControl.setBounds(0, 0, width, height);
getContentPane().add(drawingPanel);
drawingSurface = canvasControl.getDrawingSurface();
//Buttons layout.
FlowLayout buttonsLayout = new FlowLayout();
JPanel buttonsPanel = new JPanel(buttonsLayout);
buttonsPanel.setPreferredSize(new Dimension(width, 100)); // <-- Not in this way
//Buttons creation...
pack();
}
Thanks in advance.
don't know how I can make the FlowLayout to grow vertically (not horizontally) just enough for showing all the buttons
FlowLayout does not do this.
You can use the Wrap Layout, which extends FlowLayout and override the preferred size method so that the component can wrap automatically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With