0

I have some problems with using dispose() method in my GUI project. I' am making a GUI swing application for some kind of Elections in IntelliJ.

My problem is, by clicking a button(Confirm1, or 2 or 3) I want to open new JFrame which is checking the age of voter and closes the current JFrame where this button is located by calling dispose().

But frame.dispose(); doesn't work.

I have my JFrame declared in public static main().

Should I make reference for it in my ActionListener? I have been looking for solution, but I couldn't find any.

Here is a code:

import javax.swing.*;  //another libraries


public class ElectionGUI {

    private JPanel labelElection;  // another texfields or etc.


    private JButton Confirm1;
    private JButton Confirm3;
    private JButton Confirm2;
    private JPanel Elections;

    public VotesGUI(){
        Votes votes = new Votes("...","...",0);

        listX.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if(!e.getValueIsAdjusting()){
                    NrX.setText(listX.getSelectedValue().toString());
                }    
            }
        });
        listY.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if(!e.getValueIsAdjusting()){
                    NrY.setText(listY.getSelectedValue().toString());
                }
            }
        });

        listZ.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if(!e.getValueIsAdjusting()){
                    NrZ.setText(listZ.getSelectedValue().toString());
                }
            }
        });
        Confirm1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                votes.VotesX();
                votes.countVotes();
                CheckAge age = new CheckAge();
                age.Check();                        /// referention, to my next //Jframe called psvm Check();
            }
        });
        Confirm2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                votes.VotesY();
                votes.countVotes();
                CheckAge age = new CheckAge();
               age.Check();
            }
        });
        Confirm3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                votes.VotesZ();
                votes.countVotes();
                CheckAge age = new CheckAge();
                age.Check();
            }
        });
    }

    public static void main(String[] args) {
       JFrame frame = new JFrame("Elentions");
       frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       frame.setVisible(true);
        frame.setContentPane(new ElectionGUI().labelElection);
        frame.pack();
    }
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Jack G.
  • 21
  • 4
  • 1
    1) I don't see any `dispose()` call in your code. 2) See [The use of multiple JFrames, good / bad practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice) (BAD). 3) Please follow the [Java naming conventions](http://www.oracle.com/technetwork/java/codeconventions-135099.html) your variable names as well as your method names should start with lowerCamelCase – Frakcool Jan 19 '17 at 17:06
  • 1
    4) For better help sooner please post a valid [mcve] or [Short, Self Contained, Correct Example](http://sscce.org/) that demonstrates your issue as we don't know how `Check()` method or `Votes` class look like – Frakcool Jan 19 '17 at 17:07

0 Answers0