• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JButton

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like a Swing-related topic. Moving.
 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you aware of the difference between static and non-static?
 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic