Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8627605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:19:21+00:00 2026-06-12T08:19:21+00:00

Hello, I am using Java Swing and I want to close a window on

  • 0

Hello,

I am using Java Swing and I want to close a window on a button click. I don’t know using an actionlistener as the best way to do this but currently I am having compilation errors with it so its must be incorrect.

Here my code:

    public class assignment2
    {

        public static void main(String[] args){
            MyFrame f = new MyFrame(); //open the inital gui interface
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true); //set it visibile
        }
    }

    //this is the initial gui screen, presenting user with options for which action they would like to take
    //al actions for the gui are commenced here
    class MyFrame extends JFrame{

        public MyFrame(){

            buttonPanel1 p = new buttonPanel1(); // add the buttons for this frame
            add(p);

            setSize(800,600);
            setTitle("Travel Console");
            setLocationRelativeTo(null);
        }
    }

    class buttonPanel1 extends JPanel{
        public buttonPanel1(){
            //create buttons
            JButton addItem = new JButton("Add an Item");
            JButton deleteItem = new JButton("Delete an item");
            JButton listItem = new JButton("List items");
            JButton editItem = new JButton("Edit an item");
            JButton bookFlight = new JButton("Book a flight");
            JButton save = new JButton("Save data");
            JButton load = new JButton("Load data");
            JButton exit = new JButton("Exit");

            //set layout manager for button panel
            setLayout(new GridLayout(8,1,1,5));

            //create buttons
            add(addItem);
            add(deleteItem);
            add(listItem);
            add(editItem);
            add(bookFlight);
            add(load);
            add(save);
            add(exit);

            addItemListener addList = new addItemListener();
            addItem.addActionListener(addList);
            exitListener exitList = new exitListener();
            exit.addActionListener(exitList);
        }

    }

    //listener classes for the inital gui page. each button has its own actionlistener which launches the selected option
    class addItemListener implements ActionListener{
        public void actionPerformed(ActionEvent event){ //launch add item
            addItemFrame addItem = new addItemFrame();
            addItem.setDefaultCloseOperation(addItemFrame.DISPOSE_ON_CLOSE);
            addItem.setVisible(true);
        }

    }
class addItemFrame extends JFrame{
    public addItemFrame(){
        addItemButtonPanel b = new addItemButtonPanel(); // add the buttons for this frame
        add(b);

        setSize(800,500);
        setTitle("Add an Item");
        setLocationRelativeTo(null);
    }
}
//part of addItemFrame class
class addItemButtonPanel extends JPanel{
    public addItemButtonPanel(){
        JLabel selectItem = new JLabel("Select which item you would like to add:");
        JButton newCustomer = new JButton("Customer");
        JButton newflight = new JButton("Flight");
        JButton newMovie = new JButton("Movie");
        JButton goBack = new JButton("Return to main menu");

        setLayout(new GridLayout(5,1,1,5));

        add(selectItem);
        add(newCustomer);
        add(newflight);
        add(newMovie);
        add(goBack);

        goBackListener gbList = new goBackListener();
        goBack.addActionListener(gbList);
    }
}

//listener classes for the addItemFrame
class goBackListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        addItemFrame.dispose();
    }

}

The problem I am having is with the last class listed goBackListener, which effectively just closes the current window so the main menu screen is displayed again. I need a static reference to addItemFrame created in the addItemListener class. But changing it to static is an invalid modifier?

How can I fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:19:22+00:00Added an answer on June 12, 2026 at 8:19 am

    try this

    //listener classes for the addItemFrame 
    class goBackListener implements ActionListener{
        private addItemFrame frame;
        public goBackListener(addItemFrame frame){
            this.frame= frame;
        }
        public void actionPerformed(ActionEvent event){
            frame.dispose();
        }
    }
    

    and send an instance of addItemFrame to it’s constructor

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I'm using this sample in Java to try to load OpenStreetMaps Offline tiles,
I am from Java Swing background. May I know why using XML to create
Hello I am using the Java Swing timer for a candle to let it
new to Laravel (coming from Java spring), while using Route::get('/', function() { return Hello
Hello i'm using map() jquery to create a new object array DEMO but this
hello i am using this Page.ClientScript.RegisterStartupScript() to call a javascript function from vb code
Hello,I'd like to ask you two questions. (I am using java and jedis) I
I have a hello-world style java ee application using maven3 glassfish3 and javaee6. I
Hello I am trying to send a push message to my device using Java.
hello i have a basic client-server system running using java sockets. my problem is,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.