aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Adding listeners to Components Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding listeners to Components" Watch "Adding listeners to Components" New topic
Author

Adding listeners to Components

selvi parthu
Greenhorn

Joined: Feb 02, 2006
Posts: 9
Hi everybody.
Whether we can add more than one listeners to Componenets.
like,
Button b=new Button("click");
b.addActionListener(this);
d.addMouseListener(this);

if we are adding like this which listener method will be called first.

please help me.

Thanks in advance.
Rani Vish
Greenhorn

Joined: Feb 03, 2006
Posts: 22
Hi

If You Run this peice of code ,You will find mouse listener will called first,I think whatever action is triggered first, will perform first ,like when You move the mouse to the button ok mouseEntered will fired

OUTPUT ::
Entered

mouseExited

Entered

mousePressed

Hi Action Listener

mouseReleased

mouseExited
==================================================

I think this will solve Your problem


public class test extends Panel implements ActionListener ,MouseListener{
public test() {
super();
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}

private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Main Panel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test newContentPane = new test();
frame.setContentPane(newContentPane);
frame.setSize(100, 100);
frame.pack();
frame.setVisible(true);
}


TitledBorder titledBorder1 = new TitledBorder("");
public JPanel jPanel1 = new JPanel();
public BorderLayout borderLayout1 = new BorderLayout();
public JButton jButton1 = new JButton();
public JButton jButton2 = new JButton();

private void jbInit() throws Exception {
this.setLayout(null);


jPanel1.setBounds(new Rectangle(9, 13, 528, 149));
jPanel1.setLayout(borderLayout1);


jButton1.setText("OK");
jButton2.setText("Cancel");

jButton1.addActionListener(this);
jButton1.addMouseListener(this);
this.add(jPanel1);

jPanel1.add(jButton1, java.awt.BorderLayout.CENTER);
jPanel1.add(jButton2, java.awt.BorderLayout.SOUTH);


}

public void mouseClicked(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
System.out.println("Entered ");
}

public void mouseExited(MouseEvent e) {
System.out.println("mouseExited ");
}

public void mousePressed(MouseEvent e) {
System.out.println("mousePressed ");
}

public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased ");
}

public void actionPerformed(ActionEvent e) {


System.out.println("Hi Action Listener");
}
}
lakshmi v.samy
Ranch Hand

Joined: Feb 02, 2006
Posts: 30
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding listeners to Components