I am trying to create an interface using GridBagLayout with Panels and everything is just put on one line. Here is the code:
Any help is appreciated.
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.