-1

I'm a newbie in Java and I wish to add an action to a button in a JFrame frame.

When I use actionListener in the same class everything is fine but when I put it in another class called Orga_Listener to make my code cleaner, I get the "awt-event-0" error. I know it's because the action is not received by my new class.

Could someone explain m how can I get my ActionListener defined in another class to work with a button in my FrameOrganization JFrame.

Below is the source code for both frame and my ActionListner derived classes.

import ActionListener.Orga_Listener;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import javax.swing.*;


public class FrameOrganisation extends JFrame {
  private JTextField jtf = new JTextField("");
  private JLabel label = new JLabel("TITRE");

  public FrameOrganisation() {
      createAndShowGUI();
  }

  private void createAndShowGUI() {

      setTitle("Organisation");
      setSize(400,400);
      setVisible(true);
      setLocationRelativeTo(null);

      JPanel top = new JPanel();
      Font police = new Font("Arial", Font.BOLD, 14);
      jtf.setFont(police);
      jtf.setPreferredSize(new Dimension(100, 20));
      top.add(label);
      top.add(jtf);
      getContentPane().add(top, BorderLayout.PAGE_START);

      JPanel buttonPane = new JPanel();
      JButton button = new JButton("Terminé");
      buttonPane.add(button);

      button.addActionListener(new Orga_Listener());
      getContentPane().add(buttonPane, BorderLayout.PAGE_END);
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

@Override
public JRootPane createRootPane() {
    JRootPane rootPan = new JRootPane();
    KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
    Action action = new AbstractAction() {

        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent e) {
            setVisible(false);
            dispose();
        }                
    };
    InputMap inputMap = rootPan.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, "ESCAPE");
    rootPan.getActionMap().put("ESCAPE", action);
    return rootPan;
  }
}

and the second one:

public class Orga_Listener implements ActionListener { 
  private FrameOrganisation dialog;

    //close and dispose of the window.
    public void actionPerformed(ActionEvent e) {
        dialog.setVisible(false);
        dialog.dispose();
    }
}

And the error i get:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ActionListener.Orga_Listener.actionPerformed(Orga_Listener.java:21)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6297)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
at java.awt.Component.processEvent(Component.java:6062)
at java.awt.Container.processEvent(Container.java:2039)
at java.awt.Component.dispatchEventImpl(Component.java:4660)
at java.awt.Container.dispatchEventImpl(Container.java:2097)
at java.awt.Component.dispatchEvent(Component.java:4488)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4236)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166)
at java.awt.Container.dispatchEventImpl(Container.java:2083)
at java.awt.Window.dispatchEventImpl(Window.java:2489)
at java.awt.Component.dispatchEvent(Component.java:4488)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:674)
at java.awt.EventQueue.access$400(EventQueue.java:81)
at java.awt.EventQueue$2.run(EventQueue.java:633)
at java.awt.EventQueue$2.run(EventQueue.java:631)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:647)
at java.awt.EventQueue$3.run(EventQueue.java:645)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:644)
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)
Nderycke
  • 13
  • 3

1 Answers1

0

The problem is you try to invoke a method on a dialog but you don't have a reference to the dialog.

One solution is to find the dialog by using the information in the ActionEvent:

Component component = (Component)e.getSource();
Window window = SwingUtilities.windowForComponent( component );
window.setVisible( false );
window.dispose();
camickr
  • 321,443
  • 19
  • 166
  • 288