Results 1 to 10 of 10
- 11-06-2011, 02:57 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
Help With Java Buttons & Action Listener In A GUI.
So for college I have to have this button that is set to a question mark, and then it is changed to a letter once it is clicked. I have the the button made, and set as a question mark, I have also implemented the action listener, but am not sure how to get the button to change once it's been clicked, can you help me? Please be clear and simple when explaining as I find programming really difficult. Also I have some code below, thank you.
//The button set to ? and it's action listener
JButton buttonOne = new JButton();
buttonOne.setText("?");
buttonOne.addActionListener(this);
//Action Event
public void actionPerformed(ActionEvent e)
{
}
Note I already have the panel created and then added the panel to the content pane.
- 11-06-2011, 03:02 PM #2
Re: Help With Java Buttons & Action Listener In A GUI.
Look at the API doc for the button class. It will have methods you can use to change it.how to get the button to change
You are currently using one of the button class's methods to set the text. Have you tried using that in the listener code?
-
Re: Help With Java Buttons & Action Listener In A GUI.
The ActionEvent object that is passed as a parameter to the actionPerformed method has a method, getSource(), that will return the source of the action, in this case your JButton of interest. You will have to cast the object returned to JButton though, but after that it will represent your JButton in all its glory.
e.g.,
For this to work, you'd better be sure not to add this ActionListener to anything that's not a JButton!Java Code:public void actionPerformed(ActionEvent e){ JButton myButton = (JButton) e.getSource(); // now I can call methods on myButton }Last edited by Fubarable; 11-06-2011 at 03:42 PM.
- 11-06-2011, 05:41 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 6
- Rep Power
- 0
Re: Help With Java Buttons & Action Listener In A GUI.
could you please tell my how to do this code in java
- 11-06-2011, 05:44 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 6
- Rep Power
- 0
Re: Help With Java Buttons & Action Listener In A GUI.
two input dialog boxes (from the JOptionPane class). If the user closes either dialog box or presses ‘Cancel’, then the program should terminate. Otherwise, the first dialog box should return a String value which indicates the name of the customer. If this string is empty, again the program should terminate. Otherwise the customer’s name should appear in the title bar of the main GUI window
- 11-06-2011, 05:55 PM #6
Re: Help With Java Buttons & Action Listener In A GUI.
Have you looked at any of the code examples on this forum that use the JOptionPane class?
There should be some that will show you how to code what you want to do.
Also the API doc for the JOptionPane class has some examples.
- 11-06-2011, 09:05 PM #7
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
Re: Help With Java Buttons & Action Listener In A GUI.
Thanks everyone, especially Fubarable, helped alot with this part of the project :) I might have another question about using an array as I am now having difficulty doing that! :/
- 11-06-2011, 09:07 PM #8
Re: Help With Java Buttons & Action Listener In A GUI.
For new questions, start a new thread.
-
Re: Help With Java Buttons & Action Listener In A GUI.
coolgirl: please don't hijack someone else's thread with your unrelated question. You'll want to go to the New to Java forum, click on the "Post New Thread" button and ask your question there.
- 11-07-2011, 10:37 AM #10
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Java Addition Program Help with Action Listener
By Syanara in forum New To JavaReplies: 21Last Post: 05-12-2011, 07:03 PM -
Simple Action/Listener Help GUI
By aanders5 in forum New To JavaReplies: 24Last Post: 10-18-2010, 06:43 PM -
Action Listener
By greatmajestics in forum AWT / SwingReplies: 8Last Post: 03-25-2010, 05:39 PM -
Action Listener? how to use this?
By jeffrey in forum New To JavaReplies: 2Last Post: 10-12-2009, 08:51 AM -
action listener on jcombobox
By chkm8 in forum New To JavaReplies: 2Last Post: 02-05-2009, 10:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks