javaswinguser-interfacepanelflowlayout

How to use Flow Layout inside a Panel in Java?


Here is the code with problem:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;

public class xtemp {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1067, 600);
        frame.getContentPane().setBackground(new Color(0x000000));
        frame.setLayout(null);

        JPanel panel_red = new JPanel();
        panel_red.setBackground(Color.red);
        panel_red.setBounds(10, 10, 300, 300);

        JPanel panel_blue = new JPanel();
        panel_blue.setBackground(Color.blue);
        panel_blue.setBounds(10, 310, 300, 300);

        JPanel panel_green = new JPanel();
        panel_green.setBackground(Color.green);
        panel_green.setPreferredSize(new Dimension(250, 250));
        panel_green.setBounds(310, 10, 800, 600);
        panel_green.setLayout(new FlowLayout(FlowLayout.TRAILING, 9, 16));

        for (int i = 0; i <= 99; i++)
            panel_green.add(new JButton(String.format("%02d", i)));

        frame.add(panel_red);
        frame.add(panel_green);
        frame.add(panel_blue);
        frame.setVisible(true);
    }
}

And this is the output of the code:

enter image description here

I want an output similar to this:

enter image description here

I want to use multiple panels and give different layouts to different panels within a frame. I basically want that any buttons inside the panel_green must automatically get adjusted according to the space available, irrespective of the fact that it is a panel, not a frame as shown in the second gif/image.


Solution

  • Don't use null layouts. Use Swing layout managers.

    I was able to create the following GUI:

    Original GUI

    When I maximized the GUI:

    Maximized GUI

    Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section. Pay particular attention to the Laying Out Components Within a Container section.

    All Swing applications must start with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.

    I created a JFrame with a default BorderLayout.

    I created a west JPanel to hold the red JPanel and the blue JPanel. You didn't say what you wanted to happen to the red and blue JPanel when maximized, so I used a BoxLayout. That way, both JPanels would have an equal amount of height.

    I created a green JPanel and used a FlowLayout for the JButtons.

    Finally, I used methods to separate the creation of the JFrame from the creation of the JPanels. That way, the code is much easier for other people to read, I can separate my concerns, and I can focus on one small part of the GUI at a time.

    Here's the complete, runnable code.

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    
    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class BorderLayoutExample implements Runnable {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new BorderLayoutExample());
        }
    
        @Override
        public void run() {
            JFrame frame = new JFrame("Border Layout Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.add(createWestPanel(), BorderLayout.WEST);
            frame.add(createGreenPanel(), BorderLayout.CENTER);
    
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        private JPanel createGreenPanel() {
            JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
            panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            panel.setBackground(Color.green);
            panel.setPreferredSize(new Dimension(800, 600));
    
            for (int i = 0; i <= 99; i++) {
                panel.add(new JButton(String.format("%02d", i)));
            }
    
            return panel;
        }
    
        private JPanel createWestPanel() {
            JPanel panel = new JPanel();
            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
            panel.add(createColoredPanel(Color.red));
            panel.add(createColoredPanel(Color.blue));
    
            return panel;
        }
    
        private JPanel createColoredPanel(Color color) {
            JPanel panel = new JPanel();
            panel.setBackground(color);
            panel.setPreferredSize(new Dimension(300, 300));
    
            return panel;
        }
    
    }