English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

when i select an item in a jlist, then right clicking it will show the popup menu. how do i implement this in java?

2007-07-05 01:52:01 · 1 answers · asked by val 2 in Computers & Internet Programming & Design

1 answers

Use "addMouseListener()". You have to check for isPopupTrigger on both mousePressed() and mouseReleased(), because some operating systems expect to do pop-up menus on button-down and others on button-up:

jProjList.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if ( e.isPopupTrigger() && e.getClickCount() == 1 )
ConfigToolGUI.this. showProjMenu( e );
}
public void mousePressed(MouseEvent e) {
if ( e.isPopupTrigger() && e.getClickCount() == 1 )
ConfigToolGUI.this. showProjMenu( e );
} }
);

=========================
ShowProjMenu() actually brings up the pop-up and looks like this:

JPopupMenu menu = new JPopupMenu();
JMenuItem menuItem;
menuItem = new JMenuItem( "Create New Project ..." );
menuItem.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent ev) {
// here's the code you want activated
// when that menu pick is selected
} } );
menu.add( menuItem );
[... and several more items...]
menu.show( e.getComponent(), e.getX(), e.getY() );

2007-07-05 02:53:34 · answer #1 · answered by McFate 7 · 0 0

fedest.com, questions and answers