File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JButton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JButton" Watch "JButton" New topic
Author

JButton

Martin Vietor
Ranch Hand

Joined: Oct 31, 2008
Posts: 40
Alright, I have a simple one again. Actually, I'm not even sure if I can phrase it understandably, but I'll try.

I have an ArrayList of a class that contains several variables. The class looks like this, if that helps. It's a list of plants with different attributes:



What I wnat is a list of the plant names in a frame (got that) with a button next to each saying "Details" (got that) and a ToolTip that says "Display details about "+plant.name (got that, too).

Now, when I try button.addActionListener(this), I'not allowed to in a static method. When I move it to it's own little paragraph (private JButton createButton or somesuch) I can't add my plant liek this



The Button is supposed to open a frame titled plant.name, but my method createButton doesn't know the name of the plant - am I making any sense at all? I feel like I'm lacking some basic understanding here. Can I change the name and content of button and the frame created upon triggering after it is created? If so with what command?

If I'm in way over my head just tell me, I'll change my initial plans. But it would be awfully neat, and it's got to be possible to create a flexible amount of buttons in a contentpane depending on how many items are in my list, right? I can't believe Java can't do that.

Thankful as always, Martin


The problem people have in trying to create something foolproof is that they generally underestimate the inginuity of fools.
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

It would be helpful if you could post the code where you are creating your JButtons and your ActionListeners so we could have a better understanding of what you have. And post the errors you are getting (and if they are compile errors or runtime errors).


it's got to be possible to create a flexible amount of buttons in a content pane depending on how many items are in my list, right?

Yes this is possible.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Sounds like a Swing-related topic. Moving.
Martin Vietor
Ranch Hand

Joined: Oct 31, 2008
Posts: 40
Thanks for moving me, thoght it was an approprate question for the newbie section.

Here's the code. Eclipse is telling me in addActionListener(this) the "this" cannot be used in a static context.



The Error I'm getting upon compilation is



where 48 is the line with the addActionListener and 30 is the line where myOutputMethod is called upon. If I'm using the wrong technical terms correct me, I need to learn them in English anyways.

Thanks a bunch, M
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Are you aware of the difference between static and non-static?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Martin Vietor
Ranch Hand

Joined: Oct 31, 2008
Posts: 40
I hope so, like I said - I may be missing some basic understanding here. I've only been using java for a week, a lot of the terms are a bit sketchy. And in a foreign language. So bear with me, if I use simple-sounding terms.

But basically static methods belong to the class and only exist once, while "this" refers to an object (like a button) that can exist in multiple copies.

Which does explain why it doesn't work the way I got it set up right now. If I write a method createButton that is (obviously) not static, I can't "hand over" (<- simlpe sounding term) my variable plant to that method as I would in a static (createButton(plant)). Which means I will reproduce EXACTLY the same button, but mine are supposed to differ, depending on the values in plant. I'll get the same problem later on, when actionPerformed is supposed to open a frame with plant.name as title bar.

This probably isn't even a real problem, I just don't know enough code yet. Frustrating to learn a new language, isn't it.

M
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Originally posted by Martin Vietor:
I hope so, like I said - I may be missing some basic understanding here. I've only been using java for a week, a lot of the terms are a bit sketchy. And in a foreign language. So bear with me, if I use simple-sounding terms.

But basically static methods belong to the class and only exist once, while "this" refers to an object (like a button) that can exist in multiple copies.

Which does explain why it doesn't work the way I got it set up right now. If I write a method createButton that is (obviously) not static, I can't "hand over" (<- simlpe sounding term) my variable plant to that method as I would in a static (createButton(plant)). Which means I will reproduce EXACTLY the same button, but mine are supposed to differ, depending on the values in plant. I'll get the same problem later on, when actionPerformed is supposed to open a frame with plant.name as title bar.

This probably isn't even a real problem, I just don't know enough code yet. Frustrating to learn a new language, isn't it.

M

When I first learned to program in Java, the static vs non-static thing threw me for a bit (and I didn't have the verbal language barrier since English is my first language). And it is a subject that tends to throw a lot of new developers (and seasoned developers) as well. So hang in there.

You're basic understanding of static and non-static is correct. A static member belongs to the class. So all instances -- i.e. objects created from the class -- as well as the non-instantiated class itself share the member. A non-static member belongs to a specific instance -- i.e. object -- of the class.

The main method of a class has to be static since no instance of the object exist when the application starts. And a static member, such as a method, cannot reference a non-static member. For example, look a the code below:


The main method is static. So it does not belong to a specific instance of ExampleClass. But the 'creationDate' variable is non-static. So it belongs to a specific instance. So I cannot reference it since the compiler does not know what instance I am talking about.

When this happens, and the non-static versus static error message appears, the first inclination of new developers is to just start declaring everything static. All the variables, all the methods, etc. This works ok for small simple programs. But it quickly leads to problems.

It's important to eventually learn about and become more comfortable with static vs non-static concepts. In my humble opinion, the book Head First Java is simply the best book at explaining this concept. Bar none. Many others here at JavaRanch would agree with me. I would recommend you give it a look. (Be sure to get the second edition.) It is a very different book in terms of how it presents things. It may seem a little silly at first. But its methods are based on solid learning theory, and you will lean a lot. You can learn more about it here: http://www.headfirstlabs.com/books/hfjava/

In the meantime, I would recommend that you remove the static declarations from your methods and do the following. Keep your main method very simple. Simply have it create an instance of the class and then call some startup method like 'go' or 'execute'. Then put all your work in the 'go' method and methods it calls. These should be non static since you are now working with an instance of the class:



This will get you headed in the right direction. Again, you should still work on understanding static vs non-static members. But in the meantime, this will get you headed in the right direction.

I'll take look at the code you posted and see if there is any other pointers that might help.
[ November 04, 2008: Message edited by: Mark Vedder ]
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Originally posted by Martin Vietor:

If I write a method createButton that is (obviously) not static, I can't "hand over" (<- simlpe sounding term) my variable plant to that method as I would in a static (createButton(plant)). Which means I will reproduce EXACTLY the same button, but mine are supposed to differ, depending on the values in plant.
M


The first statement here isn't necessarily true. And this is where static vs non-static van get a bit confusing. Let's look at this code:



In this code, even though the createButton method is static, it is returning a new (and thus different) button each time it is called. Writing the method this way as a static method works okay since it is not referencing any instance variables (i.e. non-static members) of the 'MyPanel' instance. So it works okay. If I needed to access any such variables, then this method would have to be non-static. What is shown here however is a rather unusual way of doing things and is just done to make a point. If I was writing this code for real, I would make the createButton method non-static.

I hope that helps some. Again, Head First Java explains this concept very well. Much better than I or anyone else could in a quick forum posting.

Give your code a try with the suggestion I made above and let us know how it goes. If you have any issues, post them and we'll get you through them.
Martin Vietor
Ranch Hand

Joined: Oct 31, 2008
Posts: 40
Man, I was almost hoping for a condescending answer. Ever since graduation I was hoping for a chance to say something like

Damn it, Mark, I'm a doctor, not a java programmer!

in a setting of fellow nerds where I would be understood Ah, a man can dream.

As to your answer, thanks a bunch, I see the problem now: I didn't know I could create an instance of my class within itself (coming from Pascal, where that is not possible). That's why all of the methods had to be static. Now they're all non-static, and we'll see how that works out for me.

Also bought the book.

Thank, M
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JButton
 
Similar Threads
what affects addActionListener(this)?
EJB 3.0 remove Entity
Having problems with Window and Frame
how to add JButtons to an array
please help .. please