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;
26 public class PopupButton extends JPanel{
27 private static final long serialVersionUID = -4428839967597028837L;
29 private JButton button;
30 private JPopupMenu popup;
31 private JButton arrowButton;
33 public PopupButton(JButton initButton){
35 arrowButton = new JButton(new MetalComboBoxIcon());
36 popup = new JPopupMenu();
38 arrowButton.addActionListener(new ActionListener(){
40 public void actionPerformed(ActionEvent e){
41 Point p = button.getLocation();
42 popup.show(PopupButton.this, p.x, button.getHeight());
45 Insets insets = arrowButton.getMargin();
46 arrowButton.setMargin(new Insets(insets.top, 1, insets.bottom, 1));
48 setLayout(new GridBagLayout());
49 GridBagConstraints gbc = new GridBagConstraints();
50 gbc.weightx = 0d; gbc.weighty = 0d;
51 gbc.gridx = 0; gbc.gridy = 0;
52 gbc.fill = GridBagConstraints.BOTH;
58 add(arrowButton, gbc);
64 public void setEnabled(boolean enabled){
65 super.setEnabled(enabled);
66 button.setEnabled(enabled);
67 arrowButton.setEnabled(enabled);
70 public JButton getButton(){
74 public JMenuItem addMenuItem(JMenuItem menuItem){
75 return popup.add(menuItem);