Il mio codice è il seguente:
Il problema è che quando vado a fare:Codice PHP:
private javax.swing.JPanel table;
private java.awt.GridBagLayout tableLayout;
private javax.swing.JLabel r;
private javax.swing.JPopupMenu popup;
private javax.swing.JMenuItem menuItem1;
private javax.swing.JMenuItem menuItem2;
private javax.swing.JMenuItem menuItem3;
private class PopupListener extends MouseAdapter {
private javax.swing.JPopupMenu popup;
public PopupListener(javax.swing.JPopupMenu p){
popup = p;
}
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(),
e.getX(), e.getY());
}
}
}
[...]
popup = new javax.swing.JPopupMenu();
//Add listener to components that can bring up popup menus.
java.awt.event.MouseListener popupListener = new PopupListener(popup);
menuItem1 = new javax.swing.JMenuItem("Condividi");
menuItem1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("azione1");
r.setBackground(Color.RED);
r.repaint();
System.out.println("RED");
}
});
popup.add(menuItem1);
menuItem2 = new javax.swing.JMenuItem("Modifica");
menuItem2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("azione2");
r.setBackground(Color.BLUE);
r.repaint();
System.out.println("BLUE");
}
});
popup.add(menuItem2);
menuItem3 = new javax.swing.JMenuItem("Riferisci");
menuItem3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("azione3");
r.setBackground(Color.GREEN);
r.repaint();
System.out.println("GREEN");
}
});
popup.add(menuItem3);
//Aggiungo i rettangoli
for(int i = 0; i < 10; i++){
javax.swing.JLabel r = new javax.swing.JLabel("0");
r.setPreferredSize(new java.awt.Dimension(120,15));
r.setBorder(BorderFactory.createEtchedBorder
(EtchedBorder.RAISED));
r.setBackground(Color.red);
r.setOpaque(true);
r.addMouseListener(popupListener);
c.gridx = 0;
c.gridy = i;
c.gridy = 0;
tableLayout.setConstraints(r, c);
table.add(r);
}
jScrollPane1.setViewportView(table);
menuItem3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("azione3");
r.setBackground(Color.GREEN);
r.repaint();
System.out.println("GREEN");
}
non mi cambia il colore, ma non entra nemmeno nell'ActionListener (controllato con le stampe che non appaiono).
Non riesco a risolvere il problema.
Suggerimenti?

Rispondi quotando