NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

JButtonActionPerformed problem

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users
View previous topic :: View next topic  
Author Message
chiapa



Joined: 22 Jun 2012
Posts: 3

PostPosted: Fri Jun 22, 2012 4:19 pm    Post subject: JButtonActionPerformed problem Reply with quote

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 happens when I try to do the same.

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

Thanks

Chiapa
Back to top
SearchEngine27



Joined: 21 Jun 2012
Posts: 66

PostPosted: Fri Jun 22, 2012 7:11 pm    Post subject: Reply with quote

I have to assume what you're doing is that you only have one ActionListener.

So you have two options:
1 - create a separate action listener for each component that you want functionality for (so they all have their own ActionPerformed(ActionEvent evt) method)
2 - use the same ActionListener for all components (which is what it sounds like you're doing now), but have a chain of if-else statements using evt.getActionCommand() or evt.getSource() to compare it either to the components themselves, or the command the component uses (for JButtons, its usually the label unless u explicitly set it)

For readability, I suggest using anonymous ActionListener classes for each component you use rather than use a single class for all components, and iterating through a chain of if statements to find the right execution.

Code:

JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent evt){
     //functionality set 1
  }
});
JButton acceptlButton = new JButton("Accept");
cancelButton.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent evt){
     //functionality set 2
  }
});
Back to top
chiapa



Joined: 22 Jun 2012
Posts: 3

PostPosted: Fri Jun 22, 2012 8:21 pm    Post subject: Reply with quote

Isn't that generated code? I haven't created any listener.

Where can I find that?
Back to top
SearchEngine27



Joined: 21 Jun 2012
Posts: 66

PostPosted: Tue Jun 26, 2012 10:57 am    Post subject: Reply with quote

generated code? the only thing that generated that code was me

I dont know how youre creating functionality on a button without a listener. if you want to know how i did that, then look up the api for ActionListener, or tutorials for ActionListener.

How about for starters you post your code you have now, so we know where youre headed, and so we can tell you why what you have right now doesnt do what you want it to do. I think once you understand why your code doesn't work, I might be able to explain it a bit better.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo