Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting multiple events with one click 1

Status
Not open for further replies.

BPMan

Programmer
Jun 25, 2002
163
US
BUTTON.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){

System.out.println("PRINT");

}});

when i click on a button i am getting a bunch of clicks essentially....with one click on the button it will output PRINT multiple times....Is there anyway to either prevent the multiple clicks or ignore all but the first one....
thanks
 
No idea,

I've tried it with a JButton and a Button, but it always prints just once.
[ When using a KeyListener, you can have multiple events : KeyPressed, KeyReleased events ]
 
ok here is a simplied version of my actual program...basicially it has two buttons a and b....if you click them an a or a b will be written out....if i am clicking them at first it justs outputs one a or one b..
but if do some other stuff, like resize...it will output them multiple times with only one click from then on...here is the code:

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

public class clickTest
{
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) { }
new clickTest();
}

JFrame frame = new JFrame();
JMenuBar topMenu = new JMenuBar();
JMenu file;
JMenuItem a,b;
JPanel pshapes = new JPanel(new GridLayout(2,1));
JButton shapesB[] = new JButton[2];
String shapesN[] = {"a","b"};

Color msgrey = new Color(212, 208, 200);

clickTest()
{
int i = 0;
while(i<shapesB.length)
{
pshapes.add(shapesB=new JButton(shapesN));

i++;
}



frame.getContentPane().add(topMenu,"North");
frame.getContentPane().add(new MyComponent(),"Center");

frame.getContentPane().add(pshapes,"West");


frame.setTitle("clickTest");

int frameWidth = 800;
int frameHeight = 500;
frame.setSize(frameWidth, frameHeight);

file=new JMenu("File");

file.add(a=new JMenuItem("a"));
file.add(b=new JMenuItem("b"));
topMenu.add(file);

frame.setVisible(true);
}

class MyComponent extends JPanel
{

public void paint(Graphics g)
{


shapesB[0].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("a" );
shapesB[0].setBackground(Color.green);
shapesB[1].setBackground(msgrey);

}});
shapesB[1].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("b" );
shapesB[1].setBackground(Color.green);
shapesB[0].setBackground(msgrey);

}});



}




}
}
 
Everytime when the paint() method of "MyComponent" is executed an actionListener is added to the buttons. So everytime you resize, more and more actionListeners are added.
What is the purpose of the "MyComponent" anyway ?
The following works perfect without it :
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.IOException;

public class ClickTest
{
    public static void main(String[] args)
    {
        try
        {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) { }
        new ClickTest();
    }

            JFrame frame = new JFrame();
            JMenuBar topMenu = new JMenuBar();
            JMenu file;
            JMenuItem a,b;
            JPanel pshapes = new JPanel(new GridLayout(2,1));
            JButton shapesB[] = new JButton[2];
            String shapesN[] = {"a","b"};

            Color msgrey = new Color(212, 208, 200);

    ClickTest()
    {
            int i = 0;
            while(i<shapesB.length)
            {
            pshapes.add(shapesB[i]=new JButton(shapesN[i]));

            i++;
            }
            shapesB[0].addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
                        System.out.println("a" );
                        shapesB[0].setBackground(Color.green);
                        shapesB[1].setBackground(msgrey);

                    }});
            shapesB[1].addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
                        System.out.println("b" );
                        shapesB[1].setBackground(Color.green);
                        shapesB[0].setBackground(msgrey);

                    }});



            frame.getContentPane().add(topMenu,"North");
            //frame.getContentPane().add(new MyComponent(),"Center");

            frame.getContentPane().add(pshapes,"West");


            frame.setTitle("clickTest");

            int frameWidth = 800;
            int frameHeight = 500;
            frame.setSize(frameWidth, frameHeight);

            file=new JMenu("File");

            file.add(a=new JMenuItem("a"));
            file.add(b=new JMenuItem("b"));
            topMenu.add(file);

            frame.setVisible(true);
    }


}
 
ahhh yes i didn't realize that is how listeners work...
you are a genius, if i had a lot of money i would give some of it to you...i would have never figured that out on my own......since i can't give you money will you settle for a purple star...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top