ChartEntity on Right Mouse Release (Button 3)

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
dbritton
Posts: 15
Joined: Fri Jun 06, 2003 12:47 am
Contact:

ChartEntity on Right Mouse Release (Button 3)

Post by dbritton » Thu May 18, 2017 11:02 pm

I can get a ChartEntity from the ChartMouseListener.chartMouseClicked but it only works on the left mouse click.

In testing, I can catch both mouse buttons (left and right) with a MouseListener, but not sure how the the a ChartEntity with the information
available here.

Any pointers would appreciated.

David

MrJack
Posts: 13
Joined: Thu May 18, 2017 3:41 pm
antibot: No, of course not.

Re: ChartEntity on Right Mouse Release (Button 3)

Post by MrJack » Fri May 19, 2017 9:55 am

If you look at the source code you can that JFreechart assigned the right click to the PopUpMenu :

Code: Select all

1585:         else if (e.isPopupTrigger()) {
1586:             if (this.popup != null) {
1587:                 displayPopupMenu(e.getX(), e.getY());
1588:             }
1589:         }
I'm not sure what you want to do ^^

Here you can find the full code :
http://www.jfree.org/jfreechart/api/gjd ... ource.html

dbritton
Posts: 15
Joined: Fri Jun 06, 2003 12:47 am
Contact:

Re: ChartEntity on Right Mouse Release (Button 3)

Post by dbritton » Fri May 19, 2017 2:39 pm

Yes, I know. I'm trying get a ChartEntity for a popup menu service routine.

The strategy was to create a ChartMouseListener to setup the ChartEntity and then run the
other ChartMouseListeners, which I thought would do the popup.

You have pointed me to the root of the problem. When there is popup configured,
The mouseRelease event (on Windows) overrides the mouseClicked event and does the popup.
In this case we NEVER get a mouseClicked event! I'm surprised by this and unless there is
an overriding reason, consider it an oversight.

The mouseClicked event processes all the ChartMouseListeners. Since the mouseRelease event
processes a popup menu and seems to consume or prevent the mouseClicked event, my original
strategy is a no go. Even if we got a mousClicked event, it would be after the popup.

I would think a better implementation would be to process the popup in the mouseRelease event,
after all the ChartMouseListeners were called.

It looks like I might have to extend the ChartPanel class by overriding the mouseRelease event to get a ChartEntity for my popup routine.

If there is a more elegant solution, I'd appreciate hearing it.

Regards,
David
Last edited by dbritton on Fri May 19, 2017 2:50 pm, edited 1 time in total.

MrJack
Posts: 13
Joined: Thu May 18, 2017 3:41 pm
antibot: No, of course not.

Re: ChartEntity on Right Mouse Release (Button 3)

Post by MrJack » Fri May 19, 2017 2:48 pm

Well, you can also add elements to the existent Popup by taking the JMenu and then create your popup :

Code: Select all

        String[] list_popup_menu = {"item1","item2","item3","item4","item5"};
        JMenu popMenu = yourChartPanel.getPopupMenu();
        popMenu.addSeparator();
        JMenuItem popupItem;
        for(String jItemName : list_popup_menu){
            popupItem = new JMenuItem(jItemName);
            popupItem.addActionListener(this);
            popMenu.add(popupItem);
        }
EDIT:
You can do your own by doing something like this :

Code: Select all

private void doPop(MouseEvent e){
        popMenu.show(e.getComponent(), e.getX(), e.getY());
}

addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent me) {
                if (me.isPopupTrigger())
                    doPop(me);
            }
});
And then in the ActionPerformed you do what you want

dbritton
Posts: 15
Joined: Fri Jun 06, 2003 12:47 am
Contact:

Re: ChartEntity on Right Mouse Release (Button 3)

Post by dbritton » Fri May 19, 2017 3:07 pm

I'm kinda of doing that already, but I don't know how to get a ChartEntity in the doPop(MouseEvent e)
method. As far as I know, I'd need access to ChartPanel's info object to get the EntityCollection.
Thus, the need to override ChartPanel.

dbritton
Posts: 15
Joined: Fri Jun 06, 2003 12:47 am
Contact:

Re: ChartEntity on Right Mouse Release (Button 3) - My Solut

Post by dbritton » Fri May 19, 2017 4:14 pm

Here's my solution. I created a new MouseAdaptor that will process a mouseClicked event
prior to doing a popup.

Code: Select all

addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent me) {
                if (me.isPopupTrigger())
                    chartPanel.mouseClicked(me);
            }
});

Locked