Issue with Java GridBagConstraints

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Issue with Java GridBagConstraints



Click Here for Image



For part of my personal project, I want to add a music player to my program. However when I try using the GridBagConstraintsLayout to make the 3 buttons (Play, Pause, and Stop) directly under the label, the layout just looks weird like the picture that I attached. I don't want to have the play button be really long. I want all 3 buttons to be equal length and directly under the label. Can someone help me with this and maybe post the code to help me? The picture to what I have now is attached and I also included some of my code.


panel = new JPanel(new GridBagLayout());
add(panel);
GridBagConstraints c = new GridBagConstraints();
label1 = new JLabel("This is the MaryLand State Song. After exiting, press enter");
c.gridx = 0;
c.gridy = 0;

panel.add(label1,c);
play = new JButton("Play");
c.gridx = 0;
c.gridy = 2;
c.fill = GridBagConstraints.HORIZONTAL;
panel.add(play,c);
pause = new JButton("Pause");
c.gridx = 1;
c.gridy = 2;
c.fill = GridBagConstraints.HORIZONTAL;
panel.add(pause,c);
stop = new JButton("Stop");
c.gridx = 2;
c.gridy = 2;
c.fill = GridBagConstraints.HORIZONTAL;
panel.add(stop,c);




2 Answers
2



. I don't want to have the play button be really long. I want all 3 buttons to be equal length and directly under the label.



You need the constraints for the label to span 3 cells.



Read the section from the Swing tutorial on How to Use GridBagLayout. You will need to use the gridwidth constraint when you add the label to the panel.


gridwidth



I want all 3 buttons to be equal length



The GrigBagLayout will display components at their preferred size. The default preferred size of each button will be slightly different because of the text of each button.



If you really want them all the same size, then you will need to create a panel using the GridLayout and add the 3 buttons to this panel. Then you would add the label and the panel to the GridbagLayout, so you would now only have two components in a single column so you don't need to worry about the gridwidth constraint.



hope this helps


GridBagLayout lay = new GridBagLayout();
// three columns with same weights
lay.columnWeights = new double 1,1,1;
panel = new JPanel(lay);
add(panel);

GridBagConstraints c = new GridBagConstraints();

label1 = new JLabel("This is the MaryLand State Song. After exiting, press enter");
c.gridx = 0; c.gridy = 0;
// make label span all next to last columns (cells)
c.gridwidth = GridBagConstraints.REMAINDER;

panel.add(label1,c);

c.gridwidth = GridBagConstraints.BOTH;
c.fill = GridBagConstraints.HORIZONTAL;

play = new JButton("Play");
c.gridx = 0; c.gridy = 1;
panel.add(play,c);
pause = new JButton("Pause");
c.gridx = 1; c.gridy = 1;
panel.add(pause,c);
stop = new JButton("Stop");
c.gridx = 2; c.gridy = 1;
panel.add(stop,c);





(1-) 1) you do not need to renew the constraints. When you add a component with a constraint, the constraint is copied, so you just need to set the constraint that is changed. 2) The weightx constraint does not make the components an equal size. The GridBagLayout will display components at their preferred size. If the frame size is increased THEN the extra space is allocated to each component based on the weightx constraint.
– camickr
Aug 11 at 20:47






thank you @camickr, I updated my answer.
– guleryuz
Aug 12 at 13:07






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard