1 package jp.sourceforge.stigmata.ui.swing;
7 import java.awt.GridBagConstraints;
8 import java.awt.GridBagLayout;
9 import java.awt.Insets;
10 import java.awt.Point;
11 import java.awt.event.ActionEvent;
12 import java.awt.event.ActionListener;
14 import javax.swing.JButton;
15 import javax.swing.JMenuItem;
16 import javax.swing.JPanel;
17 import javax.swing.JPopupMenu;
18 import javax.swing.plaf.metal.MetalComboBoxIcon;
25 public class PopupButton extends JPanel{
26 private static final long serialVersionUID = -4428839967597028837L;
28 private JButton button;
29 private JPopupMenu popup;
30 private JButton arrowButton;
32 public PopupButton(JButton initButton){
34 arrowButton = new JButton(new MetalComboBoxIcon());
35 popup = new JPopupMenu();
37 arrowButton.addActionListener(new ActionListener(){
39 public void actionPerformed(ActionEvent e){
40 Point p = button.getLocation();
41 popup.show(PopupButton.this, p.x, button.getHeight());
44 Insets insets = arrowButton.getMargin();
45 arrowButton.setMargin(new Insets(insets.top, 1, insets.bottom, 1));
47 setLayout(new GridBagLayout());
48 GridBagConstraints gbc = new GridBagConstraints();
49 gbc.weightx = 0d; gbc.weighty = 0d;
50 gbc.gridx = 0; gbc.gridy = 0;
51 gbc.fill = GridBagConstraints.BOTH;
57 add(arrowButton, gbc);
63 public void setEnabled(boolean enabled){
64 super.setEnabled(enabled);
65 button.setEnabled(enabled);
66 arrowButton.setEnabled(enabled);
69 public JButton getButton(){
73 public JMenuItem addMenuItem(JMenuItem menuItem){
74 return popup.add(menuItem);