Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Incrementing button by 1

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Incrementing button by 1

    Hey guys... so here's the "problem" now. When you compile and run this code as of now, it will bring up a screen with a button on it - when you click teh button it will do nothing.
    We're supposed to make the button increment by 1 every time we click it.

    I'm somewhat new to GUI's, and the teacher is of no help at all.

    Thanks in advance.

    package FourthFrame;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    class MyJFrame extends JFrame implements ActionListener{
     
        // private instance variables
        JButton button1;
        JButton button2;
        int button1Count;
        int button2Count;
     
        public static void main (String [] args) {
     
    	MyJFrame mine = new MyJFrame();
            mine.setVisible(true);
     
        }
     
        public MyJFrame () {
     
            setTitle ("This is a \"MyJFrame\" object (V4)");
            setSize (300, 500);
            setDefaultCloseOperation( EXIT_ON_CLOSE );
     
            // get the content pane and set properties
            Container contentPane = getContentPane();
            contentPane.setBackground (Color.blue);
            contentPane.setLayout(null); // so that we can use absolute positioning
     
            // construct two buttons
            button1 = new JButton("0");
            button1.setBounds(110,230,80,40);
    	button1.addActionListener(this);
            contentPane.add(button1);
    	button2 = new JButton("0");
    	button2.setBounds(110,300,80,40);
    	button2.addActionListener(this);
    	contentPane.add(button2);
     
            // start their counts at 0
            button1Count = 0;
            button2Count = 0;
        }
     
        public void actionPerformed(ActionEvent event) {
        	button1Count++;
        	button1.setText(Integer.toString(button1Count));
        	button2Count++;
        	button2.setText(Integer.toString(button2Count)); 
     
        }
    }
    Last edited by joshft91; March 1st, 2011 at 08:40 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: Incrementing button by 1

    Quote Originally Posted by joshft91 View Post
    I'm somewhat new to GUI's, and the teacher is of no help at all.
    Sorry, but that's no excuse. Many people here are self taught, so you have a leg up on them. Don't waste time complaining about your instructor.

    Hint- Where are you incrementing anything? Where are you setting the text of the button? What are you doing when the button is pressed?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Incrementing button by 1

    I wasn't wasting my time... All I was saying was that I don't have a lot of other places to go for help.

    I know I'm going to be incrementing it in the actionPerformed method. The text is set here
    button = new JButton(new Integer(0).toString());
    and as of right now it's not doing anything when the button is pressed.

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Incrementing button by 1

    Although all of Kevin's advice leads directly to the answer, be aware that getting the source of the event for one simple button is not required if you only attach the listener to only that button.
    So whatever you have in actionPerformed() will be done when you click on your JButton.
    When or if you do start attaching a listener to multiple buttons, the efficient way to go about it would be to use:
    Object src = event.getSource();
    then check against src each time with say, if statements.
    Just some added detail.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: Incrementing button by 1

    Quote Originally Posted by joshft91 View Post
    I wasn't wasting my time... All I was saying was that I don't have a lot of other places to go for help.
    That's fine. But taking the time to complain about your instructor is indeed wasteful- if not to you, then to us. Sorry, but I get annoyed with people saying "my teacher won't even help me" when some of the best programmers learned without the benefit of an instructor/class at all.

    Quote Originally Posted by joshft91 View Post
    I know I'm going to be incrementing it in the actionPerformed method. The text is set here
    button = new JButton(new Integer(0).toString());
    and as of right now it's not doing anything when the button is pressed.
    Why don't you do anything when the button is pressed?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Incrementing button by 1

    Edit: Got it working -
    public void actionPerformed(ActionEvent event) {
        	buttonCount++;
        	button.setText(Integer.toString(buttonCount));
     
        }

    The second part to this problem is to have two buttons (updated original post) and to increment when either one is clicked.
    Obviously the way it's set up now both will increment... How would I go about making sure only the one I click increments?
    Last edited by joshft91; March 1st, 2011 at 08:41 AM.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: Incrementing button by 1

    Either add a different ActionListener to each button, or check the source of the event using event.getSource().
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Incrementing button by 1

    Alright... Well I tried something like this - it compiles fine, but it increments the 2nd button only. :S

    public void actionPerformed(ActionEvent event) {
        	if (event.equals(button1)){
        	button1Count++;
        	button1.setText(Integer.toString(button1Count));
        	}
        	else {
        	button2Count++;
        	button2.setText(Integer.toString(button2Count));
        	}
     
        }
    Is it possible to do what I'm trying to do with an if statement?

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: Incrementing button by 1

    It is possible. But why would the event equal one of the JButtons? Check out the API for ActionEvent, especially the getSource() function. You were actually already told how to do this, and in fact, your original code contained the line you want.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Junior Member
    Join Date
    Oct 2010
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Incrementing button by 1

    It appears that this works...
    public void actionPerformed(ActionEvent event) {
        	if (event.getSource().equals(button1)) {
        	button1Count++;
        	button1.setText(Integer.toString(button1Count));
        	}
        	else {
        	button2Count++;
        	button2.setText(Integer.toString(button2Count));
        	}
        }

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,180
    My Mood
    Hungover
    Thanks
    141
    Thanked 602 Times in 517 Posts

    Default Re: Incrementing button by 1

    Cool. Makes sense.

    Another way to do this would be to create your own ActionListener Object that keeps track of how many times its actionPerformed() method was called. Then create two instances of that Object- one for each button. That way you wouldn't have to specifically keep track of each button in a separate count.

    If that doesn't make sense, don't worry about it. Your way is fine too.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. button positioning
    By pds8475 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 29th, 2011, 08:35 AM
  2. Return from incrementing for loop?
    By mindlessn00b in forum Loops & Control Statements
    Replies: 16
    Last Post: October 4th, 2010, 12:15 PM
  3. Return from incrementing for loop HELP?!
    By mindlessn00b in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 3rd, 2010, 02:36 PM
  4. Incrementing every letter in a string that occupies an odd position.
    By haktheplanet in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2010, 04:45 AM
  5. Please help with Actionlistener-Button
    By ashleykathy in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2010, 07:21 PM