How to get the type of component clicked?

2

The getSource () method returns the event source, is there any method that returns the event source type, whether it was a jlabel, or jbutton, something like this?

    
asked by anonymous 06.12.2015 / 22:27

2 answers

2

You can use the instanceof in an if, checking which class belongs to the object, eg

if(getSource() instanceof JButton){
//Foi clicado em um botão
}
else if(getSource() instanceof JLabel){
//Foi clicado em uma JLabel
}
    
09.12.2015 / 14:36
2

Suppose you have a _var variable that stores the event source, obtained from getSource ().

You can get the type of this (and any other) variable with getClass (). The comparison can be made like this:

_var.getClass() == JButton.class
    
06.12.2015 / 22:33