Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: GridBagLayout help

  1. #1
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default GridBagLayout help

    I may just be totally misusing GridBagLayout, but, given the tutorials I have read, I don't understand how I might be.

    The code is:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class ButtonsGUI extends JFrame
    {
        JButton button;
        JLabel label = new JLabel("No buttons clicked");
        Flash flash;
        ActionListener bl;
     
     
        ButtonsGUI()
        {
            setSize(275, 275);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setVisible(true);
     
            setLayout(new GridBagLayout());
            GridBagConstraints con = new GridBagConstraints();
     
            bl = new ButtonsListener(this);
     
            //flash = new Flash(label);
     
            con.gridx = 0;
            con.gridy = 0;
            con.gridwidth = 2;
            add(label);
     
            button = new JButton("Left");
            con.gridx = 0;
            con.gridy = 1;
            con.gridwidth = 1;
            button.addActionListener(bl);
            add(button);
     
            button = new JButton("Right");
            con.gridx = 1;
            con.gridy = 1;
            con.gridwidth = 1;
            button.addActionListener(bl);
            add(button);
        }
    }
     
    class ButtonsListener implements ActionListener
    {
        String butStr;
        String labStr;
        String newStr;
        JLabel lab;
     
        ButtonsListener(ButtonsGUI bg)
        {
            lab = bg.label;
        }
     
        public void actionPerformed(ActionEvent ae)
        {
            butStr = ae.getActionCommand();
            labStr = lab.getText();
     
            if (labStr.equals("No buttons clicked"))
            {
                newStr = labStr.substring(2, 9) + labStr.substring(10, 18);
     
                if (butStr.equals("Left"))
                {
                    lab.setText(butStr + newStr);
                }
                else if (butStr.equals("Right"))
                {
                    lab.setText(butStr + newStr);
                }
            }
            else if (labStr.equals("Left button clicked"))
            {
                newStr = labStr.substring(4, 19);
     
                if (butStr.equals("Right"))
                {
                    lab.setText(butStr + newStr);
                }
            }
            if (labStr.equals("Right button clicked"))
            {
                newStr = labStr.substring(5, 20);
     
                if (butStr.equals("Left"))
                {
                    lab.setText(butStr + newStr);
                }
            }
     
        }    
    }
     
    class Buttons
    {
        public static void main(String args[])
        {
            new ButtonsGUI();
        }
    }

    Currently, all the elements are displaying in the same row. I thought that I had correctly used the constraints to specify column and row, but, apparently, I don't know how to specify constraints.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: GridBagLayout help

    You must pass the constraints you've created/modified to parent component when you add a child. See
    How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

Similar Threads

  1. GridBagLayout display problem
    By mjpam in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 5th, 2011, 04:58 PM
  2. Building a small app... (GridBagLayout problems)
    By lowfatbeans in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 14th, 2011, 06:49 PM
  3. GridBagLayout help
    By mjpam in forum Java Theory & Questions
    Replies: 2
    Last Post: August 19th, 2010, 06:35 PM
  4. [SOLVED] how can i resize JTextField, JButton using GridBagLayout,... TNX
    By sny in forum AWT / Java Swing
    Replies: 0
    Last Post: March 4th, 2010, 09:57 AM
  5. [SOLVED] Problem with GridBagLayout
    By lumpy in forum AWT / Java Swing
    Replies: 3
    Last Post: February 28th, 2010, 12:58 PM