Results 1 to 4 of 4
Thread: JButton not clicking help!
- 01-17-2013, 04:15 AM #1
Member
- Join Date
- Jan 2013
- Posts
- 14
- Rep Power
- 0
JButton not clicking help!
Sorry for having multiple problems posts, but a new problem came up.
How can I make an action be preformed when a button is clicked? Here is the code I tried:
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == generateitemfile)
{
String fileName = "out.java";
PrintWriter outputStream;
try {
outputStream = new PrintWriter(fileName);
outputStream.println("import java.util.Random;");
outputStream.println("import java.util.Map;");
outputStream.flush();
makeitem.setVisible(false);
makeitem.dispose();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
}
This is what should happen when the button is clicked. (Writes 'testing' to out.java then closes) but nothing happens.
Help!
- 01-17-2013, 04:22 AM #2
Re: JButton not clicking help!
How can I make an action be preformed when a button is clicked
Then print out the value of this: (e.getSource() == generateitemfile)
which needs to be true for the code to be executed.If you don't understand my response, don't ignore it, ask a question.
- 01-17-2013, 04:27 AM #3
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
Re: JButton not clicking help!
You need to add the action listener to the JButton object.
Java Code:JButton button = new JButton("Hello"); button.addActionListener(... your action listener goes here ...);
Website: Learn Java by Examples
- 01-17-2013, 04:52 AM #4
Re: JButton not clicking help!
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads
-
How set JButton icon Relative to the JButton size???
By farshad in forum AWT / SwingReplies: 1Last Post: 01-15-2013, 07:44 PM -
ActionListener for JButton after changing Button to JButton
By ravi.joshi53 in forum Java AppletsReplies: 2Last Post: 10-07-2011, 08:35 AM -
how to operate a graph step by step clicking the jbutton?
By cibip in forum New To JavaReplies: 1Last Post: 08-18-2011, 08:47 PM -
changing JButton image upon clicking
By Logical in forum AWT / SwingReplies: 2Last Post: 12-21-2010, 03:53 AM -
right clicking JTable row
By Stephen Douglas in forum New To JavaReplies: 2Last Post: 06-28-2010, 12:41 AM
Bookmarks