ActionListeners

I know this really doesnt relate direclty to gaming but I couldn’t find a more complete Java commuinity than this so I’ll ask it here :). Ok I’m pretty much completely noob, and newless so heres what I need to know, how can I get this set-up to work? The main thing I think I need is what to put connectItem.addActionListener(super); in the place of super. Thanks.



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Gui extends ActionListener
{      
      public static void makeMainGui()
      {
            JFrame frame = new JFrame("Chat");
            frame.setSize(500, 400);
            
            // COMPONENTS
            
            JMenuBar menuBar = new JMenuBar();
            JMenu connectMenu = new JMenu("Connect");
            JMenu aboutMenu = new JMenu("About");
            JMenuItem connectItem = new JMenuItem("Connect to server");
            JMenuItem serverItem = new JMenuItem("Act as server");
                  connectItem.addActionListener(super);
                  serverItem.addActionListener(super);
                  
                  connectMenu.add(connectItem);
                  connectMenu.add(serverItem);
                  menuBar.add(connectMenu);
                  menuBar.add(aboutMenu);
            
            JTextArea textArea = new JTextArea();
            JTextField textField = new JTextField();
            
            frame.getContentPane().setLayout(new BorderLayout());
            frame.getContentPane().add(textArea, BorderLayout.CENTER);
            frame.getContentPane().add(textField, BorderLayout.SOUTH);
            
            // END COMPONENTS
            
            frame.setJMenuBar(menuBar);            
            frame.setVisible(true);      
      }
      
      public void actionPerformed(ActionEvent e)
      {
            if(e.getSource() == connectItem)
            {
                  
            }
            if(e.getSource() == serverItem)
            {
                  
            }
      }
}


Use ‘this’ instead of ‘super’. ‘this’ refers to the instance of the class Gui.

Hmm but “this” cant be referenced from a static context. Im sure there is some easy way to do this I’m just tired and cant quite get a hold of it. I never really understood implements and extends either hehe.

OH heh Ill just replace my static method makemaingui with a constructor :stuck_out_tongue:

Lol that didnt really work either, ok now the only problem is that it “no interface expected here” for “class Gui extends ActionListener” Im not sure why it would say that did I type it wrong? I imported java.awt.event.* But nonnus thanks for the “this” thing thats working now.

You can’t extend an interface (which is what ActionListener is). You need to implement it, so replace “extends” with “implements”.

And set makeMainGui() to be non-static, that will allow you to use “this”.

[quote]I never really understood implements and extends either
[/quote]
Those are fundamental ideas - you can’t hope to write non-trivial Java programs without understanding them. Check out Bruce Eckel’s online book “Thinking in Java” for an in-depth explanation.

HTH,
20thCB

Thanks so much everyone, Ill read up on implements and extends in that book thanks 20thcenturyboy.