And to think... before watching this i was considering a switch from eclipse to net beans for the GUI thing. John has such a way with words, like an angel smacking my head with a bat of knowledge. Amazing.
At double speed, this was quite watchable. Thank you for this tutorial on the absolute basics of GridBagLayout. I'll need more to be able to properly use it, but it's a good start.
Thanks! The grid bag layout is peculiar, especially in regards to setting the width/height on components. I appreciate the straightforward tutorial to clear things up.
Best video un youtube about Java Swing! It is a huge pain, to orient components in java swing without a layout manager, and this one is just right and it center everything automatically too from what i can see, which is an added bonus.
Cool bro! Thank you so much! I finally understand the things of gridWidth and gridHeight, insets and fill. God Bless you so much! You are just amazing at explaining things in a very simple and efficient way!
Hey good tutorial, although you don't have to type in "gridheight" for every button, soon as you reset it to 1 it will stay at that till you change it again. Also by using the GridBagConstraints like you do, you will have almost unreadable code, I suggest making a private void method to set the constraints or even your own "add" method, which takes a JComponent and the properties for the constraints, the code that you present here is really redundant, but you explain the basics very well. Nice video overall :)
Great tutorial, I appreciate the simplicity of it. NOTE: When I did this, when my program run, the JFrame would show up, but my buttons would not. I had to swap the lines in the main() for jf.add(t) and jf.setVisible(true). For me now, jf.add(t) came first, then jf.setVisible(true) came next. Thanks again!
At 10:43 you can see that the height of button 1 is set to 4, so the expected result would be 0,0 to 0,3. A couple of seconds later it shows only in 0,1 to 0,3. I built the code exactly as the in tutorial and when you change the gbc.gridy from 0 to 1, suddenly the button shows in 0, 1 to 0, 4. One thought of mine was that maybe the button is created in the middle of the height -> upper border 0,0 lower border 0,3 middle 0,2 and then from there gets "stretched" to 0,0 and 0,3 while its real position is locked in 0,2. Maybe Lines that are not used by any Button are discarded by the layout manager. So in line 0 there is no button except the "stretched" end of button 1 and not a real button and is therefore discarded. This is not proven but only a vague theory of mine. If somebody knows more please answer.
Just in case I decide to change the grid height of each buttons in the future. :P You don't have to add that to every single buttons unless you are planning to overwrite the height.
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.GridBagLayout; import java.awt.GridBagConstraints; import java.awt.Insets; // The GridBagLayout class is a flexible layout manager that aligns components // vertically, horizontally or along their baseline // without requiring that the components be of the same size. public class layout4 extends JPanel{ // Each component managed by a GridBagLayout is associated with an instance of GridBagConstraints. // The constraints object specifies where a component's display area should be located on the grid // and how the component should be positioned within its display area. GridBagConstraints gbc = new GridBagConstraints(); JButton b1, b2, b3, b4, b5; layout4(){ setLayout(new GridBagLayout()); gbc.insets = new Insets(5,5,5,5); // top,left,bottom,right b1 = new JButton("Button 1"); gbc.gridx=0; gbc.gridy=0; gbc.gridheight=5; gbc.fill=GridBagConstraints.VERTICAL; add(b1,gbc); gbc.gridheight=1; // no need to add every time. b2 = new JButton("Button 2"); gbc.gridx=1; gbc.gridy=1; // gbc.gridheight=1; add(b2,gbc); b3 = new JButton("Button 3"); gbc.gridx=2; gbc.gridy=2; // gbc.gridheight=1; add(b3,gbc); b4 = new JButton("Button 4"); gbc.gridx=3; gbc.gridy=3; // gbc.gridheight=1; add(b4,gbc); b5 = new JButton("Button 5"); gbc.gridx=1; gbc.gridy=4; gbc.gridwidth=3; gbc.fill=GridBagConstraints.HORIZONTAL; add(b5,gbc); } public static void main(String[] args) { layout4 p = new layout4(); JFrame f = new JFrame(); f.setTitle("GridBagLayout"); f.setSize(500,500); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); f.add(p); } }
Great tutorial. However, wouldn't it be much quicker and more efficient if you were to put each button into an array(list) and use a for or for-each loop to modify their grid properties?
Sorry but I just used this layout and its acting strange. No matter what coordinates I set for each of the three buttons, the buttons remain at the center of the frame in one horizontal line. Please can you help?? :( I'm using netbeans
Ahmer Mirza maybe read about the weightx and weighty variables of the GridBagConstraints class. I think their default values may have something to do with why the components are in the center. docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
In your example the stretched-out button 1 doesn't start at y-position 0: its top is lined up with button 2 which should be lower than it! Why does this happen and how do I remedy it?
The button gets put in the middle of the screen help!! here is the code: GridBagConstraints gbc = new GridBagConstraints(); setLayout(new GridBagLayout()); JButton j = new JButton("button"); gbc.gridx = 2; gbc.gridy = 4; add(j,gbc); My class extends JFrame instead of JPanel. Will that affect anything??
i need your help to create a simple route map if you select place in map the place should be displayed in a textfield is it possible to this please help me..
I'm a bit confused, why do you define Tutorial as a JFrame in some videos and JPanel in others, whats the difference? and for some reason there is a roughly 4-pixel gap between an image i have put in and the top border of the window, why is that (by the way i havent set any insets)?
JFrame is just a frame, literally. Imagine wooden frame without a canvas. Yes, you can set background color to it, but you are not meant to put content directly to it. You put JPanels into a JFrame and there you draw and do your stuff.
Nice Video :D thank god for 1.25% speed though :)
Tony Deverill i think you mean 2x
+ignorantpizza 2 Maybe he enjoys uber slowmo. Don't judge!
+Tony Deverill thank you so much
+Tony Deverill Wouldn't have watched without this comment. :D I totally forgot.
i find myself using speed up mode on a ton of tuts
And to think... before watching this i was considering a switch from eclipse to net beans for the GUI thing.
John has such a way with words, like an angel smacking my head with a bat of knowledge. Amazing.
I've seen several tutorials about GribBagLayouts but this is the very first one which explains weigh and fill credentials in proper way. Thanks a lot.
At double speed, this was quite watchable. Thank you for this tutorial on the absolute basics of GridBagLayout. I'll need more to be able to properly use it, but it's a good start.
Thanks! The grid bag layout is peculiar, especially in regards to setting the width/height on components. I appreciate the straightforward tutorial to clear things up.
Best video un youtube about Java Swing! It is a huge pain, to orient components in java swing without a layout manager, and this one is just right and it center everything automatically too from what i can see, which is an added bonus.
Cool bro! Thank you so much! I finally understand the things of gridWidth and gridHeight, insets and fill. God Bless you so much! You are just amazing at explaining things in a very simple and efficient way!
Hey good tutorial, although you don't have to type in "gridheight" for every button, soon as you reset it to 1 it will stay at that till you change it again. Also by using the GridBagConstraints like you do, you will have almost unreadable code, I suggest making a private void method to set the constraints or even your own "add" method, which takes a JComponent and the properties for the constraints, the code that you present here is really redundant, but you explain the basics very well. Nice video overall :)
This was very helpful, thanks.
Great tutorial, I appreciate the simplicity of it.
NOTE: When I did this, when my program run, the JFrame would show up, but my buttons would not. I had to swap the lines in the main() for jf.add(t) and jf.setVisible(true).
For me now, jf.add(t) came first, then jf.setVisible(true) came next.
Thanks again!
Very well explained, I was looking for an understandable explanation for a long time now I have found one, thank you
GREAT video. I think gridbaglayout is the best option for buttons but it is a little complex. This video was of the perfect complexity and length
Amazing tutorial, 5 years I am programming and I never really had the strength to learn another layout. THANKS
You're weak.
+Cow Norris lol
this helped me so much, appreciate your help; very detailed explanation of everything, keep up the good work!
Melhor vdeo com este conteudo que ja vi.....
Muito obrigado
Thank you so much. Now I can layout my components properly.
Thank you so much.
You make this complicated layout look so simple.
At 10:43 you can see that the height of button 1 is set to 4, so the expected result would be 0,0 to 0,3. A couple of seconds later it shows only in 0,1 to 0,3.
I built the code exactly as the in tutorial and when you change the gbc.gridy from 0 to 1, suddenly the button shows in 0, 1 to 0, 4.
One thought of mine was that maybe the button is created in the middle of the height -> upper border 0,0 lower border 0,3 middle 0,2 and then from there gets "stretched" to 0,0 and 0,3 while its real position is locked in 0,2. Maybe Lines that are not used by any Button are discarded by the layout manager. So in line 0 there is no button except the "stretched" end of button 1 and not a real button and is therefore discarded.
This is not proven but only a vague theory of mine. If somebody knows more please answer.
Why is everything at the center of the Panel ... How to do you compress the layout so that it is not centered??
Thanks for the visual and helpful guide.
just what i need, thanks!
As a foreign english speaker i liked that a lot :)
Thanks so much. Regards from Zapopan, México. :')
Great tutorial! My favorite layout.
Very good Tutorial! Thank you very much.
great video. thanks
very thanks, from BR :D
this tutorial may be 8 years old but ty for saving me
You have just helped me very good! thank you and keep up the good worK!
Just in case I decide to change the grid height of each buttons in the future. :P You don't have to add that to every single buttons unless you are planning to overwrite the height.
Thanks brother! Great help!
Feels kinda weird to reply to comment which was made 8 yrs ago damnn :P
nice tutorial thanks for sharing it..............
Aye, thanks a lot.
thank you so much it helped me alot
Great tutorial.
Great tutorial, but it makes me drowsy...
Thank you very much!
Thankyou!😊
Thanks great video, helped a lot
awesome bro,you helped me a lot
thaaaaaaaaaaaaaaaaaaaank you ,,, U helped me a lot .... thanks again
"In the this video, I .. will be teaching YOU.. "
Oke bye.
+Daan Ghyselen what you don't want to learn? then bye byee!
thanks for help
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
// The GridBagLayout class is a flexible layout manager that aligns components
// vertically, horizontally or along their baseline
// without requiring that the components be of the same size.
public class layout4 extends JPanel{
// Each component managed by a GridBagLayout is associated with an instance of GridBagConstraints.
// The constraints object specifies where a component's display area should be located on the grid
// and how the component should be positioned within its display area.
GridBagConstraints gbc = new GridBagConstraints();
JButton b1, b2, b3, b4, b5;
layout4(){
setLayout(new GridBagLayout());
gbc.insets = new Insets(5,5,5,5); // top,left,bottom,right
b1 = new JButton("Button 1");
gbc.gridx=0;
gbc.gridy=0;
gbc.gridheight=5;
gbc.fill=GridBagConstraints.VERTICAL;
add(b1,gbc);
gbc.gridheight=1; // no need to add every time.
b2 = new JButton("Button 2");
gbc.gridx=1;
gbc.gridy=1;
// gbc.gridheight=1;
add(b2,gbc);
b3 = new JButton("Button 3");
gbc.gridx=2;
gbc.gridy=2;
// gbc.gridheight=1;
add(b3,gbc);
b4 = new JButton("Button 4");
gbc.gridx=3;
gbc.gridy=3;
// gbc.gridheight=1;
add(b4,gbc);
b5 = new JButton("Button 5");
gbc.gridx=1;
gbc.gridy=4;
gbc.gridwidth=3;
gbc.fill=GridBagConstraints.HORIZONTAL;
add(b5,gbc);
}
public static void main(String[] args) {
layout4 p = new layout4();
JFrame f = new JFrame();
f.setTitle("GridBagLayout");
f.setSize(500,500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.add(p);
}
}
Very helpful video. make it in 1.5x though
Thank you sooo much!!!
Its really helpfull....
thanks
Great tutorial. However, wouldn't it be much quicker and more efficient if you were to put each button into an array(list) and use a for or for-each loop to modify their grid properties?
Eric Cohen It would've been less code indeed, but since this is a tutorial for beginners, let's keep it simple
finally i understood it xD
thanks for sharing (Y)
Sorry but I just used this layout and its acting strange. No matter what coordinates I set for each of the three buttons, the buttons remain at the center of the frame in one horizontal line. Please can you help?? :( I'm using netbeans
Ahmer Mirza maybe read about the weightx and weighty variables of the GridBagConstraints class. I think their default values may have something to do with why the components are in the center.
docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
Thanks a lot !!
In this example, I have no way to make the buttons visible ...
Any Suggestion ?
Thanks for your very good Tutorials
Thankyou very musch sir :)
great great great great great great great great great great great great great great great great great great great Tutorial
Good tutorial. In speed 1.5
thanks, this really helped!
In your example the stretched-out button 1 doesn't start at y-position 0: its top is lined up with button 2 which should be lower than it! Why does this happen and how do I remedy it?
does it work only with buttons.
Asking because cant make it work
thank you!
great video! thanks.It helped a lot :-)
The button gets put in the middle of the screen help!!
here is the code:
GridBagConstraints gbc = new GridBagConstraints();
setLayout(new GridBagLayout());
JButton j = new JButton("button");
gbc.gridx = 2;
gbc.gridy = 4;
add(j,gbc);
My class extends JFrame instead of JPanel. Will that affect anything??
'UltimateProgramer' himself asking for help! can't believe :P
Why do you set gbc.gridheight = 1; for every Button?
i need your help to create a simple route map if you select place in map the place should be displayed in a textfield is it possible to this please help me..
IMO better will be if you import java.awt.* and javax.swing.*
I'm a bit confused, why do you define Tutorial as a JFrame in some videos and JPanel in others, whats the difference? and for some reason there is a roughly 4-pixel gap between an image i have put in and the top border of the window, why is that (by the way i havent set any insets)?
JFrame is the window, JPanel is where you create things, you create things in a JPanel and you add that JPanel to the JFrame.
JFrame is just a frame, literally. Imagine wooden frame without a canvas. Yes, you can set background color to it, but you are not meant to put content directly to it. You put JPanels into a JFrame and there you draw and do your stuff.
Watch this video at 1000x
Nope, gridwidth and fill not doing anything for me.
it is kind of stupid if you type his code it works, it is because he has multiple column so there are column that the button can take up space in
damnit whenever i type gbc, i think of gameboy color
public void setPositionAdd(Component c, int gridx, int gridy) {
gbc.gridx = gridx;
gbc.gridy = gridy;
mainPanel.add(c,gbc);
}
falls asleep... zzzzzz
ctrl + v = paste :)
1.75 speed