participate


Swing - popup from which component
This question is answered. Correct Answer available

<<   Back to Forum  |   Give us Feedback
This topic has 6 replies on 1 page.
choc_budda
Posts:23
Registered: 11/4/09
popup from which component   
Nov 5, 2009 4:15 PM
 
 
I'm guessing this is a simple enough work around but I'm stumped:

Is it possible to determine from which component of container a popupmenu derives?

I currently have a loop that adds components to a JPanel adding a popupmenu to each component.
I guess I could alter the ActionCommand to identify each component uniquely?

but is there a simpler way?

I'm using:

newCourseView.setComponentPopupMenu(popup);


(newCourseView being a JPanel)

if I use the above implementation for the popup...how do i then access the old popup command where i could get x/y coords??

(since I'm thinking that maybe I can use "getcomponentAt(x,y)" to accomplish this?)

TIA
Michael

Edited by: choc_budda on Nov 5, 2009 4:12 PM
 
StanislavL
Posts:1,480
Registered: 2/13/02
Re: popup from which component   
Nov 5, 2009 11:04 PM (reply 1 of 6)  (In reply to original post )
Helpful
 
What about the methods of JPopupMenu
public void show(Component invoker, int x, int y)
and
public Component getInvoker()

Regards,
Stas
 
choc_budda
Posts:23
Registered: 11/4/09
Re: popup from which component   
Nov 6, 2009 12:00 AM (reply 2 of 6)  (In reply to #1 )
 
 
Im sorry...I saw those methods, but am unsure how to access them?

In the listener how do i get a reference to this?

e.getSource gives me the menuItem?

same with e.invoker?

apologies for my newbyness

perhaps if it were an innerclass??

Edited by: choc_budda on Nov 5, 2009 11:59 PM
 
StanislavL
Posts:1,480
Registered: 2/13/02
Re: popup from which component   
Nov 6, 2009 12:45 AM (reply 3 of 6)  (In reply to #2 )
Correct
 
When you show a popup for specified component passed in the method you then can obtain the component from getInvoker().

If you have popup menu just use invoker. If no but listener and event you can get MenuItem as source of event and find popup menu which is parent of the menu item. After that use the invoker.

Regards,
Stas
 
choc_budda
Posts:23
Registered: 11/4/09
Re: popup from which component   
Nov 6, 2009 1:56 AM (reply 4 of 6)  (In reply to #3 )
 
 
Thanks:

found it independently so here's what i have:

JMenuItem menuSelect = (JMenuItem)e.getSource();
	        JPopupMenu parentMenu = (JPopupMenu)menuSelect.getParent();
	        CourseText selectedCourse = (CourseText)parentMenu.getInvoker();
 
kopik
Posts:110
Registered: 11/15/08
Re: popup from which component   
Nov 6, 2009 2:09 AM (reply 5 of 6)  (In reply to #4 )
 
 
hii,

altogether:


class PopupListener extends MouseAdapter {
 
        JPopupMenu popup;
 
        PopupListener(JPopupMenu popupMenu) {
            popup = popupMenu;
        }
 
        @Override
        public void mousePressed(MouseEvent e) {
            maybeShowPopup(e);
        }
 
        @Override
        public void mouseReleased(MouseEvent e) {
            // here you can sets prereqiusities
            maybeShowPopup(e);
            // here you can built by "clear" prereqiusities
        }
 
        private void maybeShowPopup(MouseEvent e) {
            if (e.isPopupTrigger()) {
                popup.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    }
 
    public void actionPerformed(ActionEvent e) {
        JMenuItem source = (JMenuItem) (e.getSource());
    }
 
    public void itemStateChanged(ItemEvent e) {
        JMenuItem source = (JMenuItem) (e.getSource());
    }
 
    protected String getClassName(Object o) {// Returns just the class name -- no package info.
        String classString = o.getClass().getName();
        int dotIndex = classString.lastIndexOf(".");
        return classString.substring(dotIndex + 1);
    }
 


... kopik
 
choc_budda
Posts:23
Registered: 11/4/09
Re: popup from which component   
Nov 6, 2009 10:01 PM (reply 6 of 6)  (In reply to #5 )
 
 
I wonder if (using OOP principles) it wouldn't be better to override whatever method is hidden away inside the .setComponentPopupMenu method, so that the call actually sends info about which component this popupmenu derives from??

if so...would that mean extending the popupmenu Class?
 
This topic has 6 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 54
  • Guests : 135

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums