quick question about a ButtonListener and a ActionListener


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: quick question about a ButtonListener and a ActionListener

  1. #1
    Join Date
    Jul 2005
    Posts
    1

    quick question about a ButtonListener and a ActionListener

    I have made a JPanel that has two buttons. One is called Increment and the other is called Decrement. When you press the Increment, it supposed to add 1 to the total. Decrement will subtract 1 from the total. The buttons work fine when I comment the other out, but when they are both in there they cancel each other out, so nothing happens.

    How do I go about getting them to work together?


    Here's my code:

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;

    public class ButtonPanel extends JPanel
    {

    private int count;
    private JButton plus;
    private JButton minus;
    private JLabel label;


    public ButtonPanel()
    {
    count = 50;

    plus = new JButton("Increment");
    plus.addActionListener(new ButtonListener());

    minus = new JButton("Decrement");
    minus.addActionListener(new ButtonListener());

    label = new JLabel("" + count);

    add(plus);
    add(minus);
    add(label);

    setPreferredSize (new Dimension(400, 50));
    setBackground (Color.orange);
    }

    private class ButtonListener implements ActionListener
    {
    public void actionPerformed (ActionEvent event)
    {
    count++;
    count--;

    label.setText("" + count);
    }

    }
    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Keep it simple

    Inner classes are messy.

    Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class ButtonUsage
        extends JPanel implements ActionListener {
    
      private int count;
      private JButton plus;
      private JButton minus;
      private JLabel label;
    
      public ButtonUsage() {
        count = 50;
    
        plus = new JButton("Increment");
        plus.addActionListener(this);
    
        minus = new JButton("Decrement");
        minus.addActionListener(this);
    
        label = new JLabel("" + count);
    
        add(plus);
        add(minus);
        add(label);
    
        setPreferredSize(new Dimension(400, 50));
        setBackground(Color.orange);
      }
    
      public void actionPerformed(ActionEvent event) {
        if (event.getSource()==plus) {
          label.setText("" + (++count));
        } else if (event.getSource()==minus) {
          label.setText("" + (--count));
        } else {
          label.setText("HUH ??"); // will never happend
        }
      }
    }
    eschew obfuscation

  3. #3
    Join Date
    Oct 2004
    Posts
    3
    Code:
    private class ButtonListener implements ActionListener
    {
      public void actionPerformed (ActionEvent event)
      {
        count++;
        count--;
    
        label.setText("" + count);
      }
    }
    This bit of code explains it for you...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center