Results 1 to 3 of 3
  1. #1
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default trouble with actionPerformed

    I'm still working on my notepad program and I've just been working on my "Open" button, which opens up a JFileChooser. I looked at this tutorial online and it said that I could write it into the actionPerformed method of my class like so (this code isn't from the tutorial, this is what I wrote based on what I learned from it... it is incomplete because right now I'm just focusing on getting the actual window to open):

    Java Code:
     public void actionPerformed(ActionEvent e) {
          
         if(e.getSource() == bOpen) {
          JFileChooser fc = new JFileChooser();
          int returnVal = fc.showOpenDialog(null);
          fc.requestFocusInWindow();
          fc.grabFocus();
          if(returnVal == JFileChooser.APPROVE_OPTION) {
            try {
              File f = fc.getSelectedFile();
              Scanner scan = new Scanner(f);
              String newText;
            }
            catch(FileNotFoundException exception) {
            }
          }
        }
      }
    However, when I use this code and click the open button, nothing happens. Keep in mind that I didn't make an inner class or anything like that, it's just the actionPerformed method of the overall class (Which I made implement ActionListener just for this).

    The weird thing is though, when I use it as an inner class and add it as an actionListener to the button itself, it works just fine. This is the successful code I used:


    Java Code:
    bOpen.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent event) {
            JFileChooser fc = new JFileChooser();
            int returnVal = fc.showOpenDialog(null);
            fc.requestFocusInWindow();
            fc.grabFocus();
          if(returnVal == JFileChooser.APPROVE_OPTION) {
            try {
              File f = fc.getSelectedFile();
              Scanner scan = new Scanner(f);
              String newText;
            }
            catch(FileNotFoundException exception) {
            }
          }
        }
        });
    As you can see, it's the exact same thing as the above, only I attached it to the button instead of using the if statement with the e.getSource().

    If anybody could explain why the second way worked and the first didn't, I'd really appreciate it.
    Last edited by diggitydoggz; 12-25-2008 at 09:04 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Making an actionPerformed is fine, but did you

    1) have your class implement the ActionListener interface, and
    2) add "this" as the button's ActionListener, i.e.,
    Java Code:
    myButton.addActionListener(this);
    Having said that, I am definitely of the camp that believes that using your main class as the actionlistener object is a bad idea. This can lead to creation of a "switchboard" actionPerformed method where too many buttons are relying on a single actionPerformed. Much better is to either create an anonymous inner class as you've done above, or use a private inner class or if complex enough a standalone separate ActionListener class.

  3. #3
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default

    Ooooh ok. No wonder. I didn't add an ActionListener to the button... silly me... thanks for the help as always.

Similar Threads

  1. returning String from actionPerformed
    By hardcore_teddy in forum New To Java
    Replies: 2
    Last Post: 05-17-2008, 06:32 AM
  2. Issue with Buttons and ActionPerformed
    By Deathmonger in forum Advanced Java
    Replies: 1
    Last Post: 04-17-2008, 09:47 AM
  3. Help with actionPerformed Statements
    By wco5002 in forum New To Java
    Replies: 8
    Last Post: 03-26-2008, 05:02 AM
  4. actionPerformed problem
    By tomitzel in forum New To Java
    Replies: 1
    Last Post: 01-08-2008, 07:10 PM
  5. Problems with jButton ActionPerformed
    By susan in forum AWT / Swing
    Replies: 3
    Last Post: 08-07-2007, 05:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •