Results 1 to 4 of 4
  1. #1
    chiapa is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default JButtonActionPerformed problem

    Hi everyone,

    I've got a JPanel with menu bar and menu items in it. In the design view, I double clicked on one of the items and it led me to code of that item (ActionPerformed). I edited it as I wanted, everything ok.

    But now, I need more options. But when I double click the other options it leads me to the code of action performed, but that code is placed inside the action performed of the other one, causing errors all over.

    PS: I already have two menu items working, I achieved it by doing the double click on them, but now I need other options and this problem pops when I try to do the same.

    Why does this happen? How can it be solved? Can someone help please?

    Thanks

    Chiapa

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: JButtonActionPerformed problem

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    chiapa is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default Re: JButtonActionPerformed problem

    Quote Originally Posted by DarrylBurke View Post
    So what?
    I'm just trying to get some help. No one seems to know what this problem is as no one answered yet.

    It seems an unknown issue.

  4. #4
    Tha Boss is offline Member
    Join Date
    Jun 2012
    Location
    ON, Canada.
    Posts
    25
    Rep Power
    0

    Default Re: JButtonActionPerformed problem

    C'mon Darryl.. instead of replying just to point that out, you could put forward an answer...

    To your question bro... since you're using swing, why not just use an option dialog? and make the buttons an array object like so:

    Java Code:
    String[] options = {"Button 1", "Button 2", "Button 3"};
                int userChoice = JOptionPane.showOptionDialog(null,
                        "Would you like button 1, 2, or 3?", //Dialog message.
                        "Make your Choice", //Title of the dialog.
                        JOptionPane.DEFAULT_OPTION,
                        JOptionPane.QUESTION_MESSAGE, //Dialog type.
                        null, //Icon.
                        options, //The titles of the buttons.
                        options[0]); //Default choice.
    
    //Now all you would need to do is make if statements...
    
    if (userChoice == 0) {
    JOptionPane.showMessageDialog(null, "Button 1 to do code.. Blah.");
    } else if (userChoice == 1) {
    JOptionPane.showMessageDialog(null, "Button 2 to do code.. Blah Blah.");
    } else {
    JOptionPane.showMessageDialog(null, "Button 3 to do code.. Blah Blah Blah.");
    }
    Notice when a button is clicked, it returns an int (the array index of the button). Makes it that much easier to determine the users choice. I think this route is a much simpler alternative to the whole action listeners thing. But I do encourage you to post a chunk of the code you're having trouble with and we can work things out with that. But yeah.. that's it.
    - CHEERS!
    Last edited by Tha Boss; 06-23-2012 at 05:40 PM.

    E.Hughes
    "I'm sometimes hard to please... because I'm only satisfied with the very best." - Fernand Point.

Tags for this Thread

Posting Permissions

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