Results 1 to 20 of 20
  1. #1
    Frank_21 is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Question changing int with jbutton

    Hey I'm currently working on my first java application and there's one problem I can't find the answer to. So i have a int displayed on the screen and i have a button that will add to the int. how do i make the button add 2 to the int? Any help would be great!

    - Frank

  2. #2
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: changing int with jbutton

    You would have to change the method associate with the button.

  3. #3
    Frank_21 is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: changing int with jbutton

    can you be more specific maybe give an example?

  4. #4
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: changing int with jbutton

    Sure. The JButton would be associate with a method. Within that method you would probably see something like :

    Java Code:
    {
    displayNum= displayNum+1;
    
    }
    All you would want to do is change that 1 to a 2 within the code. To be much more specific I would have to see the actual code.

  5. #5
    Frank_21 is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: changing int with jbutton

    okay thanks and I'm assuming displayNum is the variable name?

  6. #6
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: changing int with jbutton

    Yes.

  7. #7
    Frank_21 is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: changing int with jbutton

    alrigth so I tried that but i had an error that said to change the type to final which i did, but now its saying: the final local variable score1 cannot be assigned, since it is defined in an enclosing type

  8. #8
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: changing int with jbutton

    Can you put up a the snippet of code between ? I think I know the issue, but I don't want to lead you down the wrong path.

  9. #9
    Frank_21 is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: changing int with jbutton

    final int score2 = 0;

    JButton l3 = new JButton("2-Pointer");
    c.gridx = 4;
    c.gridy = 1;
    panel.add(l3, c);
    l3.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
    score1= score1+2;
    }
    });


    all in the same method if that matters

  10. #10
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: changing int with jbutton

    You are trying to access a variable within an inner class. What you need to do is move the variable int score2 out of the method and make it a global variable. It doesn't need to be final. Just a global variable.

  11. #11
    Frank_21 is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: changing int with jbutton

    thanks a ton that worked!

  12. #12
    Frank_21 is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: changing int with jbutton

    alright last question this time, i cannot put the variable in the parenthesis for the jlabel unless i set it as a string which i cannot do

    JLabel s1 = new JLabel(score1);
    c.gridx = 1;
    c.gridy = 2;
    panel.add(l2, c);

  13. #13
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: changing int with jbutton

    JLabel(Integer.toString(score1));
    Last edited by jbarke12; 05-21-2013 at 10:44 PM.

  14. #14
    Frank_21 is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: changing int with jbutton

    I put that in right above where the jlabel is and that didn't change anything?

  15. #15
    jbarke12's Avatar
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    88
    Rep Power
    0

    Default Re: changing int with jbutton

    no do it within the jlabel
    Java Code:
    JLabel s1= new JLabel(Integer.toString(score1));

  16. #16
    Frank_21 is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: changing int with jbutton

    alright thanks i know i said the last question was the last one but this one is the last one, when i did that and ran it, i pressed the button and nothing happened, so if im thinking right that means the variable has to be updated. how would i do this? if im wrong tell me what i should do

  17. #17
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,764
    Rep Power
    5

    Default Re: changing int with jbutton

    When you create your button, use
    Java Code:
    button.setText(Integer.toString(score1));
    to set the text of the button.

    In your actionListener routine, use the setText method to change the text of the button as it is updated.

    Java Code:
    public void actionPerformed(ActionEvent ae) {
             if (ae.getSource() instanceof JButton) {
                JButton b = (JButton) ae.getSource();
                b.setText(Integer.toString(++score1));
             }
    }
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    Frank_21 is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: changing int with jbutton

    wait i think your thinking of updating the button, im wanted to update what the jlabel says

  19. #19
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,764
    Rep Power
    5

    Default Re: changing int with jbutton

    Sorry, I thought you wanted to update the button since you tried to put "score1" in the constructor. You can do the same thing with the JLabel but you probably already figured that out.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  20. #20
    765891 is offline Member
    Join Date
    Jan 2013
    Location
    'Murica
    Posts
    25
    Rep Power
    0

    Default Re: changing int with jbutton

    Hey instead you can change that to
    displayNum++;
    //this adds one to the number and stores it
    displayNum+=10;
    //this adds 10 to the number and stores it, you can change thee 10 to any number you want

Similar Threads

  1. 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
  2. Replies: 7
    Last Post: 07-12-2011, 04:19 PM
  3. changing JButton image upon clicking
    By Logical in forum AWT / Swing
    Replies: 2
    Last Post: 12-21-2010, 02:53 AM
  4. Replies: 0
    Last Post: 08-14-2010, 10:09 AM
  5. [SOLVED] Changing a JButton's border
    By a_elalaily in forum AWT / Swing
    Replies: 5
    Last Post: 05-11-2009, 12:07 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
  •