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.
Re: Help With Java Buttons & Action Listener In A GUI.
Quote:
how to get the button to change
Look at the API doc for the button class. It will have methods you can use to change it.
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.,
Code:
public void actionPerformed(ActionEvent e){
JButton myButton = (JButton) e.getSource();
// now I can call methods on myButton
}
For this to work, you'd better be sure not to add this ActionListener to anything that's not a JButton!
Re: Help With Java Buttons & Action Listener In A GUI.
could you please tell my how to do this code in java
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
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.
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! :/
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.
Quote:
Originally Posted by
coolgirl
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
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.
Re: Help With Java Buttons & Action Listener In A GUI.
Thanks again, I got the array part sorted, have a different question though so will start a new thread.