Reputation: 100
I created a class called CreateNewGraph
that extends JDialog
, and this class is called from a separate class. The first time it is called, it behaves as expected, but any time after that, the layout (I am using MigLayout) gets all messed up: components are not spaced correctly, and each component is added twice.
Sorry in advance for so much code, I want to include all of it because I don't know where the problem is.
Here is the code for CreateNewGraph
:
public class CreateNewGraph extends JDialog {
private static final JPanel contentPanel = new JPanel();
private static JPanel buttonPanel;
private static JTextField txtFldName;
private static Font directionFont = new Font("TimesRoman", Font.PLAIN, 15);
private static JTextPane txtPaneTypeError, txtPaneNameError, txtPaneEnterName;
private static JButton okButton, cancelButton;
private static JRadioButton rdbtnXYGraph, rdbtnTimeGraph;
private static ButtonGroup kind;
private static JLabel lblWhichKind, lblName;
/**
* Create the dialog.
*/
public CreateNewGraph() {
setTitle("Add a New Graph");
setModalityType(ModalityType.APPLICATION_MODAL);
setModal(true);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(new MigLayout("", "[grow]", "[][][][][][][grow][grow]"));
lblWhichKind = new JLabel("What kind of graph would you like to add? You may select only one.");
lblWhichKind.setFont(directionFont);
contentPanel.add(lblWhichKind, "cell 0 0");
kind = new ButtonGroup();
Component strutRadioBtns = Box.createHorizontalStrut(20);
contentPanel.add(strutRadioBtns, "flowx,cell 0 1");
rdbtnXYGraph = new JRadioButton("XY Graph");
contentPanel.add(rdbtnXYGraph, "cell 0 1");
kind.add(rdbtnXYGraph);
rdbtnTimeGraph = new JRadioButton("Time Graph");
contentPanel.add(rdbtnTimeGraph, "cell 0 1");
kind.add(rdbtnTimeGraph);
Component verticalStrut = Box.createVerticalStrut(10);
contentPanel.add(verticalStrut, "cell 0 2");
txtPaneEnterName = new JTextPane();
txtPaneEnterName.setText("What would you like to name this graph? It must be a unique name (you may not use one you have already used.)");
txtPaneEnterName.setFont(directionFont);
txtPaneEnterName.setEditable(false);
txtPaneEnterName.setOpaque(false);
contentPanel.add(txtPaneEnterName, "cell 0 3,grow");
Component horizontalStrut = Box.createHorizontalStrut(20);
contentPanel.add(horizontalStrut, "flowx,cell 0 4");
lblName = new JLabel("Name:");
contentPanel.add(lblName, "cell 0 4");
txtFldName = new JTextField();
contentPanel.add(txtFldName, "cell 0 4");
txtFldName.setColumns(10);
Component verticalStrut_1 = Box.createVerticalStrut(10);
contentPanel.add(verticalStrut_1, "cell 0 5");
txtPaneTypeError = new JTextPane();
txtPaneTypeError.setText("Graph Type Error: ");
txtPaneTypeError.setEditable(false);
txtPaneTypeError.setOpaque(false);
contentPanel.add(txtPaneTypeError, "cell 0 6,grow");
txtPaneNameError = new JTextPane();
txtPaneNameError.setText("Graph Name Error: ");
txtPaneNameError.setEditable(false);
txtPaneNameError.setOpaque(false);
contentPanel.add(txtPaneNameError, "cell 0 7,grow");
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
okButton = new JButton("OK");
okButton.setActionCommand("OK");
buttonPanel.add(okButton);
cancelButton = new JButton("Cancel");
buttonPanel.add(cancelButton);
getRootPane().setDefaultButton(cancelButton);
}
I also have ActionListener
s on each button, but i removed those for the sake of conciseness. Normally, I would not post this much code, but I am not sure where the error is occurring (no exceptions are thrown.)
And, the code I am using in a different class to create an instance of CreateNewGraph
:
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CreateNewGraph add = new CreateNewGraph();
add.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
This is what happens when it opens the first time:
This first case is exactly what it should look like. But the second time, this occurs: This is the second case, expanded to fill my screen:
Upvotes: 1
Views: 40
Reputation: 285405
You're killing yourself with the static components that are being re-added to static JPanels that are ruining your GUI. Solution: make all the JDialog class fields instance fields. This will allow you to create them anew on object creation, so you don't ruin your layout. An added bonus is that you will also be following good OOPs practices.
Upvotes: 3