Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Exclamation 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.

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

    Default Re: Help With Java Buttons & Action Listener In A GUI.

    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?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.,

    Java 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!
    Last edited by Fubarable; 11-06-2011 at 03:42 PM.

  4. #4
    coolgirl is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default Re: Help With Java Buttons & Action Listener In A GUI.

    could you please tell my how to do this code in java

  5. #5
    coolgirl is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default 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

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

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

  7. #7
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

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

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Help With Java Buttons & Action Listener In A GUI.

    For new questions, start a new thread.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help With Java Buttons & Action Listener In A GUI.

    Quote Originally Posted by coolgirl View Post
    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.

  10. #10
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

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

Similar Threads

  1. Java Addition Program Help with Action Listener
    By Syanara in forum New To Java
    Replies: 21
    Last Post: 05-12-2011, 07:03 PM
  2. Simple Action/Listener Help GUI
    By aanders5 in forum New To Java
    Replies: 24
    Last Post: 10-18-2010, 06:43 PM
  3. Action Listener
    By greatmajestics in forum AWT / Swing
    Replies: 8
    Last Post: 03-25-2010, 05:39 PM
  4. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 08:51 AM
  5. action listener on jcombobox
    By chkm8 in forum New To Java
    Replies: 2
    Last Post: 02-05-2009, 10:14 AM

Tags for this Thread

Posting Permissions

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