Shannon Shannon - 1 year ago 64
Java Question

JDialog has different behavior each time it is created

I created a class called

that extends
, 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

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");
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.");
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");

rdbtnTimeGraph = new JRadioButton("Time Graph");
contentPanel.add(rdbtnTimeGraph, "cell 0 1");

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.)");
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");

Component verticalStrut_1 = Box.createVerticalStrut(10);
contentPanel.add(verticalStrut_1, "cell 0 5");

txtPaneTypeError = new JTextPane();
txtPaneTypeError.setText("Graph Type Error: ");
contentPanel.add(txtPaneTypeError, "cell 0 6,grow");

txtPaneNameError = new JTextPane();
txtPaneNameError.setText("Graph Name Error: ");
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");

cancelButton = new JButton("Cancel");

I also have
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

EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CreateNewGraph add = new CreateNewGraph();
} catch (Exception e) {

This is what happens when it opens the first time:
First time

This first case is exactly what it should look like. But the second time, this occurs:
Second time small
This is the second case, expanded to fill my screen:
Second time full

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download