• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Attaching listeners to dialog box buttons

 
Brendan Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am very new to swing and am having some trouble trying to attach listeners to buttons in a dialog box. My current assignment question is:

Create a Java program that employs a MouseListener in a Dialog component.

My goal is to have the buttons' text display the current events that are happeneing (e.g. "Button 1" will change to "mouse pointer in 'Button 1'" or "'Button 1' has been clicked" when the different events are happening.

Here is my code so far, the problem is trying to assign each button an event. I am getting compiler errors when I try to add events to the buttons array:



Again I am very new to Java so I'm sure this has an easy solution. There is no textbook for this course and the study material is the docs.Oracle tutorials which are hard for me to understand.

Thanks.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have an array of type Object, variable name buttons. Since Object doesn't have a method addMouseListener(...) the compiler will complain.

Moreover, even when you sort that out and create an array of JButton, the MouseListener will be added only after the dialog is dismissed. You will need to add the mouse listener before you show the dialog.

For your future reference, when seeking help with an error it always helps to post the entire error text without paraphrasing.
 
Brendan Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I have updated so far:



And the test class:



Should I change the null on line 14 to something else? From what I understand the first parameter can be null and a default frame will be used. Pretty lost right now at this point.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Should I change the null on line 14 to something else? From what I understand the first parameter can be null

the null pointer exception has nothing to do with that first parameter (or the later one).

the NPE is from this
JButton[] Buttons = new JButton[3];

that line does not create any buttons.
somewhere you need
Buttons[elementNumber] = new JButton("ButtonText");

fix that and it will run without error, but none of the listeners will work.
Darryl has already told you why, so you need to re-read his reply,
and amend your code accordingly.

when you get the listeners working, you'll see the running code is awful,
and you'll understand why when you see it.

Either shorten the text that each button changes to, or give each button
a much larger preferredSize (width only), so the buttons are much wider
when the program starts.

also, mouseClicked() is always a poor choice as it doesn't always fire,
change to mousePressed().
 
Brendan Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the feedback everyone. Here is my final working code:



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic