• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

PopupMenu & popupwindow

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic