Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I close a single frame in Java?
    primarykey
    data
    text
    <p>So, I have a frame(awt) with a button, and if you click "Sync" it makes a popup. Right now, I simply want it so you can click the X on that popup, and the popup will go away. I have a comment showing where I want it to go away.</p> <pre><code>import java.awt.*; import java.awt.event.*; public class Main { public static void main(String[] args) { ActionListener listen=new MyActionListener(); Frame frame= new Frame("frame");//make the frame Button exit=new Button("Exit"); exit.setActionCommand("Exit"); exit.addActionListener(listen); Button Sync=new Button("Sync"); Sync.setActionCommand("Sync"); Sync.addActionListener(listen);//create Exit and Sync buttons. frame.add(exit); frame.add(Sync); frame.setLayout(new GridLayout(2, 1)); frame.setSize(100,80); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void MyActionListener(ActionEvent e) { System.exit(0); } } class MyActionListener implements ActionListener{ public void actionPerformed(ActionEvent ae) { String s = ae.getActionCommand(); if (s.equals("Exit")) { System.exit(0); }//note that the exit button DOES work, so this system is working. if (s.equals("Sync")) { Frame frame2= new Frame("Popup!"); frame2.setVisible(true); frame2.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { //Remove frame2 here. } }); } } } </code></pre> <p>I saw a very similar (if not the same) question asked before, and the "answer" was to use dispose. ("frame2.dispose();") I tried that, and i got the following error report:</p> <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - local variable frame2 is accessed from within inner class; needs to be declared final at vexscouting.MyActionListener$1.windowClosing(Main.java:76) at java.awt.Window.processWindowEvent(Window.java:1865) at java.awt.Window.processEvent(Window.java:1823) at java.awt.Component.dispatchEventImpl(Component.java:4630) at java.awt.Container.dispatchEventImpl(Container.java:2099) at java.awt.Window.dispatchEventImpl(Window.java:2478) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) </code></pre> <p>Note: I'm using NetBeans</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload