1 package jp.sourceforge.stigmata.ui.swing;
3 import java.awt.GridBagConstraints;
4 import java.awt.GridBagLayout;
5 import java.awt.Insets;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
10 import javax.swing.JButton;
11 import javax.swing.JMenuItem;
12 import javax.swing.JPanel;
13 import javax.swing.JPopupMenu;
14 import javax.swing.plaf.metal.MetalComboBoxIcon;
21 public class PopupButton extends JPanel{
22 private static final long serialVersionUID = -4428839967597028837L;
24 private JButton button;
25 private JPopupMenu popup;
26 private JButton arrowButton;
28 public PopupButton(JButton initButton){
30 arrowButton = new JButton(new MetalComboBoxIcon());
31 popup = new JPopupMenu();
33 arrowButton.addActionListener(new ActionListener(){
35 public void actionPerformed(ActionEvent e){
36 Point p = button.getLocation();
37 popup.show(PopupButton.this, p.x, button.getHeight());
40 Insets insets = arrowButton.getMargin();
41 arrowButton.setMargin(new Insets(insets.top, 1, insets.bottom, 1));
43 setLayout(new GridBagLayout());
44 GridBagConstraints gbc = new GridBagConstraints();
45 gbc.weightx = 0d; gbc.weighty = 0d;
46 gbc.gridx = 0; gbc.gridy = 0;
47 gbc.fill = GridBagConstraints.BOTH;
53 add(arrowButton, gbc);
59 public void setEnabled(boolean enabled){
60 super.setEnabled(enabled);
61 button.setEnabled(enabled);
62 arrowButton.setEnabled(enabled);
65 public JButton getButton(){
69 public JMenuItem addMenuItem(JMenuItem menuItem){
70 return popup.add(menuItem);