permaculture playing cards*
The moose likes Swing / AWT / SWT and the fly likes Attaching listeners to dialog box buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Attaching listeners to dialog box buttons" Watch "Attaching listeners to dialog box buttons" New topic
Author

Attaching listeners to dialog box buttons

Brendan Thompson
Greenhorn

Joined: Apr 27, 2012
Posts: 21
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

Joined: May 03, 2008
Posts: 4529
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Brendan Thompson
Greenhorn

Joined: Apr 27, 2012
Posts: 21
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Apr 27, 2012
Posts: 21
Thanks for all the feedback everyone. Here is my final working code:



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Attaching listeners to dialog box buttons