0

My problem is that I have a class that when the user types out the text displayed dispose() is called, which works the first time but if you don't close the program and open it again, dispose() is called, but doesn't do anything which breaks the program.

public class TypeMenu extends JDialog {

    protected final JPanel contentPanel = new JPanel();
    protected static JTextField inputTxtField;
    protected static JTextField textField;
    protected static JTextField introTxtField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            Easy dialog = new Easy();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     * @param introTxtField2 
     * @param textField2 
     * @param inputTxtField2 
     */
    public TypeMenu(JTextField inputTxtField2, JTextField introTxtField2, JTextField textField2) {
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);
        contentPanel.add(getInputTxtField());
        contentPanel.add(getTextField());
        contentPanel.add(getIntroTxtField());
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
        }
    }
    protected JTextField getInputTxtField() {
        if (inputTxtField == null) {
            inputTxtField = new JTextField();
            inputTxtField.setHorizontalAlignment(SwingConstants.CENTER);
            inputTxtField.addKeyListener(new KeyAdapter() {
                @Override
                public void keyReleased(KeyEvent arg0) {
                    String strField = textField.getText();
                    char key = arg0.getKeyChar();
                    int length = strField.length();
                    if (Character.toLowerCase(strField.charAt(0)) == Character.toLowerCase(key)) {
                        inputTxtField.setText(" ");
                        textField.setText(strField.substring(1));
                        System.out.println(length);
                        System.out.println(strField);
                        if (length - 1 <= 0) {
                            dispose();
                        }
                    } else {
                        inputTxtField.setText(" ");
                    } 
                }
            });
            inputTxtField.setBounds(56, 177, 314, 40);
            inputTxtField.setColumns(10);
        }
        return inputTxtField;
    }
    protected JTextField getIntroTxtField() {
        if (introTxtField == null) {
            introTxtField = new JTextField();
            introTxtField.setHorizontalAlignment(SwingConstants.CENTER);
            introTxtField.setFont(new Font("Tahoma", Font.BOLD, 15));
            introTxtField.setText("Easy Mode");
            introTxtField.setEditable(false);
            introTxtField.setBounds(56, 11, 314, 29);
            introTxtField.setColumns(10);
        }
        return introTxtField;
    }
    private JTextField getTextField() {
        if (textField == null) {
            textField = new JTextField();
            textField.setHorizontalAlignment(SwingConstants.CENTER);
            textField.setFont(new Font("Monospaced", Font.BOLD, 20));
            textField.setBounds(10, 51, 414, 40);
        }
        return textField;
    }
}

This is one of the child classes

public class Easy extends TypeMenu {

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            Easy dialog = new Easy();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public Easy() {
        super(inputTxtField, introTxtField, textField);
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);
        contentPanel.add(getInputTxtField());
        contentPanel.add(getIntroTxtField());
        getString();
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            textField.selectAll();
        }
    }
    private void getString() {
        String str = textField.getText();
        if (str.equals("")) {
            String generator = StringGenerator.medium();
            String nStr = "" + generator;
            textField.setText(nStr);
        }
    }
}

The code that calls this class

public class StartMenu extends JDialog {

private final JPanel contentPanel = new JPanel();
private JTextField introTxt;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    try {
        StartMenu dialog = new StartMenu();
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Creates the dialog and creates the buttons that take the user to each variation of the game when pressed.
 */
public StartMenu() {
    setBounds(100, 100, 450, 300);
    getContentPane().setLayout(new BorderLayout());
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    contentPanel.setLayout(null);
    {
        introTxt = new JTextField();
        introTxt.setFont(new Font("Tahoma", Font.BOLD, 12));
        introTxt.setHorizontalAlignment(SwingConstants.CENTER);
        introTxt.setText("Start Menu\r\n");
        introTxt.setEditable(false);
        introTxt.setBounds(75, 11, 276, 20);
        contentPanel.add(introTxt);
        introTxt.setColumns(10);
    }
    {
        JButton btnEasyButton = new JButton("Easy Mode");
        btnEasyButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                new Easy().setVisible(true);
            }
        });
        btnEasyButton.setBounds(141, 42, 140, 23);
        contentPanel.add(btnEasyButton);    
    }
    {
        JButton btnMediumButton = new JButton("Medium Mode");
        btnMediumButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new Medium().setVisible(true);
            }
        });
        btnMediumButton.setBounds(141, 81, 140, 23);
        contentPanel.add(btnMediumButton);
    }
    {
        JButton btnHardButton = new JButton("Hard Mode");
        btnHardButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new Hard().setVisible(true);
            }
        });
        btnHardButton.setBounds(141, 120, 140, 23);
        contentPanel.add(btnHardButton);
    }
    {
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
        getContentPane().add(buttonPane, BorderLayout.SOUTH);
        {
            JButton okButton = new JButton("OK");
            okButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dispose();
                }
            });
            okButton.setActionCommand("OK");
            buttonPane.add(okButton);
            getRootPane().setDefaultButton(okButton);
        }
    }
}

}

Bradeurs
  • 53
  • 1
  • 8

2 Answers2

1

your text field is static so he will have only one instance across the application. so in the method getIntroTxtField() you have if statement that says :

if (introTxtField == null) 

in the first time this condition is true but when you create the new instance this condition is false because the instance of the static field is all ready created in the first and you are adding the key listener inside the condition so the action listener will be added only in the first creation. if you need to keep the static because you need in other class you need to remove the == null

protected JTextField getInputTxtField() {
    inputTxtField = null;
   {
        inputTxtField = new JTextField();
        inputTxtField.setHorizontalAlignment(SwingConstants.CENTER);
        inputTxtField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent arg0) {
                String strField = textField.getText();
                char key = arg0.getKeyChar();
                int length = strField.length();
                if (Character.toLowerCase(strField.charAt(0)) == Character.toLowerCase(key)) {
                    inputTxtField.setText(" ");
                    textField.setText(strField.substring(1));
                    System.out.println(length);
                    System.out.println(strField);
                    if (length - 1 <= 0) {
                        dispose();
                    }
                } else {
                    inputTxtField.setText(" ");
                } 
            }
        });
        inputTxtField.setBounds(56, 177, 314, 40);
        inputTxtField.setColumns(10);
    }
    return inputTxtField;
}

or remove the static from you field declaration static is used in shared instace only when you are using only one instace across the application like sessionFactory or any thing that need to be created only one time.

Charif DZ
  • 14,415
  • 3
  • 21
  • 40
0

you code is a lot to read but i think your problem is that you are calling the dispose() of the wrong object. may you are calling it always for the first object and you are creating a new one, and the dispose of this new dialog will never be call,Check you code may be you are creating many object and the dispose() is not called at all. make sure that you have full control of your objects instance to know if you are diposing the wanted dialog.

Charif DZ
  • 14,415
  • 3
  • 21
  • 40
  • But since the method getInputTxtField is called in the child classes, doesn't that mean that dispose would be called on that class, not the parent class? – Bradeurs Mar 03 '17 at 21:36
  • do some debuge or print some message to see if the code is warking like it should try setVisble(false) to see if the dialog will hide – Charif DZ Mar 04 '17 at 08:46
  • but your programe runs as it should when i type the same string disposed normally ?!! what does you program do? – Charif DZ Mar 05 '17 at 07:51
  • This class is called by another class and runs fine the first time, but when it's called the second time the dispose just doesn't work and it stays open leaving it to create a null string error – Bradeurs Mar 05 '17 at 10:03
  • I added in the other class that calls it – Bradeurs Mar 05 '17 at 21:41