The c component is added to a parent component more than once.
pane.add(testBox, c);
pane.add(scrollPane, c);
The source code is:
- Code: Select all
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.TableColumn;
public class SwingPanel {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new GridBagLayout());
pane.setPreferredSize(new Dimension(1100, 600));
GridBagConstraints c = new GridBagConstraints();
Box testBox = Box.createHorizontalBox();
JLabel testLabel = new JLabel("test Identifier: ");
testBox.add(testLabel);
JTextField testID = new JTextField("TT123456789012");
c.gridwidth = 1;
c.gridx = 9;
c.gridy = 0;
c.anchor = GridBagConstraints.FIRST_LINE_END;
testID.setEditable(false);
testBox.add(testID);
pane.add(testBox, c);
Object rowData[][] = {
{ "100000000", "Mouse, Mickey",
"123 NOWHERE STREET, NEW YORK, NY, 12345",
"UNITED STATES", "50%" },
{ "123456789", "Peter, Pan",
"12 Main Street, Waterloo, On, S4N 4G6", "CANADA",
"50%" } };
Object columnNames[] = { "ID number", "Name", "Address", "Country",
"Role" };
JTable table = new JTable(rowData, columnNames);
table.setShowGrid(false);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
TableColumn column = null;
int width[] = { 100, 150, 300, 150, 50 };
for (int i = 0; i < 5; i++) {
column = table.getColumnModel().getColumn(i);
column.setPreferredWidth(width[i]);
}
c.gridx = 0;
c.gridy = 4;
c.insets.top = 10;
c.gridwidth = 10;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.0;
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(750, 300));
scrollPane
.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
pane.add(scrollPane, c);
c.gridx = 0;
c.gridy = 8;
c.gridwidth = 1;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.LAST_LINE_START;
JButton b1Button = new JButton("b1"), b2Button = new JButton("b2"), b3Button = new JButton(
"b3"), b3xButton = new JButton("b3 x"), b4Button = new JButton(
"b4");
Box buttonBox = Box.createHorizontalBox();
buttonBox.add(b1Button);
buttonBox.add(Box.createHorizontalStrut(160));
pane.add(buttonBox, c);
c.gridx = 6;
pane.add(b2Button, c);
c.gridx = GridBagConstraints.RELATIVE;
pane.add(b3Button, c);
pane.add(b3xButton, c);
pane.add(b4Button, c);
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("SwingPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set up the content pane.
addComponentsToPane(frame.getContentPane());
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Software versions are:
Swing Designer 8.1.1.r36x201012170656 com.instantiations.designer.swing.feature.feature.group
Eclipse IDE for Java EE Developers 1.3.1.20100916-1202 epp.package.jee
java version "1.6.0_24"