Results 1 to 3 of 3
  1. #1
    Zumbah is offline Member
    Join Date
    Jan 2014
    Posts
    1
    Rep Power
    0

    Default GridBagLayout inside CardLayout

    How do you use GridBagLayout and CardLayout together?

    I'm trying to add these buttons to a panel, but it breaks the GridBagLayout, here's what is in the doc;
    Java Code:
            button = new JButton("Button 1");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 0;
            pane.add(button, c);
         
            button = new JButton("Button 2");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 0.5;
            c.gridx = 1;
            c.gridy = 0;
            pane.add(button, c);
    I'm trying to do:
    Java Code:
            JPanel m = new JPanel();
         
            button = new JButton("Button 1");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 0;
            m.add(button, c);
         
            button = new JButton("Button 2");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 0.5;
            c.gridx = 1;
            c.gridy = 0;
            m.add(button, c);
            
            
            windows = new JPanel(new CardLayout());
                windows.add(m, "test");
                
            pane.add(windows, c);
    However it reverts as though it's using flowlayout.

    What is it I'm missing? Any help is much appreciated.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: GridBagLayout inside CardLayout

    You are not setting the layout of the panel, so it IS using FlowLayout, as that is the default.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: GridBagLayout inside CardLayout

    In other words, you create a Panel with a CardLayout, then for each "card", you create a Panel with a GridBagLayout, add the items for that card to that Panel, and add that Panel to the Panel with the CardLayout.

Similar Threads

  1. Replies: 1
    Last Post: 06-14-2013, 05:00 PM
  2. CardLayout... maybe
    By NotTooOld in forum New To Java
    Replies: 0
    Last Post: 05-07-2013, 05:45 PM
  3. CardLayout Help
    By David M. in forum New To Java
    Replies: 4
    Last Post: 08-06-2011, 03:57 AM
  4. Replies: 9
    Last Post: 04-12-2011, 08:26 PM
  5. Regarding CardLayout
    By adeeb in forum AWT / Swing
    Replies: 1
    Last Post: 06-07-2008, 08:52 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •