Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need something similar for setTitle but for a button? When b1 is clicked I need b2 to run.  RSS feed

 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ross, could you say a little more about what you are trying to accomplish? Saying, "I need b2 to run," is a bit confusing. Do you mean you want an ActionListener for b2 to run? I'm also confused by what you mean by "something similar to setTitle."

We can probably help you out, but we'll need more details from you first.
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want the user to click on b1 and everytime they click b1 I want b2 to run the student object and method for random picture generated.
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want my action performed to be my second button executed from outside the action performed method
 
Carey Brown
Bartender
Posts: 2980
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As someone mentioned in your other thread, using a JButton (b2) for the purpose of displaying an image will work but is probably inappropriate. Drawing the image on a JPanel would be better. (I don't have time to look this up right now, but their are a lot of samples online.)

Ok, assuming you keep it as a button for now, all you need is for the b1 action to invoke b2.setIcon(...) to display a new image.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created a button and I want it to do something in java but I am not sure what to do
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a popular tutorial that might help you, Ross. In particular, it has a section on Swing, which is a commonly chosen library for dealing with buttons (and a lot of other things) in Java programs.
 
Sandun Suranga Wijenayaka
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller had given a great link to you to learn about Swing Components step by step.
But if you need to just do something by using a JButton, simply you just have to create an actionListener or something like that(mouseListener etc).
Following is an example, which will print "You pressed Yes button" on the console/command prompt.
 
Carey Brown
Bartender
Posts: 2980
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You spawned off a few threads related (I assume) to the same project. In this thread you want to "do something" but don't elaborate on what that something is. In your other thread you imply that by "something", you mean update the image on a button. It would help us to keep all questions related to this project in the same thread and to avoid vague questions like "do something". Perhaps a moderator could merge these threads.
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here is what I finally ended up doing but I decided to change it around a little bit...

 
Carey Brown
Bartender
Posts: 2980
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks good. Does it work?
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But... What what if I wanted to put button1 in its own class like below and read it from myjpanel with an action performed?



i
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first one worked great!!


I am disregard the first set of code. Now I need to somehow work around the fact in my last posted code that "myJPanels" is a JPanel, which does not contain a setText() method.
I want to set the text of a JButton and I know I need to pass the button as a reference to my method. But not sure how to pull this off
 
Carey Brown
Bartender
Posts: 2980
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ross Gerard wrote:The first one worked great!!


I am disregard the first set of code. Now I need to somehow work around the fact in my last posted code that "myJPanels" is a JPanel, which does not contain a setText() method.
I want to set the text of a JButton and I know I need to pass the button as a reference to my method. But not sure how to pull this off

You can add your own setText() method to your myJPanel class, and have it do nothing more than call the setText() methods on other widget(s).

Something like



Please take the time to change all your class names to begin with an upper case letter.
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take the time to change all your class names to begin with an upper case letter.


Yes, you have already been asked to do this when you cross posted this question yesterday:

http://stackoverflow.com/questions/35530765/actionlistner-and-actionperformed-returning-action-outside-of-class/35531045#35531045
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help everyone
 
Ross Gerard
Ranch Hand
Posts: 53
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I renamed everything































 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!