jesric1029
jesric1029

Reputation: 718

Closing a JFrame on action listener within method error

I am working on a project for work and have run into a bit of a roadblock. This project actually involves allowing a user to generate an XML file. The user is first presented with a number of GUI's - Those which appear depend upon selections made on the first GUI. Once the user progresses with the GUI's the data will be used to fill a JTable and then once confirmed put into an XML file.

As silly as it may sounds with something fairly complicated I have run into a problem with JFrames. Once the user fills out the information on one of the GUI screens and hits the "confirm" button I want that JFrame to go away and the next to appear. I have no problem getting the next to appear but due to the design of the class I can't figure out how to properly use the JFrame.dispose() method. I'll post my classes below:

Tester Class

package mainClasses;

import gui.AllGUI;

public class Tester 

{

    public static void main(String args[]){

        AllGUI aGUI = new AllGUI();
        aGUI.createAllGUI();

    }

}

First GUI Screen

package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class AllGUI 

{

    private static final Insets normalInsets = new Insets(10, 10, 0, 10);
    private static final Insets comboInsets = new Insets(10,10,10,10);
    public static String type = null;
    public boolean finished = false;

    public void createAllGUI(){

        JFrame frame = new JFrame("All File Types Selection");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createMainPanel());
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);


    }

    private JPanel createMainPanel(){

        JPanel mainPanel = new JPanel(new BorderLayout());
        JPanel formPanel = new JPanel(new GridBagLayout());

        int gridy=0;

        JLabel groupMessageIdTitle = new JLabel("Group Message Id:");
        addComponent(formPanel, groupMessageIdTitle,0,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
                ,GridBagConstraints.HORIZONTAL);

        JTextField groupMessageIdText = new JTextField("",10);
        addComponent(formPanel, groupMessageIdText,1,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
                ,GridBagConstraints.HORIZONTAL);

        JLabel isoDateTimeTitle = new JLabel("ISO Creation Date/Time:");
        addComponent(formPanel, isoDateTimeTitle,2,gridy,1,1,normalInsets,GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL);

        JTextField isoDateTimeText = new JTextField("",10);
        addComponent(formPanel, isoDateTimeText,3,gridy++,1,1,normalInsets,GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL);

        JLabel notificationIdTitle = new JLabel("Notification Id:");
        addComponent(formPanel, notificationIdTitle,0,gridy,1,1,normalInsets,GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL);

        JTextField notificationIdText = new JTextField("",10);
        addComponent(formPanel, notificationIdText,1,gridy,1,1,normalInsets,GridBagConstraints.LINE_START,
                GridBagConstraints.HORIZONTAL);

        JLabel notificationAcctIdTitle = new JLabel("Notification Account Id");
        addComponent(formPanel, notificationAcctIdTitle,2,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
                ,GridBagConstraints.HORIZONTAL);

        JTextField notificationAcctIdText = new JTextField("",10);
        addComponent(formPanel, notificationAcctIdText,3,gridy++,1,1,normalInsets,GridBagConstraints.LINE_START
                ,GridBagConstraints.HORIZONTAL);

        JLabel numberOfEntriesTitle = new JLabel("Number of Entries");
        addComponent(formPanel, numberOfEntriesTitle,0,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
                ,GridBagConstraints.HORIZONTAL);

        JTextField numberOfEntriesText = new JTextField("",10);
        addComponent(formPanel,numberOfEntriesText,1,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
                ,GridBagConstraints.HORIZONTAL);

        JLabel sumOfAmountsTitle = new JLabel("Sum of Amounts");
        addComponent(formPanel,sumOfAmountsTitle, 2,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
                ,GridBagConstraints.HORIZONTAL);

        JTextField sumOfAmountsText = new JTextField("",10);
        addComponent(formPanel,sumOfAmountsText,3,gridy++,1,1,normalInsets,GridBagConstraints.LINE_START
                ,GridBagConstraints.HORIZONTAL);

        JLabel fileTypeTitle = new JLabel("Camt54 File Type");
        addComponent(formPanel,fileTypeTitle,0,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
                ,GridBagConstraints.HORIZONTAL);

        String[] fileTypes = {"OTC-R Message","Home Banking","Cleared Checks"};

        final JComboBox<String> fileTypesComboBox = new JComboBox<String>(fileTypes);
        addComponent(formPanel,fileTypesComboBox,1,gridy,1,1,comboInsets,GridBagConstraints.LINE_START
                ,GridBagConstraints.HORIZONTAL);

        JPanel confirmPanel = new JPanel();

        JButton confirmButton = new JButton("Confirm");

        confirmButton.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent ae){

                if(fileTypesComboBox.getSelectedIndex()==0){
                    type="OTC";

                    TCRSpecificGUI tcrGUI = new TCRSpecificGUI();
                    tcrGUI.createTCRSpecificGUI();

                }else if(fileTypesComboBox.getSelectedIndex()==1){
                    type="HOME";
                }else if(fileTypesComboBox.getSelectedIndex()==2){
                    type="CLEARED";
                }

            }

        });

        confirmPanel.add(confirmButton);

        mainPanel.add(formPanel,BorderLayout.NORTH);

        mainPanel.add(confirmPanel,BorderLayout.CENTER);

        return mainPanel;

    }

    private void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth
            ,int gridheight, Insets insets, int anchor, int fill){

        GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 0.0D, 0.0D
                ,anchor, fill, insets, 0,0);

        container.add(component,gbc);

    }

}

Second GUI Screen

package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TCRSpecificGUI 

{

    private static final Insets normalInsets = new Insets(10,10,0,10);

    public void createTCRSpecificGUI(){

        JFrame frame = new JFrame("TCR-Specific Tags");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createMainPanel());
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

    }

    private JPanel createMainPanel(){

        JPanel mainPanel = new JPanel(new BorderLayout());
        JPanel formPanel = new JPanel(new GridBagLayout());

        int gridy=0;

        JLabel proprietaryPartyTypeTitle = new JLabel("Proprietary Party Type:");
        addComponent(formPanel,proprietaryPartyTypeTitle,0,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
                ,GridBagConstraints.HORIZONTAL);

        JTextField proprietaryPartyTypeText = new JTextField("",10);
        addComponent(formPanel, proprietaryPartyTypeText,1,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
                ,GridBagConstraints.HORIZONTAL);

        JLabel proprietaryPartyIdTitle = new JLabel("Proprietary Party ID:");
        addComponent(formPanel, proprietaryPartyIdTitle,2,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
                ,GridBagConstraints.HORIZONTAL);

        JTextField proprietaryPartyIdText = new JTextField("",10);
        addComponent(formPanel, proprietaryPartyIdText,3,gridy++,1,1,normalInsets,GridBagConstraints.LINE_START
            ,GridBagConstraints.HORIZONTAL);

        JLabel transactionDateTimeTitle = new JLabel("Transaction Date/Time:");
        addComponent(formPanel, transactionDateTimeTitle,0,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
                ,GridBagConstraints.HORIZONTAL);

        JTextField transactionDateTimeText = new JTextField("",10);
        addComponent(formPanel, transactionDateTimeText,1,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
                ,GridBagConstraints.HORIZONTAL);

        JLabel rMessageFileNameTitle = new JLabel("R-Message File Name:");
        addComponent(formPanel,rMessageFileNameTitle,2,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
                ,GridBagConstraints.HORIZONTAL);

        JTextField rMessageFileNameText = new JTextField("", 10);
        addComponent(formPanel, rMessageFileNameText,3,gridy++,1,1,normalInsets,GridBagConstraints.LINE_START
                ,GridBagConstraints.HORIZONTAL);

        JLabel supplementaryXPathTitle = new JLabel("Supplementary X-Path:");
        addComponent(formPanel, supplementaryXPathTitle,0,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
                ,GridBagConstraints.HORIZONTAL);

        JTextField supplementaryXPathText = new JTextField("",10);
        addComponent(formPanel, supplementaryXPathText,1,gridy,1,1,normalInsets,GridBagConstraints.LINE_START
                ,GridBagConstraints.HORIZONTAL);

        JPanel confirmPanel = new JPanel();

        JButton confirmButton = new JButton("Confirm");

        confirmButton.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent ae){


            }

        });

        confirmPanel.add(confirmButton);
        mainPanel.add(formPanel,BorderLayout.NORTH);
        mainPanel.add(confirmPanel,BorderLayout.CENTER);

        return mainPanel;

    }



    private void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth
            ,int gridheight, Insets insets, int anchor, int fill){

        GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 0.0D, 0.0D
                ,anchor,fill,insets,0,0);

        container.add(component,gbc);

    }

}

Specifically the JComboBox selection on the first GUI will determine which GUI appears next.

confirmButton.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent ae){

                if(fileTypesComboBox.getSelectedIndex()==0){
                    type="OTC";

                    TCRSpecificGUI tcrGUI = new TCRSpecificGUI();
                    tcrGUI.createTCRSpecificGUI();

                }else if(fileTypesComboBox.getSelectedIndex()==1){
                    type="HOME";
                }else if(fileTypesComboBox.getSelectedIndex()==2){
                    type="CLEARED";
                }

            }

        });

So for now I only have logic for if the user selects the first option in the JComboBox. This works properly and the new GUI opens however it just opens on top of the first JFrame. I have tried using JFrame.dispose() within the action listener like this:

if(fileTypesComboBox.getSelectedIndex()==0){
                    type="OTC";

                    JFrame.dispose();


                    TCRSpecificGUI tcrGUI = new TCRSpecificGUI();
                    tcrGUI.createTCRSpecificGUI();

However it is flagged as an error by Eclipse for:

Cannot make a static reference to the non-static method dispose() from the type Window

I understand why this error occurs and what the problem is however I have no idea how to fix. I have tried numerous approaches but nothing seems to work. I'd really appreciate any help in getting that first JFrame to close when the other opens.

Upvotes: 0

Views: 145

Answers (1)

camickr
camickr

Reputation: 324118

If you know the button that was clicked, then you need to find the frame the button belongs to.

So in the ActionListener of your button you can use code something like:

Component component = (Component)e.getSource();
Window window = SwingUtilties.windowForComponent(component);
window.dispose();

Upvotes: 1

Related Questions