Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GridBagLayout with Panels

Status
Not open for further replies.

pete1970

Programmer
Aug 16, 2007
10
0
0
US
I am trying to create an interface using GridBagLayout with Panels and everything is just put on one line. Here is the code:

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SMG3AuditInterface extends JFrame
{
    JComboBox comboBox;
    JLabel lblComponent, lblObjectName, lblFileName, lblDirectName;
    JTextField tfObjectName, tfFileName, tfDirectName;
    JFileChooser fc, dc;
    JButton btnSubmit, btnCancel, btnFileBrowse, btnDirectBrowse;
    JPanel comboPanel, objectPanel, filePanel, directPanel, buttonPanel;
    GridBagLayout gb;
    GridBagConstraints gbc;

    public SMG3AuditInterface()
    {
	setTitle("SMG3 Audit Interface");
	gb = new GridBagLayout();
	gbc = new GridBagConstraints();
	Container cp = getContentPane();
	cp.setLayout(gb);

        String components[] = {"BOF", "Class Set", "Derived Data Script",
                               "Index Table", "Matrix", "Policy Rule Set",
                               "Strategy Table", "Sub Population", "Subroutine Flow",
                               "User Function", "BOF - Object", "Subroutine - Object"};

	fc = new JFileChooser();
	dc = new JFileChooser();

        comboBox = new JComboBox(components);
	comboBox.setSelectedIndex(-1);
	lblComponent = new JLabel("Component Type: ");
	comboPanel = new JPanel();
	comboPanel.add(lblComponent);
	comboPanel.add(comboBox);
	cp.add(comboPanel);

	lblObjectName = new JLabel("Object name: ");
	tfObjectName = new JTextField(20);
	objectPanel = new JPanel();
    	objectPanel.add(lblObjectName);
    	objectPanel.add(tfObjectName);
    	cp.add(objectPanel);
	objectPanel.setVisible(false);

	lblFileName = new JLabel("XML Filename: ");
	tfFileName = new JTextField(20);
        btnFileBrowse = new JButton("Browse");
	filePanel = new JPanel();
    	filePanel.add(lblFileName);
    	filePanel.add(tfFileName);
    	filePanel.add(btnFileBrowse);
    	cp.add(filePanel);
	filePanel.setVisible(false);

	lblDirectName = new JLabel("XML Directory name: ");
	tfDirectName = new JTextField(20);
        btnDirectBrowse = new JButton("Browse");
	directPanel = new JPanel();
    	directPanel.add(lblDirectName);
    	directPanel.add(tfDirectName);
    	directPanel.add(btnDirectBrowse);
    	cp.add(directPanel);
	directPanel.setVisible(false);

        btnSubmit = new JButton("Submit");
        btnCancel = new JButton("Cancel");
	buttonPanel = new JPanel();
   	buttonPanel.add(btnSubmit);
    	buttonPanel.add(btnCancel);
    	cp.add(buttonPanel);
	buttonPanel.setVisible(false);

	gbc.gridx = GridBagConstraints.RELATIVE;
	gbc.gridy = GridBagConstraints.RELATIVE;
	gbc.fill = GridBagConstraints.BOTH;
	gbc.ipadx = 0;
	gbc.ipady = 0;

	gbc.gridwidth = 3;
	gbc.gridheight = 1;
	gbc.gridwidth = GridBagConstraints.REMAINDER;
	gb.setConstraints(comboPanel, gbc);
	add(comboPanel);

	gbc.gridwidth = GridBagConstraints.REMAINDER;
	gbc.gridheight = 1;
	gb.setConstraints(objectPanel, gbc);
	add(objectPanel);

	gbc.gridwidth = GridBagConstraints.REMAINDER;
	gbc.gridheight = 2;
	gb.setConstraints(filePanel, gbc);
	add(filePanel);

	gbc.gridwidth = GridBagConstraints.REMAINDER;
	gbc.gridheight = 2;
	gb.setConstraints(directPanel, gbc);
	add(directPanel);

	gbc.gridwidth = 2;
	gbc.gridheight = 1;
	gbc.gridwidth = GridBagConstraints.REMAINDER;
	gb.setConstraints(buttonPanel, gbc);
	add(buttonPanel);

        comboBox.addItemListener(new ItemListener()
        {
            public void itemStateChanged(ItemEvent e)
            {
                JComboBox cb = (JComboBox)e.getSource();
                String itemName = (String)cb.getSelectedItem();
                System.out.println("Combo Box Item Selected = " + itemName);
		buttonPanel.setVisible(true);

		if (itemName == "BOF - Object" || itemName == "Subroutine - Object")
		{
			objectPanel.setVisible(true);
			filePanel.setVisible(true);
			directPanel.setVisible(false);
		}
		else
		{
			objectPanel.setVisible(false);
			filePanel.setVisible(false);
			directPanel.setVisible(true);
		}
            }
        });

        btnSubmit.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("Submit clicked");
            }
        });

        btnCancel.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("Cancel clicked");
            }
        });

        btnFileBrowse.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("File Browser clicked");
            }
        });

        btnDirectBrowse.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("Directory Browser clicked");
            }
        });
    }


    public static void main(String[] args)
    {
	SMG3AuditInterface ai = new SMG3AuditInterface();
	ai.setDefaultCloseOperation(ai.EXIT_ON_CLOSE);
	ai.setLocation(400, 300);
	ai.pack();
	ai.show();
    }
}

Any help is appreciated.
 
What did you expect?
Why did you choose to make GridBagConstraints gbc, all the Labels, ... an attribute of the class?

Do we need all those Listeners and all those Components to answer your question, or wouldn't be 3 of them sufficient?

don't visit my homepage:
 
I obviously did not expect what I got.

I set it up very similarly to other examples I have seen.

No, I suppose you do not need all of those listeners and components to answer my question.

The way I have it set up makes sense to me but if you could point me in the right direction where I went wrong I would appreciate it.
 
Well - no.
What I mean is:
a) You don't make us know how it should look like.
b) If you reduce the code to the bare minimum, which is needed to show the problem, somebody would probably look at 30-50 lines of code.
But there are 175 lines of code.
And while reducing the problem, you'll often find the solution yourself.

don't visit my homepage:
 
I don't know if this helps or not but here is a little shorter version:
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SMG3AuditInterface extends JFrame
{
    JComboBox comboBox;
    JLabel lblComponent, lblObjectName, lblFileName, lblDirectName;
    JTextField tfObjectName, tfFileName, tfDirectName;
    JButton btnSubmit, btnCancel, btnFileBrowse, btnDirectBrowse;
    JPanel comboPanel, objectPanel, filePanel, directPanel, buttonPanel;
    GridBagLayout gb;
    GridBagConstraints gbc;

    public SMG3AuditInterface()
    {
	gb = new GridBagLayout();
	gbc = new GridBagConstraints();
	Container cp = getContentPane();
	cp.setLayout(gb);

	String components[] = {"Display directPanel",
		"Display object and filePanel"};

	comboBox = new JComboBox(components);
	comboBox.setSelectedIndex(-1);
	lblComponent = new JLabel("Component Type: ");
	comboPanel = new JPanel();
	comboPanel.add(lblComponent);
	comboPanel.add(comboBox);
	cp.add(comboPanel);

	lblObjectName = new JLabel("Object name: ");
	tfObjectName = new JTextField(20);
	objectPanel = new JPanel();
	objectPanel.add(lblObjectName);
	objectPanel.add(tfObjectName);
	cp.add(objectPanel);
	objectPanel.setVisible(false);

	lblFileName = new JLabel("XML Filename: ");
	tfFileName = new JTextField(20);
	btnFileBrowse = new JButton("Browse");
	filePanel = new JPanel();
	filePanel.add(lblFileName);
	filePanel.add(tfFileName);
	filePanel.add(btnFileBrowse);
	cp.add(filePanel);
	filePanel.setVisible(false);

	lblDirectName = new JLabel("XML Directory name: ");
	tfDirectName = new JTextField(20);
	btnDirectBrowse = new JButton("Browse");
	directPanel = new JPanel();
	directPanel.add(lblDirectName);
	directPanel.add(tfDirectName);
	directPanel.add(btnDirectBrowse);
	cp.add(directPanel);
	directPanel.setVisible(false);

	btnSubmit = new JButton("Submit");
	btnCancel = new JButton("Cancel");
	buttonPanel = new JPanel();
	buttonPanel.add(btnSubmit);
	buttonPanel.add(btnCancel);
	cp.add(buttonPanel);
	buttonPanel.setVisible(false);

	gbc.gridx = GridBagConstraints.RELATIVE;
	gbc.gridy = GridBagConstraints.RELATIVE;
	gbc.fill = GridBagConstraints.BOTH;
	gbc.ipadx = 0;
	gbc.ipady = 0;

	gbc.gridwidth = 3;
	gbc.gridheight = 1;
	gbc.gridwidth = GridBagConstraints.REMAINDER;
	gb.setConstraints(comboPanel, gbc);
	add(comboPanel);

	gbc.gridwidth = GridBagConstraints.REMAINDER;
	gbc.gridheight = 1;
	gb.setConstraints(objectPanel, gbc);
	add(objectPanel);

	gbc.gridwidth = GridBagConstraints.REMAINDER;
	gbc.gridheight = 2;
	gb.setConstraints(filePanel, gbc);
	add(filePanel);

	gbc.gridwidth = GridBagConstraints.REMAINDER;
	gbc.gridheight = 2;
	gb.setConstraints(directPanel, gbc);
	add(directPanel);

	gbc.gridwidth = 2;
	gbc.gridheight = 1;
	gbc.gridwidth = GridBagConstraints.REMAINDER;
	gb.setConstraints(buttonPanel, gbc);
	add(buttonPanel);

	comboBox.addItemListener(new ItemListener()
	{
	   public void itemStateChanged(ItemEvent e)
	   {
	      JComboBox cb = (JComboBox)e.getSource();
	      String itemName = (String)cb.getSelectedItem();
	      buttonPanel.setVisible(true);

	      if (itemName == "Display object and filePanel")
	      {
	         objectPanel.setVisible(true);
	         filePanel.setVisible(true);
	         directPanel.setVisible(false);
	      }
	      else
	      {
	         objectPanel.setVisible(false);
	         filePanel.setVisible(false);
	         directPanel.setVisible(true);
	      }
	   }
	});
    }

    public static void main(String[] args)
    {
	SMG3AuditInterface ai = new SMG3AuditInterface();
	ai.setDefaultCloseOperation(ai.EXIT_ON_CLOSE);
	ai.setLocation(400, 300);
	ai.setPreferredSize(new Dimension(400,300));
	ai.pack();
	ai.show();
    }
}

In a nutshell I have five panels. The comboPanel contains a label and comboBox; objectPanel has a label and text field; filePanel and directPanel have a label, text field and button; buttonPanel has two buttons.

When the program is first run only the comboPanel is visible. If "Display object and filePanel" is selected from the comboBox then the objectPanel and filePanel are visible, else directPanel is visible. buttonPanel is visible no matter what is selected.

I am trying to add them to the GridBayLayout below one another which is why I am using gbc.gridwidth = GridBagConstraints.REMAINDER for each but they are all showing up on the same line.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top