Java Tutorial 47 (GUI) - GridBagLayout and GridBagConstraints

Поділитися
Вставка
  • Опубліковано 4 жов 2024

КОМЕНТАРІ • 91

  • @TheDevTony
    @TheDevTony 9 років тому +157

    Nice Video :D thank god for 1.25% speed though :)

    • @kevindunne367
      @kevindunne367 9 років тому +14

      Tony Deverill i think you mean 2x

    • @AoSCow
      @AoSCow 9 років тому +1

      +ignorantpizza 2 Maybe he enjoys uber slowmo. Don't judge!

    • @Najla533
      @Najla533 8 років тому +1

      +Tony Deverill thank you so much

    • @CP-sr6ml
      @CP-sr6ml 8 років тому +1

      +Tony Deverill Wouldn't have watched without this comment. :D I totally forgot.

    • @firebird208
      @firebird208 6 років тому

      i find myself using speed up mode on a ton of tuts

  • @maxb2402
    @maxb2402 8 років тому +9

    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.

  • @vladeb1104
    @vladeb1104 6 років тому +1

    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.

  • @Phagocytosis
    @Phagocytosis 5 років тому +3

    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.

  • @SecondShiftPleb
    @SecondShiftPleb 4 роки тому +1

    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.

  • @germangonzalez7185
    @germangonzalez7185 Рік тому

    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.

  • @francksgenlecroyant
    @francksgenlecroyant 4 роки тому +1

    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!

  • @Matt-tp9lh
    @Matt-tp9lh 8 років тому +24

    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 :)

    • @abeteme5799
      @abeteme5799 3 роки тому

      This was very helpful, thanks.

  • @kylep677
    @kylep677 10 років тому +4

    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!

  • @marcodicarlo5084
    @marcodicarlo5084 7 років тому

    Very well explained, I was looking for an understandable explanation for a long time now I have found one, thank you

  • @afifkhaja
    @afifkhaja 5 років тому

    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

  • @ChristopheStevens
    @ChristopheStevens 10 років тому

    Amazing tutorial, 5 years I am programming and I never really had the strength to learn another layout. THANKS

  • @joefads5040
    @joefads5040 10 років тому +3

    this helped me so much, appreciate your help; very detailed explanation of everything, keep up the good work!

  • @amadeujoaquim2543
    @amadeujoaquim2543 4 роки тому +1

    Melhor vdeo com este conteudo que ja vi.....
    Muito obrigado

  • @pjguitar15
    @pjguitar15 2 роки тому

    Thank you so much. Now I can layout my components properly.

  • @navid617
    @navid617 8 років тому

    Thank you so much.
    You make this complicated layout look so simple.

  • @Elatus-kn5sh
    @Elatus-kn5sh 3 роки тому +3

    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.

  • @ArbitraryP
    @ArbitraryP 5 років тому +1

    Why is everything at the center of the Panel ... How to do you compress the layout so that it is not centered??

  • @JacobL221
    @JacobL221 10 років тому

    Thanks for the visual and helpful guide.

  • @oscarsimental27
    @oscarsimental27 3 роки тому

    just what i need, thanks!

  • @DarkSilverNight
    @DarkSilverNight 11 років тому

    As a foreign english speaker i liked that a lot :)

  • @splytek2140
    @splytek2140 5 років тому

    Thanks so much. Regards from Zapopan, México. :')

  • @joedadocker
    @joedadocker 10 років тому

    Great tutorial! My favorite layout.

  • @DialecticalMaterialismRocks
    @DialecticalMaterialismRocks 5 років тому

    Very good Tutorial! Thank you very much.

  • @EchoENKO
    @EchoENKO 11 місяців тому

    great video. thanks

  • @Andersonleandrooo
    @Andersonleandrooo 8 років тому +2

    very thanks, from BR :D

  • @aafiqbukhari5383
    @aafiqbukhari5383 4 роки тому

    this tutorial may be 8 years old but ty for saving me

  • @dirkkuijer8440
    @dirkkuijer8440 6 років тому

    You have just helped me very good! thank you and keep up the good worK!

  • @JohnGizdich
    @JohnGizdich  11 років тому +4

    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.

  • @uzairafridi5589
    @uzairafridi5589 7 років тому

    nice tutorial thanks for sharing it..............

  • @DaxterSnickers
    @DaxterSnickers 3 роки тому

    Aye, thanks a lot.

  • @badeesabood8136
    @badeesabood8136 4 роки тому

    thank you so much it helped me alot

  • @tsogiaidze
    @tsogiaidze 10 років тому

    Great tutorial.

  • @ephraimbenet8881
    @ephraimbenet8881 9 років тому +7

    Great tutorial, but it makes me drowsy...

  • @yamayamayama554
    @yamayamayama554 8 років тому

    Thank you very much!

  • @_._lis_._
    @_._lis_._ 2 роки тому

    Thankyou!😊

  • @Israel9068
    @Israel9068 7 років тому

    Thanks great video, helped a lot

  • @nelaturiramakrishnareddy1535
    @nelaturiramakrishnareddy1535 7 років тому

    awesome bro,you helped me a lot

  • @mohamadbahgat
    @mohamadbahgat 8 років тому

    thaaaaaaaaaaaaaaaaaaaank you ,,, U helped me a lot .... thanks again

  • @MrOmgiamdead
    @MrOmgiamdead 8 років тому +20

    "In the this video, I .. will be teaching YOU.. "
    Oke bye.

    • @yamayamayama554
      @yamayamayama554 8 років тому +3

      +Daan Ghyselen what you don't want to learn? then bye byee!

  • @mesutuluag2791
    @mesutuluag2791 4 роки тому

    thanks for help

  • @_._lis_._
    @_._lis_._ 2 роки тому +1

    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);
    }
    }

  • @clashwithtim74
    @clashwithtim74 4 роки тому

    Very helpful video. make it in 1.5x though

  • @brodymovies
    @brodymovies 10 років тому

    Thank you sooo much!!!

  • @AstroWithRaj
    @AstroWithRaj 11 років тому

    Its really helpfull....
    thanks

  • @rickbox99
    @rickbox99 9 років тому

    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?

    • @カラスKarasu
      @カラスKarasu 9 років тому +4

      Eric Cohen It would've been less code indeed, but since this is a tutorial for beginners, let's keep it simple

  • @joanrodriguez7345
    @joanrodriguez7345 8 років тому

    finally i understood it xD
    thanks for sharing (Y)

  • @ahmermirza
    @ahmermirza 7 років тому +3

    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

    • @poopsintub
      @poopsintub 7 років тому

      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

  • @GiancarloRonchi
    @GiancarloRonchi 8 років тому

    Thanks a lot !!
    In this example, I have no way to make the buttons visible ...
    Any Suggestion ?
    Thanks for your very good Tutorials

  • @muhammadzaryabsiddiquekhan4594
    @muhammadzaryabsiddiquekhan4594 6 років тому

    Thankyou very musch sir :)

  • @molusridas
    @molusridas 12 років тому

    great great great great great great great great great great great great great great great great great great great Tutorial

  • @yusufbulentavci6092
    @yusufbulentavci6092 6 років тому +1

    Good tutorial. In speed 1.5

  • @raterake
    @raterake 11 років тому

    thanks, this really helped!

  • @ER_K
    @ER_K 8 років тому

    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?

  • @203i9
    @203i9 4 роки тому

    does it work only with buttons.
    Asking because cant make it work

  • @ahmermirza
    @ahmermirza 7 років тому

    thank you!

  • @aboorva
    @aboorva 12 років тому

    great video! thanks.It helped a lot :-)

  • @theultimateprogrammer6932
    @theultimateprogrammer6932 8 років тому

    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??

    • @ravialwayswins
      @ravialwayswins 6 років тому +2

      'UltimateProgramer' himself asking for help! can't believe :P

  • @DarkSilverNight
    @DarkSilverNight 11 років тому

    Why do you set gbc.gridheight = 1; for every Button?

  • @wingexplorers
    @wingexplorers 11 років тому

    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..

  • @jandanczura8028
    @jandanczura8028 3 роки тому

    IMO better will be if you import java.awt.* and javax.swing.*

  • @123TeeMee
    @123TeeMee 8 років тому

    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)?

    • @johnnerson4115
      @johnnerson4115 7 років тому

      JFrame is the window, JPanel is where you create things, you create things in a JPanel and you add that JPanel to the JFrame.

    • @kornelijekovac9793
      @kornelijekovac9793 7 років тому

      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.

  • @safionweb
    @safionweb 3 роки тому

    Watch this video at 1000x

  • @kornelijekovac9793
    @kornelijekovac9793 7 років тому

    Nope, gridwidth and fill not doing anything for me.

    • @noahd8673
      @noahd8673 7 років тому

      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

  • @redflameken
    @redflameken 2 роки тому

    damnit whenever i type gbc, i think of gameboy color

  • @definitelynot1562
    @definitelynot1562 4 роки тому

    public void setPositionAdd(Component c, int gridx, int gridy) {

    gbc.gridx = gridx;
    gbc.gridy = gridy;
    mainPanel.add(c,gbc);
    }

  • @DefQon1992
    @DefQon1992 8 років тому +1

    falls asleep... zzzzzz

  • @Julius-rx1dx
    @Julius-rx1dx 2 роки тому

    ctrl + v = paste :)

  • @jackprot351
    @jackprot351 4 роки тому

    1.75 speed