Description Resource Path Location Type
The serializable class Mainp does not declare a static final serialVersionUID field of type long Mainp.java ‪/Gui-test/src‬ line 13 Java Problem
The import java.awt.event.ActionEvent is never used Mainp.java ‪/Gui-test/src‬ line 8 Java Problem
The import java.awt.event.ActionListener is never used Mainp.java ‪/Gui-test/src‬ line 7 Java Problem

can you show me how to add the actionlistener to the button?

i can see the button but i dont know how to make the button start "creation()"

it still says- Error: Main method is not static in class Mainp, please define the main method as:
public static void main(String[] args)

09-06-2013, 01:42 PM

PhHein

Re: problems with jbutton/actionlistener

No. Seriously, get back to the tutorials, your books, whatever you use and read up on the language basics. You're making first week beginner's mistakes here.

Code:

public void run()
{
Mainp mp = new Mainp();
mp.creation();
}

09-06-2013, 02:01 PM

gimbal2

Re: problems with jbutton/actionlistener

I call that the "forum thread trap". You have a thread running in a forum and then it becomes too big an attraction to just ask every little thing there in stead of doing the right thing and spending time figuring stuff out yourself. Using Google.

09-06-2013, 04:16 PM

mstabosz

Re: problems with jbutton/actionlistener

Quote:

Originally Posted by gimbal2

I call that the "forum thread trap". You have a thread running in a forum and then it becomes too big an attraction to just ask every little thing there in stead of doing the right thing and spending time figuring stuff out yourself. Using Google.

I like that. Now I'm no expert on Java. I'm just a student myself, but I pick stuff up pretty quickly and am usually pretty slow to ask questions. Even then I only do so after a lot of hesitation and effort.

Anyway, to the original poster: I think you're missing something fundamental about listeners.

1. You need to define a separate class to make a listener work. You call this class whatever you want. I usually go with something related to the type of GUI element it's supposed to handle. HandleButtonClicks for buttons, HandleCheckBoxes for check boxes, HandleSliders for slider bars etc.,

2. The class must implement a listener interface, using the implements keyword. There are a few different listener interfaces, ItemListener and ActionListener are the ones I know off the top of my head. I think there are other ones for mouse and keyboard events. You'll need to do your own research to figure out what you need for a given situation. That's what I think is one of the keys to programming: you don't need to know the particulars off the top of your head, but you need to know how to find them.

3. The listener class MUST use the function public void actionPerformed(ActionEvent e). This function is invoked whenever you interact with the item to which you register the listener (i.e. click a button, check a check box, move a slider bar etc.,) You can add additional functions if you like. Just like your primary class. I usually keep the body of my actionPerformed function simple. It usually contains if or switch statements and function calls and not much else. I like to pass off the heavy load to special functions.

4. You have to invoke the addActionListener method on each GUI element you want tied to this listener. As the documentation for addActionListener states, you have to pass an instance of a listener object. You can do this with an explicitly named instance or pass it anonymously. So like if I had a "Submit" button named jbtSubmit, and a listener class named "HandleButtonClicks", I could do it explicitly:

Then, any time you click the submit button, the statements inside the "actionPerformed" method of the "HandleButtonClicks" class are executed.

There are a lot of problems with your code, but I wanted to give you a broad breakdown of how event driven programming works.

09-06-2013, 04:19 PM

mstabosz

Re: problems with jbutton/actionlistener

Quote:

Originally Posted by PhHein

No. Seriously, get back to the tutorials, your books, whatever you use and read up on the language basics. You're making first week beginner's mistakes here.

Code:

public void run()
{
Mainp mp = new Mainp();
mp.creation();
}

Isn't public void run() the mandatory function you use when invoking the Runnable interface for a Task class when Multithreading? I've only read on Multithreading; didn't get around to writing anything with it yet. I just ask because I found the original poster's choice of function name incredibly odd.