popup menu should be visible with in the associated component

hello all,

how can i make a popup menu should be visible with in the associated component.

package popup;

import java.awt.Dimension;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.JFrame;

import javax.swing.JMenuItem;

import javax.swing.JPanel;

import javax.swing.JPopupMenu;

import javax.swing.JScrollPane;

import javax.swing.JTable;

publicclass PopupMenuTempextends JFrame{

protected JPopupMenu popupMenu =new JPopupMenu();

protected JMenuItem m_mniInsertRow;

protected JMenuItem m_mniInsertScrip;

protected JMenuItem m_mniDeleterRow;

protected JMenuItem m_mniDeleteExpiredScrip;

protected JMenuItem m_mniSetAlert;

JTable tbl =new JTable(100, 8){

public Object getValueAt(int row,int col){

return row+","+col;

}

};

public PopupMenuTemp(){

m_mniInsertRow =new JMenuItem("Insert a Row");

m_mniInsertScrip =new JMenuItem("Insert a Scrip");

m_mniDeleterRow =new JMenuItem("Delete a Row");

m_mniDeleteExpiredScrip =new JMenuItem("Delete a Expired Scrip");

m_mniSetAlert =new JMenuItem("Set Alert");

popupMenu.add(m_mniInsertRow);

popupMenu.add(m_mniInsertScrip);

popupMenu.add(m_mniDeleterRow);

popupMenu.add(m_mniDeleteExpiredScrip);

popupMenu.add(m_mniSetAlert);

JPanel jp =new JPanel();

JScrollPane src =new JScrollPane(tbl);

jp.add(src);

tbl.addMouseListener(new MouseAdapter(){

publicvoid mouseReleased(MouseEvent evt){

if (evt.isPopupTrigger()){

JTable source = (JTable)evt.getSource();

int row = source.rowAtPoint( evt.getPoint() );

int column = source.columnAtPoint( evt.getPoint() );

System.out.println(row);

source.changeSelection(row, column, false,false);

popupMenu.show(evt.getComponent(), evt.getX(), evt.getY());

}

}

});

JPanel pnlTemp =new JPanel();

pnlTemp.setPreferredSize(new Dimension(50,50));

getContentPane().add(jp,"Center");

getContentPane().add(pnlTemp,"South");

setVisible(true);

pack();

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

publicstaticvoid main(String[] args)

{

new PopupMenuTemp();

}

}

thanks

daya

[4560 byte] By [dayanandabva] at [2007-10-3 9:12:56]
# 1
Your code works for me, or else I don't understand your question..
Andre_Uhresa at 2007-7-15 4:25:15 > top of Java-index,Desktop,Core GUI APIs...
# 2

Here is some old code, which I think does basically what you want:

public void mouseReleased(MouseEvent e)

{

if (e.isPopupTrigger())

{

Dimension dimensionParent = e.getComponent().getSize();

Dimension dimensionPopup = editPopupMenu.getPreferredSize();

int x = e.getX();

int y = e.getY();

if (x + dimensionPopup.width > dimensionParent.width)

x = dimensionParent.width - dimensionPopup.width;

if (y + dimensionPopup.height > dimensionParent.height)

y = dimensionParent.height - dimensionPopup.height;

editPopupMenu.show(e.getComponent(), x, y);

editPopupMenu.setInvoker( editMenu );

}

}

camickra at 2007-7-15 4:25:15 > top of Java-index,Desktop,Core GUI APIs...
# 3

OK, now I understand the problem.

One small change is needed to camick's solution, due to the table being scrollable:

Dimension dimensionParent = e.getComponent().getParent().getSize();

EDIT: sorry this does not work when you have already scrolled down.

Message was edited by:

Andre_Uhres

Andre_Uhresa at 2007-7-15 4:25:15 > top of Java-index,Desktop,Core GUI APIs...
# 4

thanks camickr and Andre_Uhres,

when showing popup menu, why the first popupmenu item always selected. how can clear it?

i tried with the piece of code

popupMenu.getSelectionModel().clearSelection();

but it did't help.(any way i am giong through the api)

camickr the piece of code u given it works fine. it bounds with the associated component. that is fine.

but the popup menu should be display from the mouse point right?

mouse point should not display in between popup menu.

thanks

daya

null

dayanandabva at 2007-7-15 4:25:15 > top of Java-index,Desktop,Core GUI APIs...
# 5

hello camickr

> > public void mouseReleased(MouseEvent e)

> {

> if (e.isPopupTrigger())

> {

> Dimension dimensionParent =

> = e.getComponent().getSize();

> Dimension dimensionPopup =

> = editPopupMenu.getPreferredSize();

> int x = e.getX();

> int y = e.getY();

>

> if (x + dimensionPopup.width >

> > dimensionParent.width)

> x = dimensionParent.width - dimensionPopup.width;

>

> if (y + dimensionPopup.height >

> > dimensionParent.height)

> y = dimensionParent.height -

> t - dimensionPopup.height;

>

> editPopupMenu.show(e.getComponent(), x, y);

> editPopupMenu.setInvoker( editMenu );

> }

> }

>

i changed this code little bit so that the popup menu should apper mouse point

if (x + dimensionPopup.width > dimensionParent.width)

x = dimensionParent.width - (dimensionParent.width - x) - dimensionPopup.width ;

if (y + dimensionPopup.height > dimensionParent.height)

y = dimensionParent.height - (dimensionParent.height - y) - dimensionPopup.height;

it's work fine,

i am tring figure why the first menu item is selected

thanks

daya

dayanandabva at 2007-7-15 4:25:15 > top of Java-index,Desktop,Core GUI APIs...
# 6
> i am tring figure why the first menu item is selectedIts a bug: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=641745
camickra at 2007-7-15 4:25:15 > top of Java-index,Desktop,Core GUI APIs...
# 7

thanks camickr,

the workaround is not working for me,.

here the demo.

and the printf statement in the setSelected() method also not printing

import java.awt.Component;

import java.awt.Dimension;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.JFrame;

import javax.swing.JMenuItem;

import javax.swing.JPanel;

import javax.swing.JPopupMenu;

import javax.swing.JSeparator;

import javax.swing.JTable;

import javax.swing.MenuElement;

import javax.swing.MenuSelectionManager;

import javax.swing.SwingUtilities;

public class PopupMenu extends JFrame

{

protected JMenuItem m_mniInsertRow;

protected JMenuItem m_mniInsertScrip;

protected JMenuItem m_mniDeleterRow;

protected JMenuItem m_mniDeleteExpiredScrip;

protected JMenuItem m_mniSetAlert;

public PopupMenu() {

final JPopupMenu popupMenu = new JPopupMenu()

{

public void setSelected(Component sel)

{

int index = getComponentIndex( sel );

System.out.println("index"+index);

getSelectionModel().setSelectedIndex(index);

final MenuElement me[] = new MenuElement[2];

me[0]=(MenuElement)this;

me[1]=getSubElements()[index];

System.out.println("element"+me);

SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

MenuSelectionManager.defaultManager().setSelectedPath(me);

}

});

}

};

m_mniInsertRow = new JMenuItem("Insert a Row");

m_mniInsertScrip = new JMenuItem("Insert a Scrip");

m_mniDeleterRow = new JMenuItem("Delete a Row");

m_mniDeleteExpiredScrip = new JMenuItem("Delete a Expired Scrip");

m_mniSetAlert = new JMenuItem("Set Alert");

popupMenu.add(m_mniInsertRow);

popupMenu.add(m_mniInsertScrip);

popupMenu.addSeparator();

popupMenu.add(m_mniDeleterRow);

popupMenu.add(m_mniDeleteExpiredScrip);

popupMenu.add(new JSeparator());

popupMenu.add(m_mniSetAlert);

JPanel jp = new JPanel();

jp.setPreferredSize(new Dimension(400,400));

jp.addMouseListener(new MouseAdapter() {

public void mouseReleased(MouseEvent e) {

if (e.isPopupTrigger()) {

int x = e.getX();

int y = e.getY();

popupMenu.show(e.getComponent(), x, y);

}

}

});

getContentPane().add(jp);

pack();

setDefaultCloseOperation(EXIT_ON_CLOSE);

setVisible(true);

}

public static void main(String[] args)

{

new PopupMenu();

}

}

thanks

daya

Message was edited by:

dayanandabv

dayanandabva at 2007-7-15 4:25:15 > top of Java-index,Desktop,Core GUI APIs...
# 8

> the workaround is not working for me,.

Its not meant to be a workaround. You can't clear the selection. You can only change the selection to be on some item other than the first item.

> and the printf statement in the setSelected() method also not printing

Isn't that a clue? How do you expect the code to be executed if you never invoke the method?

camickra at 2007-7-15 4:25:15 > top of Java-index,Desktop,Core GUI APIs...
# 9

what i think is setSelected method is Overrided method?

and one more thing is throwing an exception java.lang.ArrayIndexOutOfBoundsException

i found out this is due to JSeparator() component.

but i have to do it dynamicaly.

thanks

daya

Message was edited by:

dayanandabv

dayanandabva at 2007-7-15 4:25:15 > top of Java-index,Desktop,Core GUI APIs...