I have a panel in which some components are added. They are classes that extend JPanel
and JLabels
In my example 3+3 are placed but let's say there can be many of them.
import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
public class MyPane {
JFrame frame;
JPanel panel;
JPanel addpanel;
public void createUI()
{
frame = new JFrame("Test clicks");
panel = new JPanel();
frame.setPreferredSize(new Dimension(300, 300));
frame.setLayout(new BorderLayout());
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
//ADD SMALLPANELS
ArrayList<Color> colorList = new ArrayList<Color>();
colorList.add(Color.red);
colorList.add(Color.yellow);
colorList.add(Color.blue);
for (int i=0; i<3; i++){
String mytext="no"+ i;
MyArea addpanel = new MyArea(mytext);
addpanel.setBackground(colorList.get(i));
panel.add(addpanel);
}
//ADD LABELS
for (int i=0; i<4 ; i++){
JLabel myLabel= new JLabel();
myLabel.setText("label"+ i);
panel.add(myLabel);
}
//ADD INFORMATION LABEL
JLabel MyTitle= new JLabel();
MyTitle.setText("You just clicked on:");
MyTitle.setBackground(Color.yellow);
MyTitle.setOpaque(true);
panel.add(MyTitle);
}
public static void main(String[] args) {
MyPane overlapPane = new MyPane();
overlapPane.createUI();
}
public class MyArea extends JPanel{
public String areaname;
public MyArea(String myname) {
areaname=myname;
}
public String getAreaName() {
return areaname;
}
}
}
What I want is, each time the user clicks on a component in the container, get the class of what is clicked. Then, depending on what is clicked, get some of its properties (JLabel text or myarea
areaname
and change the MyTitle caption. How can it be done?