coo12
coo12

Reputation: 117

Array JLabel ActionListener multiple JPanels

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

Answers (2)

MadProgrammer
MadProgrammer

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

ControlAltDel
ControlAltDel

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 JLabels. 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

Related Questions