Results 1 to 9 of 9
  1. #1
    ashton is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default Help with JButtons...

    Hi guys... can I ask a question?

    Why is it that a JButton only works once??

    I have a problem with my program... It works this way:

    Imagine this. In front of you, there is a small GUI that has a textfield for the user to input his name.. Now, you type in your name and then, BOOOM!!! your picture pops up on the corner... (well, ofcourse your picture is built-in into the program..). BUT BUT bUT... Your friend wants to see his face, he takes your seat and types his name in the textfield, but when he clicked on the button, nothing happens...

    This is my sample statement:
    Java Code:
            if(name.equals("john")){
                jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/swingPack/joham.jpg")));
            }else if(name.equals("Smiley")){
                jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/swingPack/smiley.jpg")));
            }
          }
    is there a way to keep the button working continuously?? coz it only works once.. do i need to have a do{}while(); or something like that? Do you still need to have a looping statement around the button's "ActionPerformed" code??

    Thank you in advance...

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Ok, you have if-else condition in the above. What happen if you call it with different parameters several times?

  3. #3
    ashton is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default woah! you got me there!

    woah! you got me there! :D

    uhm.. honestly, the term parameter is not that clear to me... please clarify it.. i really need your help.. please define it in a 'newbie term'... tnx

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  5. #5
    Waqas is offline Member
    Join Date
    Jan 2009
    Posts
    10
    Rep Power
    0

    Default

    send me the action of the Jbutton

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Waqas View Post
    send me the action of the Jbutton
    Just you are going to do others assignments once without guiding him to think and have a try something. ;)

  7. #7
    ashton is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default well...

    here's the action:

    (should've placed the first lines.. haha:D sorry 'bout that..)

    Java Code:
     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    
    
           String name = nameField.getText();
           String age = ageField.getText();
    
            if(name.equals("john")){
                jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/swingPack/joham.jpg")));
            
                }else if(name.equals("Smiley")){
                jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/swingPack/smiley.jpg")));
            }

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    As the other thread explain you have to think about a way to repaint the component with new stuff you want to have on that.

  9. #9
    ashton is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default what a stupid move!!!!!!!

    :eek:]
    don't worry guys! I'm ok! i'm ok! tnx for the replies though...

    I'm so stupid!!! take a close look at the ...}else{... line, i typed in
    Smiley... with a capital 'S'!! i was just typing 'smiley' all the time... That letter 's' is the one... Sorry for this guyz.. that's why smiley's face doesn't show up.. stupid me...:D

Similar Threads

  1. JButtons
    By jadaleus in forum Advanced Java
    Replies: 4
    Last Post: 10-17-2008, 02:49 AM
  2. 2D Array of JButtons
    By stevemcc in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2008, 11:42 PM
  3. JButtons
    By fgasimzade in forum SWT / JFace
    Replies: 1
    Last Post: 12-25-2007, 05:39 AM
  4. how to change the appearance of jbuttons
    By katie in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 10:26 PM
  5. disabling JButtons after win in TicTacToe
    By noisepoet in forum New To Java
    Replies: 1
    Last Post: 05-18-2007, 11:01 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
  •