Results 1 to 4 of 4
  1. #1
    cyentw is offline Member
    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Default 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!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,259
    Rep Power
    25

    Default Re: JButton not clicking help!

    How can I make an action be preformed when a button is clicked
    First you need to make sure the actionPerformed method is being called. Add a println() first thing in the method and print out the e object that is passed to the method. It's useful to see what is printed.

    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.

  3. #3
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default 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 ...);

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

Similar Threads

  1. Replies: 1
    Last Post: 01-15-2013, 06:44 PM
  2. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 07:35 AM
  3. Replies: 1
    Last Post: 08-18-2011, 07:47 PM
  4. changing JButton image upon clicking
    By Logical in forum AWT / Swing
    Replies: 2
    Last Post: 12-21-2010, 02:53 AM
  5. right clicking JTable row
    By Stephen Douglas in forum New To Java
    Replies: 2
    Last Post: 06-27-2010, 11:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •