wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes PopupMenu & popupwindow Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "PopupMenu & popupwindow" Watch "PopupMenu & popupwindow" New topic
Author

PopupMenu & popupwindow

Arun Sanker
Ranch Hand

Joined: Mar 21, 2007
Posts: 44
Hi Guys,


Have some questions regarding invoking a popup menu.Hope some one could give me a hand on this one...
Please see attached Figure as well.



[1]
Have a Table having a certain number of rows.want to create a pop up menu when user selects a cell and rightclicks from that cell.

But I read the following from the java tutorial:

>>To bring up a popup menu ( JPopupMenu), you must register a mouse listener on each >>component that the popup menu should be associated with. The mouse listener must >>detect user requests that the popup menu be brought up.

So is it possible to bring up the popupmenu when rightclicking on a certain cell ,rather than a component(Ofcourse cells belong to the component,but dont want the popup window to come up when right clicking on any cell in the table).How can I achive this behaviour.

[2]After clicking on one of the menuItem from the popupmenu,want a window with some data to be displayed as shown in the attached figure.This window should have scrollbars & each string should be in a different line in this window.
Also the window position or origin should be just below the cell where the right click occured.The window should vanish when selection is made on any other cells.

Would be nice if someone could tell me how these things are to be done.A code snippet would be great....

Thanks
[ August 09, 2007: Message edited by: Arun Sanker ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
So is it possible to bring up the popupmenu when rightclicking on a certain cell ,rather
than a component(Ofcourse cells belong to the component,but dont want the popup window to
come up when right clicking on any cell in the table).How can I achive this behaviour.

The general approach is to add a MouseListener and a JPopupMenu to your table and decide in
your mouse event code when to show the popupMenu. The JTable class api has many methods to
make navigation easy and efficient.
[2]After clicking on one of the menuItem from the popupmenu,want a window with some data
to be displayed as shown in the attached figure.This window should have scrollbars & each
string should be in a different line in this window.

You can use a JWindow with a fixed size and a JScrollPane containing a JList for this. The
problem is providing the user with a way to dismiss/close it. You would set it up in a way
similar to the dialog demonstrated below.
Also the window position or origin should be just below the cell where the right click
occured.The window should vanish when selection is made on any other cells.

Easy enough for the location. For the closure of the window/dialog call dispose on it from
within your mouse code if it is visible.
Arun Sanker
Ranch Hand

Joined: Mar 21, 2007
Posts: 44
That was great......Appreciate it....

Did make a few changes in the "initDialog" method,so that the window is not resizable,no header & not movable ..

private void initDialog() {

JFrame owner = (JFrame)table.getTopLevelAncestor();
dialog = new JDialog(owner, false);
dialog.setUndecorated(true);
dialog.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
JList list = new JList(new DefaultListModel());
this.pane = new JScrollPane(list);
dialog.add(this.pane);
dialog.setResizable(false);

dialog.addWindowFocusListener(new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {

}

public void windowLostFocus(WindowEvent e) {
// this will invoke the action on the popup invoking button
// and hide the visible window
dialog.hide();

}
});




}


And it works perfectly

Thanks once again for the code ....

Regards
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: PopupMenu & popupwindow
 
Similar Threads
Menu Bar
JMenuItem question
Adding graphics to JPanel using PopupMenu
Popup menu not showing when JTable cell clicked
complex JTable cell mouseover issue