Added actionListener, then GUI broke

Hi,
1. first i ran my menu GUI, it worked fine
2. then i added the actionListener code... now she no workie...
what did i do wrong???
ERROR MSG:
C:\jLotto\LotFrame.java:34: cannot resolve symbol
symbol : class ActionListener
location: class LottoFrame
          new ActionListener(){
^
1 error
Tool completed with exit code 1
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import java.awt.Event;
import javax.swing.JOptionPane; //print menu trial run
public class LotFrame extends JFrame
  // Constructor
  public LotFrame(String title)
    setTitle(title);                             // Set the window title
    setDefaultCloseOperation(EXIT_ON_CLOSE);     // handle exit operation
    setJMenuBar(menuBar);                        // Add the menu bar to the window
    //MAIN MENU
    JMenu fileMenu    = new JMenu("File");          // Create File menu
    JMenu findMenu    = new JMenu("Find");
    // Construct the file pull down menu
    newItem   = fileMenu.add("New");             // Add New item
    openItem  = fileMenu.add("Open");            // Add Open item
    closeItem = fileMenu.add("Close");           // Add Close item
    fileMenu.addSeparator();                      // Add separator
    saveItem  = fileMenu.add("Save");            // Add Save item
    saveAsItem= fileMenu.add("Save As...");      // Add Save As item
    fileMenu.addSeparator();                      // Add separator
    printItem.addActionListener(
           new ActionListener(){
                public void actionPerformed( ActionEvent e )
                     JOptionPane.showMessageDialog( LotFrame.this,
                     "adding actionListener at end of GUI setup",
                     "action at end", JOptionPane.PLAIN_MESSAGE);
                }//end of actionPerformed
          }//end of actionLinstener
     );//endof .addActionListener
   printItem = fileMenu.add("Print");           // Add Print item
    menuBar.add(fileMenu);                       // Add the file menu
    menuBar.add(findMenu);
  }//end of constructor
  private JMenuBar menuBar = new JMenuBar();     // Window menu bar
  // File menu items
  private JMenuItem newItem, openItem, closeItem, saveItem, saveAsItem, printItem;
}//end of class LotFrame

java.awt.event.*;
import java.awt.Event;

Similar Messages

  • Adding actionListener to JComboBox

    Hi there
    I have added actionListener to a JComboBox. I only want the actionPerformed() get called when the user choose the item in the JComboBox, however, it also get called whenever I change the items in the JComboBox.
    I have tried to call the ActionEvent.getID() method to identify the action, however, changing the items and choosing the item give me the same ID - 1001.
    Has anyone got this problem before? How did you solve it?
    Should I use other Listener for listening only the choosing event?
    In advance thanks!
    From
    Edmond

    I had many problems using actionListeners with JComboBox because it gets called all the time, I have had much better luck using mouseListener and the events getClickCount() method for single and double click selections, this avoided many of the problems for me.

  • Adding actionlistener to menuItem

    hi...i'm trying to add an actionListener to a menuItem. i looked at the tutorial on "how to use menus" but i cant seem to execute the menuitem. here's my code...
    public class Controller implements ActionListener
        GUI gui;
        public Controller(GUI g)
            gui = g;
            gui.menuItem.addActionListener(this);
        public void actionPerformed(ActionEvent e)
            gui.menuItem = (JMenuItem)(e.getSource());
            if (gui.menuItem.getText().equals("New Database"))
                System.out.println("Hello");
    }where the class GUI is where the menItems have been added to the menus. i'm just trying out for one menuItem first but i wont get the "hello" printed on the screen...can someone give me some assistance on this given that i havent used JMenuItems before...thx in advance...

    As carnickr said we really need the source from GUI to be able to help you more. Even without looking at GUI it's apparent that you're methodology is lacking. Depending on design and requirements I can think of many different ways you might need to handle menu item actions. I can't think of a single one that would involve comparing the text, which is what you're doing.
    First of all, adding a listener to a public field of a different class is inherently the product of a flawed design or bad design itself. Your code should look more like this:
    JMenuItem newDatabase = new JMenuItem("New Database");
    JMenuItem openDatabase = new JMenuItem("Open Database");
    JMenuItem closeDatabase = new JMenuItem("Close Database");
    ActionListener listener = new ActionListener() {
         public void actionPerformed(ActionEvent e) {
              if (e.getSource() == newDatabase) {
                   System.out.println("New database clicked");
              } else if (e.getSource() == openDatabase) {
                   System.out.println("Open database clicked");
              } else fi (e.getSource() == closeDatabase) {
                   System.out.println("Close database clicked");
    newDatabase.addActionListener(listener);
    openDatabase.addActionListener(listener);
    closeDatabase.addActionListener(listener);Obviously an anonymous class is not always appropriate. The important thing here is that you're comparing the reference to see if it's the same Object, not trying to compare the text. If you post GUI and a little more about what you're trying to do we can try and give you some pointers on how to design it better.

  • ITunes shows files adding--but then they're not in the library

    I have all the music from my old computer & old iTunes in a "Songs" folder on my desktop (courtesy of YamiPod). Per the instructions, I went to "Add Folder to Library" in iTunes and selected the Songs folder, and iTunes appeared to copy all the files into the library. But then, when the process was over, I discovered that many, many albums are nowhere to be found in the library, and some of the albums that are there don't even include all the songs. I've checked the Songs folder--and the iTunes Music folder in the My Music folder--and both folders include all the missing songs and albums. I've retried adding each of these folders to the library again, and again, I've watched iTunes goes through the whole process of adding them only to see yet again that when all is done, they're not in the library.
    Advice, anyone?
    Update: I take part of this back. My iTunes Music folder *does not* show all the missing albums. It shows some that the library doesn't (but that my original Songs folder does) but not all. I'm so confused. And when I try to just add individual song files, I encounter the same problem. I select the song(s) and select "Open," but though I receive no error messages, the song is not added to the library.
    Thanks,
    Stephanie
    Message was edited by: stephanie_e
    Message was edited by: stephanie_e

    I have exactly the same problem: much more songs on hard drive but not in the ligrary after I´ve imported them. Please help! Thanks Jan

  • Adding more then one file at a time...

    Well I was going to "add file to library" it would take me to where my downloads are then I would go through a bit of it selecting each song until I had about 100 then I would click select and add them to the library but when I got to iTunes their not there, so I tried adding 2 or 3 at a time and that worked, so I kept adding a bit more until I got to like 8 at a time after that if I tried adding them to the library it wouldn't work, so I was wondering if there is a setting that lets you add as many songs as you want to the library (I know you can go add folder, but I don't want the whole folder)

    I am also having this problem and it is a real pain in the ***. And it seems we are not alone, some one else suggested that you add files by selecting them in their windows folder and dragging and dropping them into your library pane on the left, especially with the "copy into itunes music folder" turned on. That works for me and it is about the same as adding them with the add file option. Cheers.

  • Adding actionlistener to JTextArea

    I'm trying to add an actionListener to my JTextArea, so that when the user hits 'enter', the action occurs.
    sort of like this:
    JTextArea.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ??????) {
    doSomethingToTextArea();
    but I'm having problems (e.g. how to make enter the action event, and some problem that says JTextArea can't use actionlisteners???)
    any ideas?
    thanks,
    n00bProgrammer

    Like serveral of the more complex swing gadgets the JTextarea uses model/view architecture. In this case the model behind the textarea is a Document, and the text change listener needs to be added to the Document, not the gadget itself. i.e. area.getModel().addDocumentListener();
    However JTextArea still inherits the methods of java.awt.Component including addKeyListener();

  • Adding ActionListener to JMenu

    Is it possible to add actionListener to Jmenu object
    for example File, Format, Exit these are Menus added in the menuBar not menu items
    on clicking the Exit the frame shuld get closed.
    For detecting the action clicked on the Menu shall i use actionListener?
    I have tried using MenuListener its getting invoked on selection
    I dont want it on selection , I want the frame to be displayed only on clicking that menu.

    Alas, whilst a JMenu is-a JMenuItem (the Composite Pattern at work), it doesn't function entriely like on,
    and registering ActionListeners with a JMenu doesn't work. Try MenuListener:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class ActionExample {
        public static void main(String[] args) {
            Action sample = new SampleAction();
            JMenu menu = new JMenu("Menu");
            menu.setMnemonic(KeyEvent.VK_M);
            menu.add(sample);
            menu.addMenuListener(new SampleMenuListener());
            JToolBar tb = new JToolBar();
            tb.add(sample);
            JTextField field = new JTextField(10);
            field.setAction(sample);
            JFrame f = new JFrame("ActionExample");
            JMenuBar mb = new JMenuBar();
            mb.add(menu);
            f.setJMenuBar(mb);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(tb, BorderLayout.NORTH);
            f.getContentPane().add(field, BorderLayout.SOUTH);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
    class SampleMenuListener implements MenuListener {
        public void menuSelected(MenuEvent e) {
            System.out.println("menuSelected");
        public void menuDeselected(MenuEvent e) {
            System.out.println("menuDeelected");
        public void menuCanceled(MenuEvent e) {
            System.out.println("menuCanceled");
    class SampleAction extends AbstractAction {
        public SampleAction() {
            super("Sample");
            putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("alt S"));
            putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_S));
            putValue(SHORT_DESCRIPTION, "Just a sample action");
        public void actionPerformed(ActionEvent evt) {
            System.out.println("sample...");
    }

  • Adding actionListener to JOptionPane

    how do you add an actionListener to a JOptionPane?
    im using JOptionPane.showMessageDialog, and i want an event to happen when the user clicks on the "Okay" button

    Can you do something like this instead? Use a showConfirmDialog(...) istead of the showMessageDialog(...), then use the return value to determine which button the user pushed?
    int retValue = JOptionPane.showConfirmDialog(parent, message, Title, JOptionPane.OK_CANCEL_OPTION );
    if (retValue == JOptionPane.OK_OPTION)
      doOkayEvent();
    else
      doNotOkayEvent();

  • Adding more then one horizontal row at a time

    How do I add multiple horizontal rows in Numbers?

    Hi Whixen,
    OK, different question, but still not quite clear.
    Do you want individual sums for each row, one sum for all of the cells in a block, or both of these? Here's a sample of each:
    The formulas in E2 and E5 are shown in column F. Both were created using the steps below.:
    E2:
    Enter =sum(
    Click on B2 and drag to include C2 and D2 in the selection.
    Release the mouse button, then press return or click the green Accept button on the function editor.
    Numbers will supply the capitalization and the closing parenthesis.
    E5:
    Enter =sum(
    Click on B2 and drag to D4 to add all intervening cells to the selection.
    Release the mouse button, then press return or click the green Accept button on the function editor.
    Numbers will supply the capitalization and the closing parenthesis.
    The formulas in E3 and E4 are copies of the one in E2. They were added by filling the formula in E2 down to E4:
    Select E2.
    Place the mouse pointer on the Fill Control handle,
    When the mouse pointer changed to a black + sign, click and drag the Fill handle down to include E3 and E4 in the selection rectangle.
    The Fill Handle is the small circle at the bottom right corner of the selection. You can see an example in E5 in the image above.
    Regards,
    Barry

  • Logic X randomly adding tracks then freezing?

    I am on ver 1.0.0.2 now. When I start a new project the program starts adding tracks on its own, when I attempt to cancel I find 6 more tracks, I cannot cancel the proceedure, after it creates about 16 tracks it freezes and I have to force quit. I hope I can someday start using the software I just purchased.
    GypsydoctorDonat

    I am on ver 1.0.0.2 now. When I start a new project the program starts adding tracks on its own, when I attempt to cancel I find 6 more tracks, I cannot cancel the proceedure, after it creates about 16 tracks it freezes and I have to force quit. I hope I can someday start using the software I just purchased.
    GypsydoctorDonat

  • Bought apps/music, then iphone broke

    I spent money on songs and apps on my iphone, purchased and cleared through itunes and then my phone crashed. Apple replaced it (they were wonderful) and I had it backed up but only until the day before I bought the music/apps I'm missing. Is there any way to get these things back to my phone directly from itunes???!

    Apps can be re-downloaded for free (http://support.apple.com/kb/HT2519). For music (and films and tv shows) you'll need to contact Apple and see if they'll let you do a re-download - try explaining your situation via the Lost or Missing Items link under the Music Purchases tab on this page http://www.apple.com/uk/support/itunes/

  • Adding actionListener to a button

    pls i want to create an applet with buttons rectangle,circle,and polygon and having a textArea.when someone clicks on the rectangle buttons it draws a rectangle,on the circle buttons it draws a circle and on the polygon button it draws a polygon.pls i have already created the user interface how do i add the actionListeners to the respective buttons and reggister them.

    Hi,
    try something like this:
    JButton jButton1 = new JButton("yourbutton");
    jButton1.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
                    someMethodThatDoesWhatYouNeed();
    });this is just a way to do it, there are other ways, just look around in the forum to discover them.
    JoY?TiCk

  • Adding ActionListener to JPanel or equivalent

    Hi all.
    I am trying to add an actionListener for mouse clicks on a JPanel, ideally using something like this:
    panel.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e)
              //do something
       });However, JPanel does not implement addActionListener, and as far as I can see the only components which do are buttons, radio buttons and the like.
    I tried extending JPanel to implement ActionListener, but had no success.
    Is there an easy way to monitor for mouse clicks on a JPanel or container? Bearing in mind there will be several of these containers at different locations, so using a global mouse listener would not be useful.
    Thanks

    Hi all.
    I am trying to add an actionListener for mouse clicks
    on a JPanel, ideally using something like this:
    panel.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e)
    //do something
    });owever, JPanel does not implement addActionListener,
    and as far as I can see the only components which do
    are buttons, radio buttons and the like.
    I tried extending JPanel to implement ActionListener,
    but had no success.
    Is there an easy way to monitor for mouse clicks on a
    JPanel or container? Use a MouseListener.
    Bearing in mind there will be
    several of these containers at different locations,
    so using a global mouse listener would not be
    useful.So add individual MouseListeners to each container.
    >
    Thanks

  • Trouble adding actionlistener to a panel in a tabbed pane

    Hi, please help me! this is the code i wrote, with the error given below:
    CODE:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class tabslisten extends JApplet
         JPanel RecPanel;
         GridBagLayout gbl;
         GridBagConstraints gbc;
         public void init ()
              FlowLayout flow;
              flow = new FlowLayout();     
              Container content = getContentPane();
              content.setLayout(new GridLayout());
              JTabbedPane tabpane = new JTabbedPane();
              content.add(tabpane, flow);
              RecPanel = new JPanel();
              recipientDetails();
              tabpane.addTab("Recipient Details", null, RecPanel, "Recipient Details");     
         public void recipientDetails()
              JLabel lblRecFName;
              JLabel lblRecLName;
              JTextField txtRecFName;
              JTextField txtRecLName;
              JButton btnValidate;
              gbl = new GridBagLayout();
              gbc = new GridBagConstraints();
              RecPanel.setLayout(gbl);
              lblRecFName = new JLabel("First Name");
              lblRecLName = new JLabel("Last Name");
              txtRecFName = new JTextField(8);
              txtRecLName = new JTextField(5);
              btnValidate= new JButton("Validate");
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.gridx = 1;
              gbc.gridy = 8;
              gbl.setConstraints(lblRecFName, gbc);
              RecPanel.add(lblRecFName);
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.gridx = 4;
              gbc.gridy = 8;
              gbl.setConstraints(txtRecFName, gbc);
              RecPanel.add(txtRecFName);
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.gridx = 1;
              gbc.gridy = 11;
              gbl.setConstraints(lblRecLName, gbc);
              RecPanel.add(lblRecLName);
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.gridx = 4;
              gbc.gridy = 11;
              gbl.setConstraints(txtRecLName, gbc);
              RecPanel.add(txtRecLName);
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.gridx = 4;
              gbc.gridy = 32;
              gbl.setConstraints(btnValidate, gbc);
              RecPanel.add(btnValidate);
              validateAction validateButton = new validateAction();
              btnValidate.addActionListener(validateButton);
         class validateAction implements ActionListener
              public void actionPerformed(ActionEvent evt)
                   Object obj = evt.getSource();
                   if (obj == btnValidate)
                        String fName = txtRecFName.getText();
                        String lName = txtRecLName.getText();
                        if (fName.length()==0)
                             getAppletContext().showStatus("First Name not entered.");
                             return;
                        else if (lName.length()==0)
                             getAppletContext().showStatus("Last Name not entered.");
                             return;
                        else
                             getAppletContext().showStatus("Successful.");
    ERROR:
    tabslisten.java:90: Undefined variable: btnValidate
    if (obj == btnValidate)
    ^
    tabslisten.java:92: Undefined variable or class name: txtRecFName
    String fName = txtRecFName.getText();
    ^
    tabslisten.java:93: Undefined variable or class name: txtRecLName
    String lName = txtRecLName.getText();
    ^
    3 errors
    Press any key to continue...
    I'm sure I'm doing something really stupid, but I don't know what! ANY and ALL help will be appreciated! Thanks!

    I copied your program and ran it. It works fine. Make sure the three variables you made class variables are only defined in one place. (ie you moved them not copied them). Other than that I have no ideas.
    With regards to naming conventions. My only comment is, take a look at the Java API.
    1) classes have uppercased words. Examples from your program - JTextField, Container, FlowLayout, GridLayout...
    2) methods and variable names don't upper case the first word - getContentPane(), setLayout()...
    Here is the program that works for me:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class FirstApplet extends JApplet
         JPanel RecPanel;
         GridBagLayout gbl;
         GridBagConstraints gbc;
         JTextField txtRecFName;
         JTextField txtRecLName;
         JButton btnValidate;
         public void init ()
              System.out.println("hello there");
              FlowLayout flow;
              flow = new FlowLayout();
              Container content = getContentPane();
              content.setLayout(new GridLayout());
              JTabbedPane tabpane = new JTabbedPane();
              content.add(tabpane, flow);
              RecPanel = new JPanel();
              recipientDetails();
              tabpane.addTab("Recipient Details", null, RecPanel, "Recipient Details");
         public void recipientDetails()
              JLabel lblRecFName;
              JLabel lblRecLName;
              gbl = new GridBagLayout();
              gbc = new GridBagConstraints();
              RecPanel.setLayout(gbl);
              lblRecFName = new JLabel("First Name");
              lblRecLName = new JLabel("Last Name");
              txtRecFName = new JTextField(8);
              txtRecLName = new JTextField(5);
              btnValidate= new JButton("Validate");
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.gridx = 1;
              gbc.gridy = 8;
              gbl.setConstraints(lblRecFName, gbc);
              RecPanel.add(lblRecFName);
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.gridx = 4;
              gbc.gridy = 8;
              gbl.setConstraints(txtRecFName, gbc);
              RecPanel.add(txtRecFName);
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.gridx = 1;
              gbc.gridy = 11;
              gbl.setConstraints(lblRecLName, gbc);
              RecPanel.add(lblRecLName);
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.gridx = 4;
              gbc.gridy = 11;
              gbl.setConstraints(txtRecLName, gbc);
              RecPanel.add(txtRecLName);
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.gridx = 4;
              gbc.gridy = 32;
              gbl.setConstraints(btnValidate, gbc);
              RecPanel.add(btnValidate);
              validateAction validateButton = new validateAction();
              btnValidate.addActionListener(validateButton);
         class validateAction implements ActionListener
              public void actionPerformed(ActionEvent evt)
                   Object obj = evt.getSource();
                   if (obj == btnValidate)
                        String fName = txtRecFName.getText();
                        String lName = txtRecLName.getText();
                        if (fName.length()==0)
                             getAppletContext().showStatus("First Name not entered.");
                             return;
                        else if (lName.length()==0)
                             getAppletContext().showStatus("Last Name not entered.");
                             return;
                        else
                             getAppletContext().showStatus("Successful.");

  • Adding string to gui installation

    Hi,
    I have installed a gui 710 server
    i want to enter a string for the snc field at the end of the installation
    Can anyone assist me with this?
    Regards,
    Moshe

    The SNC Name contained within the system entry can be set in the saplogon.ini file.  Is that the area you're talking about?

Maybe you are looking for