Posted By:
durg_prasad
Posted On:
Thursday, September 9, 2010 02:28 AM
I am using the splitbutton in my project. when i am clking the dropdown button a menu will be displayed. but is not hiding or disable when i am click the mouse on the jpanel. Please help me any one! //The code is below import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.plaf.basic.BasicArrowButton; public class SplitButton extends JButton implements ActionListener { private JButton mainButton; private JButton dropDownButton; private JPopupMenu dropDownMenu; public SplitButton() { this(" "); } public SplitButton(St
More>>
I am using the splitbutton in my project. when i am clking the dropdown button a menu will be displayed. but is not hiding or disable when i am click the mouse on the jpanel.
Please help me any one!
//The code is below
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicArrowButton;
public class SplitButton extends JButton implements ActionListener
{
private JButton mainButton;
private JButton dropDownButton;
private JPopupMenu dropDownMenu;
public SplitButton()
{
this(" ");
}
public SplitButton(String text)
{
this(new JButton(text), SwingConstants.SOUTH);
}
public SplitButton(String text, int orientation)
{
this(new JButton(text), orientation);
}
public SplitButton(JButton mainButton, int orientation)
{
super();
this.mainButton = mainButton;
this.dropDownButton = new BasicArrowButton(orientation);
dropDownButton.addActionListener(this);
this.setBorderPainted(false);
this.dropDownButton.setBorderPainted(false);
this.mainButton.setBorderPainted(false);
this.setPreferredSize(new Dimension(75, 34));
this.setMaximumSize(new Dimension(75, 34));
this.setMinimumSize(new Dimension(200, 34));
this.setLayout(new BorderLayout());
this.setMargin(new Insets(-3, -3,-3,-3));
this.add(mainButton, BorderLayout.CENTER);
this.add(dropDownButton, BorderLayout.EAST);
}
public void setMenu(JPopupMenu menu)
{
this.dropDownMenu = menu;
}
public JButton getMainButton()
{
return mainButton;
}
public JButton getDropDownButton()
{
return dropDownButton;
}
public JPopupMenu getMenu()
{
return dropDownMenu;
}
public void actionPerformed(ActionEvent e)
{
if(this.dropDownMenu == null)
{
return;
}
if(!dropDownMenu.isVisible())
{
Point p = this.getLocationOnScreen();
dropDownMenu.setLocation( (int) p.getX(),(int) p.getY() + this.getHeight());
dropDownMenu.setVisible(true);
}
else
{
dropDownMenu.setVisible(false);
}
}
public void addActionListener(ActionListener al)
{
this.mainButton.addActionListener(al);
this.addActionListener(al);
}
public class Draw extends JPanel
{
public void mousePressed(MouseEvent e)
{
dropDownMenu.setVisible(false);
}
}
public static void test()
{
JFrame frame = new JFrame("Simple Split Button Test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
JToolBar toolBar = new JToolBar("tb");
JButton sbButton = new JButton("sb");
sbButton.setBackground(Color.BLACK);
sbButton.setContentAreaFilled(false);
SplitButton sb = new SplitButton(sbButton, SwingConstants.SOUTH);
toolBar.add(new JButton("test button"));
toolBar.add(sb);
p.add(new JLabel("SplitButton test"), BorderLayout.CENTER);
JPopupMenu testMenu = new JPopupMenu("test menu");
testMenu.add(addMI("menuItem1"));
testMenu.add(addMI("menuItem2"));
sb.setMenu(testMenu);
frame.getContentPane().add(toolBar, BorderLayout.NORTH);
frame.getContentPane().add(p, BorderLayout.CENTER);
frame.setSize(200, 100);
frame.show();
/* frame.addMouseListener( new MouseAdapter()
{
public void mousePressed(MouseEvent evt)
{
dropDownMenu.setVisible(false);
}
} );*/
}
private static JMenuItem addMI(String text)
{
JMenuItem mi = new JMenuItem(text);
mi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println(e.getActionCommand());
}
});
return mi;
}
public static void main(String[] args)
{
test();
}
}
<<Less