An SO User
An SO User

Reputation: 24998

Why does GridBagLayout provide strange result?

enter image description here

I want the various components to spread out and fill the entire window.

Have you tried anything else? Yes, I tried GridLayout but then the buttons look huge. I also tried pack() which made the window small instead. The window should be 750x750 :)

What I was trying is this:

  • These 4 buttons on the top as a thin strip
  • The scroll pane with JPanels inside which will contain all the video conversion tasks. This takes up the maximum space
  • A JPanel at the bottom containing a JProgressBar as a thin strip.
  • But something seems to have messed up somewhere. Please help me solve this

    SSCCE

    import java.awt.*;
    import javax.swing.*;
    import com.explodingpixels.macwidgets.*;
    
    public class HudTest {
        public static void main(String[] args) {
            HudWindow hud = new HudWindow("Window");
            hud.getJDialog().setSize(750, 750);
            hud.getJDialog().setLocationRelativeTo(null);
            hud.getJDialog().setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            JPanel buttonPanel = new JPanel(new FlowLayout());
    
    
            JButton addVideo = HudWidgetFactory.createHudButton("Add New Video");
            JButton removeVideo = HudWidgetFactory.createHudButton("Remove Video");
            JButton startAll = HudWidgetFactory.createHudButton("Start All Tasks");
            JButton stopAll = HudWidgetFactory.createHudButton("Stop All Tasks");
    
            buttonPanel.add(addVideo);
            buttonPanel.add(startAll);
            buttonPanel.add(removeVideo);
            buttonPanel.add(stopAll);
    
            JPanel taskPanel = new JPanel(new GridLayout(0,1));
            JScrollPane taskScrollPane = new JScrollPane(taskPanel);
            IAppWidgetFactory.makeIAppScrollPane(taskScrollPane);
            for(int i=0;i<10;i++){
                ColorPanel c = new ColorPanel();
                c.setPreferredSize(new Dimension(750,100));
                taskPanel.add(c);
            }
    
            JPanel progressBarPanel = new JPanel();
    
            JComponent component = (JComponent) hud.getContentPane();
            component.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            Insets in = new Insets(2,2,2,2);
    
            gbc.insets = in;
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridwidth = 10;
            gbc.gridheight = 1;
            gbc.fill = GridBagConstraints.BOTH;
            component.add(buttonPanel,gbc);
    
            gbc.gridy += 1;
            gbc.gridheight = 17;
            component.add(taskScrollPane,gbc);
    
            gbc.gridy += 17;
            gbc.gridheight = 2;
            component.add(progressBarPanel,gbc);
    
            hud.getJDialog().setVisible(true);
        }
    }
    

    Upvotes: 1

    Views: 235

    Answers (2)

    nIcE cOw
    nIcE cOw

    Reputation: 24626

    Why not simply place three JPanels on top of one JPanel with BorderLayout as Layout Manager, where the middle JPanel with all custom panels with their respective sizes can be accommodated inside a JScrollPane, as shown in the below example :

    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
    
    /**
     * Created with IntelliJ IDEA.
     * User: Gagandeep Bali
     * Date: 5/17/13
     * Time: 6:09 PM
     * To change this template use File | Settings | File Templates.
     */
    public class PlayerBase
    {
        private JPanel contentPane;
        private JPanel buttonPanel;
        private JPanel centerPanel;
        private CustomPanel[] colourPanel;
        private JPanel progressPanel;
    
        private JButton addVideoButton;
        private JButton removeVideoButton;
        private JButton startAllButton;
        private JButton stopAllButton;
    
        private JProgressBar progressBar;
    
        private Random random;
    
        public PlayerBase()
        {
            colourPanel = new CustomPanel[10];
    
            random = new Random();
        }
    
        private void displayGUI()
        {
            JFrame playerWindow = new JFrame("Player Window");
            playerWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            contentPane = new JPanel(new BorderLayout(5, 5));
            contentPane.setBorder(
                    BorderFactory.createEmptyBorder(5, 5, 5, 5));
            buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
            addVideoButton = new JButton("Add New Video");
            removeVideoButton = new JButton("Remove Video");
            startAllButton = new JButton("Start all tasks");
            stopAllButton = new JButton("Stop all tasks");
    
            buttonPanel.add(addVideoButton);
            buttonPanel.add(removeVideoButton);
            buttonPanel.add(startAllButton);
            buttonPanel.add(stopAllButton);
    
            contentPane.add(buttonPanel, BorderLayout.PAGE_START);
    
            JScrollPane scroller = new JScrollPane();
            centerPanel = new JPanel(new GridLayout(0, 1, 2, 2));
            for (int i = 0; i < colourPanel.length; i++)
            {
                colourPanel[i] = new CustomPanel(new Color(
                        random.nextInt(255), random.nextInt(255)
                        , random.nextInt(255)));
                centerPanel.add(colourPanel[i]);
            }
            scroller.setViewportView(centerPanel);
    
            contentPane.add(scroller, BorderLayout.CENTER);
    
            progressPanel = new JPanel(new BorderLayout(5, 5));
            progressBar = new JProgressBar(SwingConstants.HORIZONTAL);
            progressPanel.add(progressBar);
    
            contentPane.add(progressPanel, BorderLayout.PAGE_END);
    
            playerWindow.setContentPane(contentPane);
            playerWindow.pack();
            //playerWindow.setSize(750, 750);
            playerWindow.setLocationByPlatform(true);
            playerWindow.setVisible(true);
        }
    
        public static void main(String[] args)
        {
            Runnable runnable = new Runnable()
            {
                @Override
                public void run()
                {
                    new PlayerBase().displayGUI();
                }
            };
            EventQueue.invokeLater(runnable);
        }
    }
    
    class CustomPanel extends JPanel
    {
        public CustomPanel(Color backGroundColour)
        {
            setOpaque(true);
            setBackground(backGroundColour);
        }
    
        @Override
        public Dimension getPreferredSize()
        {
            return (new Dimension(750, 100));
        }
    }
    

    OUTPUT :

    PLAYERLAYOUT

    Upvotes: 2

    K Adithyan
    K Adithyan

    Reputation: 396

    Use this

    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.fill = GridbagConstraints.BOTH
    

    Upvotes: 2

    Related Questions