-1

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?

geo
  • 517
  • 1
  • 9
  • 28
  • Attach a MouseListener to your panel. – initramfs Feb 03 '14 at 18:25
  • Use Java naming convention. Classes start with capital letters using `CamelCasing` – Paul Samsotha Feb 03 '14 at 18:33
  • What I was asking was answered here: [http://stackoverflow.com/questions/7340001/determine-clicked-jpanel-component-in-the-mouselistener-event-handling][1] [1]: http://stackoverflow.com/questions/7340001/determine-clicked-jpanel-component-in-the-mouselistener-event-handling – geo Feb 04 '14 at 10:01

2 Answers2

5
  1. Use Java naming convention. Class names begin with capital letters using CamelCasing i.e. MyArea

  2. Don't do this JPanel addpanel = new myarea(..). Why? Because JPanel does not have the methods myarea does. You should be doing this (after correct naming convention).

    MyArea addPanel = new MyArea(...);
    
  3. Use private fields a getters

    public class MyArea extends JPanel {
        private String areaName;
        ...
        public String getAreaName() {
            return areaName;
        }
    }
    
  4. Add a MouseListener to the panel.

    private class MyMouseListener extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent e) {
            MyArea source = (MyArea)e.getSource();
            String areaName = source.getAreaName();
            System.out.println(areaName);
        }
    }
    ....
    MyMouseListener listener = new MyMouseListener();
    for (int i=0; i<3; i++){
       String mytext="no"+ i;
       addpanel = new myarea(mytext);
       addPanel.addMouseListener(new MyMouseListener());
       addpanel.setBackground(colorList.get(i));  
       panel.add(addpanel);
    }
    
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thanks. It works fine. What should I change if I don't know what is clicked? Maybe there are other things in the container such as JLabels. In that case MyArea source = (MyArea)e.getSource(); cannot work – geo Feb 03 '14 at 19:17
  • What exactly are you trying to achieve? – Paul Samsotha Feb 03 '14 at 19:22
  • In the panel there are the myarea components and some other components such as Jlabels. What I need is when the user clicks on a myarea instance then he gets the areaName. When he click elsewhere nothing happens. When the mouse moves over a myarea the mouse changes to "hand" cursor. Actualy what you propose is adding a listener to the myarea. But maybe something should be added to the container that listens to what is clicked. – geo Feb 03 '14 at 19:26
  • You can also override `mouseEntered` and do something when the mouse enters the panel. Also `mouseExited` – Paul Samsotha Feb 03 '14 at 19:28
  • I unaccepted because I understood that I need something in the container that listents to what is clicked. Not in the component. – geo Feb 03 '14 at 19:34
  • Nowhere in your original question do mention anything about what you're comment suggests. _"What I want is to click on a small panel (myarea panel) and get which panel was clicked. Then I could get any properties that panel has (such as areaname) How could I do it?"_ – Paul Samsotha Feb 03 '14 at 19:46
1

Add a MouseListener to the panel.

    addpanel.addMouseListener(new MouseListener() {
        @Override
        public void mouseReleased(MouseEvent e) {
        }
        @Override
        public void mousePressed(MouseEvent e) {
        }
        @Override
        public void mouseExited(MouseEvent e) {
        }
        @Override
        public void mouseEntered(MouseEvent e) {
        }
        @Override
        public void mouseClicked(MouseEvent e) {
            myarea panel = (myarea)e.getSource();
            //you can access the properties of the panel here
        }
    });
4J41
  • 5,005
  • 1
  • 29
  • 41