1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.ArrayList;
/**
*
*/
public class ListEtat extends JPanel {
private JToggleButton invokePopupButton;
protected JPopupMenu popup;
protected ArrayList etat = new ArrayList();
public ListEtat() {
popup = new JPopupMenu();
invokePopupButton = new JToggleButton("Etats désirés V");
invokePopupButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!popup.isVisible()) {
invokePopupButton.setText("Etats désirés ^");
Point location = invokePopupButton.getLocation();
SwingUtilities.convertPointToScreen(location, invokePopupButton.getParent());
location.translate(0, invokePopupButton.getHeight()
+ (invokePopupButton.getBorder() == null ? 0
: invokePopupButton.getBorder().getBorderInsets(invokePopupButton).bottom));
popup.setLocation(location);
popup.removeAll();
for(int i = 0; i<etat.size(); i++){
JCheckBox tmp = (JCheckBox)etat.get(i);
popup.add(tmp);
}
popup.addSeparator();
popup.add(new JCheckBox("Tous"));
popup.validate();
popup.repaint();
popup.setVisible(true);
popup.requestFocus();
} else {
invokePopupButton.setText("Etats désirés V");
popup.setVisible(false);
}
}
});
this.add(invokePopupButton, BorderLayout.CENTER);
popup.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (popup.isVisible())
invokePopupButton.doClick();
}
});
}
});
}
} |
Partager