dcsimg
SplitButton/DropDown Button PopupMenu Items are not hiding this code. I need we click on popupmenu items hiding.
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   kasiram_pusalapati
Posted On:   Wednesday, September 8, 2010 12:11 AM

SplitButton MenuItems are not hiding this code. I need hide that menu items. import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.plaf.basic.BasicArrowButton; import java.awt.Image; import java.awt.image.BufferedImage; import javax.swing.event.PopupMenuListener; import javax.swing.event.PopupMenuEvent; /** * SplitButton class that provides a drop down menu when the right side arrow * is clicked. * Written by Edward Scholl (edscholl@atwistedweb.com)- use as you wish, but * a acknowlegement would be appreciated if you use this... * @author Edward Scholl */ public class SplitBut   More>>

SplitButton MenuItems are not hiding this code. I need hide that menu items.



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicArrowButton;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.event.PopupMenuListener;
import javax.swing.event.PopupMenuEvent;



/**
* SplitButton class that provides a drop down menu when the right side arrow
* is clicked.
* Written by Edward Scholl (edscholl@atwistedweb.com)- use as you wish, but
* a acknowlegement would be appreciated if you use this...
* @author Edward Scholl
*/
public class SplitButton extends JButton implements ActionListener{
private JButton mainButton, dropDownButton;
private JPopupMenu dropDownMenu;

/**
* Default Constructor that creates a blank button with a down facing arrow.
*/
public SplitButton() {
this(" ");
}

/**
* Creates a button with the specified text and a down facing arrow.
* @param text String
*/
public SplitButton(String text) {
this(new JButton(text), SwingConstants.SOUTH);
}

/**
* Creates a button with the specified text
* and a arrow in the specified direction.
* @param text String
* @param orientation int
*/
public SplitButton(String text, int orientation) {
this(new JButton(text), orientation);
}

/**
* Passes in the button to use in the left hand side, with the specified
* orientation for the arrow on the right hand side.
* @param mainButton JButton
* @param orientation int
*/
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);
}

/**
* Sets the popup menu to show when the arrow is clicked.
* @param menu JPopupMenu
*/
public void setMenu(JPopupMenu menu) {
this.dropDownMenu = menu;
}

/**
* returns the main (left hand side) button.
* @return JButton
*/
public JButton getMainButton() {
return mainButton;
}

/**
* gets the drop down button (with the arrow)
* @return JButton
*/
public JButton getDropDownButton() {
return dropDownButton;
}

/**
* gets the drop down menu
* @return JPopupMenu
*/
public JPopupMenu getMenu() {
return dropDownMenu;
}

/**
* action listener for the arrow button- shows / hides the popup menu.
* @param e ActionEvent
*/

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);
dropDownMenu.requestFocus();
}else{
dropDownMenu.setVisible(false);

}
}
//------------------------------------

private MouseListener createButtonMouseListener() {
return new MouseAdapter() {
public void mousePressed(MouseEvent e) {
// if the popup menu is currently showing, then hide it.
// else if the popup menu is not showing, then show it.
if (dropDownMenu.isShowing()) {
setVisible(true);
} else {
setVisible(false);
}
}
};
}

private PopupMenuListener createPopupMenuListener() {
return new PopupMenuListener() {
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
// no implementation.
}
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
// no implementation.
}
public void popupMenuCanceled(PopupMenuEvent e) {
// the popup menu has been canceled externally (either by
// pressing escape or clicking off of the popup menu). update
// the button's state to reflect the menu dismissal.
mainButton.setSelected(false);
}
};
}

//------------------------------------------
/*

//--------------------------------------------------------
//frame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// set popup window visibility
if (!dropDownMenu.isVisible()) {

Point p = this.getLocationOnScreen();
dropDownMenu.setLocation( (int) p.getX(),(int) p.getY() + this.getHeight());

// show the popup if not visible
mainButton.setText("Hide popup");
dropDownMenu.setVisible(true);
dropDownMenu.requestFocus();
} else {
// hide it otherwise
mainButton.setText("Show popup");
dropDownMenu.setVisible(false);
}
}
//});
*/
//-------------------------------------------------------------


/**
* adds a action listener to this button (actually to the left hand side
* button, and any left over surrounding space. the arrow button will not
* be affected.
* @param al ActionListener
*/
public void addActionListener(ActionListener al){
this.mainButton.addActionListener(al);
this.addActionListener(al);
}

public static void test(){
JFrame frame = new JFrame("Simple Split Button Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
JPanel p = new JPanel();
p.setLayout(new BorderLayout());


//ImageIcon icon=new ImageIcon("G:/Split/icons/search.gif");

JButton sbButton=new JButton(new ImageIcon("G:/Split/icons/search.gif"));
JToolBar toolBar = new JToolBar("tb");
// JButton sbButton = new JButton("icon");
sbButton.setBackground(Color.BLACK);
sbButton.setContentAreaFilled(false);
SplitButton sb = new SplitButton(sbButton, SwingConstants.SOUTH);

toolBar.add(new JButton(new ImageIcon("G:/Split/icons/max.png")));
toolBar.add(sb);


p.add(new JLabel(new ImageIcon("G:/Split/icons/Untitled-2.png")), 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(400, 250);
frame.show();

}

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
About | Sitemap | Contact