Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi all,

I was just writing a code to practice Swing Components when i came across this behavior of a JButton and now curious to know the reason behind it.

I had a few JButtons on a JFrame.
I added actionlisteners to those buttons. And in the actionPerformed method wrote the following code.

Java
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(this,e.getSource());
}


JOptionPane is a class which I am using to display a message box. The first argument is the object of container and the second argument is a string which i want to show. Just out of curiosity I wrote e.getSource() instead of a string. Now the object of button invoking the method is being sent in the MessageBox and the message box is showing the button.
But when I click the ok button the button (which had actually shown the message box) appears to be pressed and cannot be pressed again.
I just want to know the reason for this behavior.

Here is my complete code. It doesnt do anything. I was only practicing use of Swing Components.
Java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class fdemo extends JFrame implements ActionListener
{
fdemo()
{
super("MyFrame");
JButton a = new JButton("a");
JButton b = new JButton("b");
JButton c = new JButton("c");
a.addActionListener(this);
b.addActionListener(this);
c.addActionListener(this);
add(a,BorderLayout.NORTH);
add(b,BorderLayout.CENTER);
add(c,BorderLayout.SOUTH);
setSize(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(this,e.getSource());
}
public static void main(String args[])
{
new fdemo();
}
}
Posted
Comments
TorstenH. 9-Jul-12 9:42am    
thanks for arguing. Have a fine day.
Rahul_Patel 9-Jul-12 10:24am    
lol... when u cnt answer a question... this is wht u do.. rate it 1 star... :D :P

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900