I need a hand with something.
I got a panel, which I'm filling with Objects. These objects each create a panel inside the panel, which is filled with labels. It's a homemade JTable so to speak. Now I need to make a right-click menu, where I click edit/delete/etc. which needs to know what Object I clicked on.
public class Aktivitet {
static JPanel sPanel;
static String navn;
static String开发者_如何学C kontakt;
static String event;
static String oprettet;
static String note;
static String deadline;
static int tilstand;
public Aktivitet(JPanel panel, String sNavn, String sKontakt, String sEvent, String sOprettet, String sNote, String sDeadline, int sTilstand) {
this.navn = sNavn;
this.kontakt = sKontakt;
this.event = sEvent;
this.oprettet = sOprettet;
this.note = sNote;
this.deadline = sDeadline;
this.tilstand = sTilstand;
this.sPanel = panel;
JPanel akPan = new JPanel();
JPanel fillerPanel = new JPanel();
if (tilstand == 0) akPan.setBackground(Color.GREEN);
if (tilstand == 1) akPan.setBackground(Color.YELLOW);
if (tilstand == 2) akPan.setBackground(Color.RED);
akPan.setLayout(new GridLayout(4,3));
akPan.setBorder(BorderFactory.createRaisedBevelBorder());
akPan.setSize(new Dimension(10000, 75));
akPan.setMaximumSize(new Dimension(10000, 75));
fillerPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
fillerPanel.setSize(new Dimension(10000, 1));
fillerPanel.setMaximumSize(new Dimension(10000, 1));
JLabel navnP = new JLabel();
JLabel kontaktP = new JLabel();
JLabel eventP = new JLabel();
JLabel oprettetP = new JLabel();
JLabel noteP = new JLabel();
JLabel deadlineP = new JLabel();
JLabel label_navn = new JLabel();
JLabel label_kontakt = new JLabel();
JLabel label_event = new JLabel();
JLabel label_oprettet = new JLabel();
JLabel label_note = new JLabel();
JLabel label_deadline = new JLabel();
navnP.setText("Aktivitetsnavn:");
navnP.setFont(new Font("dialog",Font.ITALIC,9));
kontaktP.setText("Kontaktperson:");
kontaktP.setFont(new Font("dialog",Font.ITALIC,9));
eventP.setText("Event:");
eventP.setFont(new Font("dialog",Font.ITALIC,9));
label_navn.setText(navn);
label_navn.setFont(new Font("monospaced",Font.BOLD,16));
label_kontakt.setText(kontakt);
label_kontakt.setFont(new Font("monospaced",Font.BOLD,16));
label_event.setText(event);
label_event.setFont(new Font("monospaced",Font.BOLD,16));
oprettetP.setText("Oprettet:");
oprettetP.setFont(new Font("dialog",Font.ITALIC,9));
noteP.setText("Evt. Note:");
noteP.setFont(new Font("dialog",Font.ITALIC,9));
deadlineP.setText("Deadline:");
deadlineP.setFont(new Font("dialog",Font.ITALIC,9));
label_oprettet.setText(oprettet);
label_oprettet.setFont(new Font("monospaced",Font.BOLD,16));
label_note.setText(note);
label_note.setFont(new Font("monospaced",Font.BOLD,16));
label_deadline.setText(deadline);
label_deadline.setFont(new Font("monospaced",Font.BOLD,16));
akPan.add(navnP);
akPan.add(kontaktP);
akPan.add(eventP);
akPan.add(label_navn);
akPan.add(label_kontakt);
akPan.add(label_event);
akPan.add(oprettetP);
akPan.add(noteP);
akPan.add(deadlineP);
akPan.add(label_oprettet);
akPan.add(label_note);
akPan.add(label_deadline);
panel.add(akPan);
panel.add(fillerPanel);
panel.addMouseListener(new MouseActionListener());
akPan.setVisible(true);
fillerPanel.setVisible(true);
}
public class MouseActionListener implements MouseListener {
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
}
Thanks a lot!
You can use retrieve the source component that generated the event from the MouseEvent itself:
public void mouseClicked(MouseEvent e) {
Object o = e.getSource();
}
You should use instanceof to check the source type and cast the object to it:
if (o instanceof JLabel){
JLabel label = (JLabel)o;
}else if (o instanceof JPanel){
JPanel panel = (JPanel)o;
}
In order to discriminate between different components of the same type you could add a property to them with putClientProperty() method, and then retrieve it:
JLabel label = new JLabel();
label.putClientProperty("id", new Integer(10));
then from inside the event handler retrieve the property:
if (o instanceof JLabel){
JLabel label = (JLabel)o;
Integer labelId = (Integer)label.getClientProperty("id");
}
You can use getParent() method on a component to find it's parent and so on...
Add same mouse listener to all panels you create. Use actionEvent's getSource() method to obtain panel which was clicked.
This snippet might help,
JPanel panel = (JPanel)yourComponent.getParent();
精彩评论