Reputation: 117
I have an array of JLabels and I want to add an ActionListener to them. Every label should display a panel and the other should be removed. How can I realize this?
Upvotes: 1
Views: 104
Reputation: 347204
I have an array of JLabels and I want to add an ActionListener to them.
JLabel
doesn't have ActionListener
support. You can use a undecorated JButton
instead
Every label should display a panel and the other should be removed. How can I realize this?
Use a CardLayout
Upvotes: 2
Reputation: 35011
You cannot directly add an ActionListener
to a JLabel
- it doesn't have that functionality. Instead, you should create a MouseAdapter
, override the mouseClicked
method, and use JLabel.addMouseListener
to add it to your JLabel
s. The best way to get it to, as you say, "display a panel and the other should be removed" would be to use a CardLayout
.
Upvotes: 2