Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #29652 > unrolled thread
| Started by | Knute Johnson <eternal@knutejohnson.com> |
|---|---|
| First post | 2014-08-12 17:39 -0700 |
| Last post | 2014-08-14 19:20 -0700 |
| Articles | 7 — 4 participants |
Back to article view | Back to comp.lang.java.programmer
Problem getting the JPopupMenu from the Action it generates Knute Johnson <eternal@knutejohnson.com> - 2014-08-12 17:39 -0700
Re: Problem getting the JPopupMenu from the Action it generates Steven Simpson <ss@domain.invalid> - 2014-08-13 08:37 +0100
Re: Problem getting the JPopupMenu from the Action it generates FredK <fred.l.kleinschmidt@gmail.com> - 2014-08-13 08:02 -0700
Re: Problem getting the JPopupMenu from the Action it generates Knute Johnson <eternal@knutejohnson.com> - 2014-08-13 08:43 -0700
Re: Problem getting the JPopupMenu from the Action it generates Mike Amling <mamling@chaff.us> - 2014-08-14 00:12 -0500
Re: Problem getting the JPopupMenu from the Action it generates Steven Simpson <ss@domain.invalid> - 2014-08-14 09:54 +0100
Re: Problem getting the JPopupMenu from the Action it generates Knute Johnson <eternal@knutejohnson.com> - 2014-08-14 19:20 -0700
| From | Knute Johnson <eternal@knutejohnson.com> |
|---|---|
| Date | 2014-08-12 17:39 -0700 |
| Subject | Problem getting the JPopupMenu from the Action it generates |
| Message-ID | <lsec34$d64$1@dont-email.me> |
I'm having a problem getting the JPopupMenu from an Action that is
generated by the JPopupMenu. The error is a ClassCastException and it
is saying that what is returned by ActionEvent.getSource() cannot be
cast to a JPopupMenu because it is a JPopupMenu$1. JPopupMenu$1 is an
anonymous class and shouldn't be returned by ActionEvent.getSource().
In the end, I'm trying to get to the component that was right clicked to
pop up the menu.
I'm stumped, any ideas?
Thanks,
C:\Users\Knute Johnson\com\knutejohnson\cookbook>java test
javax.swing.JPopupMenu$1[,1,3,97x21,invalid,alignmentX=0.0,alignmentY=0.0,border
=javax.swing.plaf.metal.MetalBorders$MenuItemBorder@23b2ab4,flags=264,maximumSiz
e=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,
margin=javax.swing.plaf.InsetsUIResource[top=2,left=2,bottom=2,right=2],paintBor
der=true,paintFocus=false,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rollo
verSelectedIcon=,selectedIcon=,text=do something]
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
javax.swing
.JPopupMenu$1 cannot be cast to javax.swing.JPopupMenu
at test$someAction.actionPerformed(test.java:29)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown
Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown
Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at
javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown
Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JFrame {
final JPopupMenu popupMenu;
public test() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
popupMenu = new JPopupMenu();
popupMenu.add(new someAction());
JTextField tf = new JTextField("textField");
tf.setComponentPopupMenu(popupMenu);
add(tf,BorderLayout.CENTER);
pack();
setVisible(true);
}
public class someAction extends AbstractAction {
public someAction() {
putValue(NAME,"do something");
}
public void actionPerformed(ActionEvent ae) {
System.out.println(ae.getSource());
System.out.println();
System.out.println((JPopupMenu)ae.getSource());
}
}
public static void main(String... args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new test();
}
});
}
}
--
Knute Johnson
[toc] | [next] | [standalone]
| From | Steven Simpson <ss@domain.invalid> |
|---|---|
| Date | 2014-08-13 08:37 +0100 |
| Message-ID | <66hrbb-0uo.ln1@s.simpson148.btinternet.com> |
| In reply to | #29652 |
On 13/08/14 01:39, Knute Johnson wrote: > I'm having a problem getting the JPopupMenu from an Action that is > generated by the JPopupMenu. The error is a ClassCastException and it > is saying that what is returned by ActionEvent.getSource() cannot be > cast to a JPopupMenu because it is a JPopupMenu$1. JPopupMenu$1 is an > anonymous class and shouldn't be returned by ActionEvent.getSource(). Why not? The JPopupMenu.add(Action) method creates a JMenuItem (of type JPopupMenu$1), adds it to the menu, and returns it. If you click that item, it will be the source. > In the end, I'm trying to get to the component that was right clicked > to pop up the menu. Couldn't see anything, but why not pass popupMenu as a constructor argument to someAction? -- ss at comp dot lancs dot ac dot uk
[toc] | [prev] | [next] | [standalone]
| From | FredK <fred.l.kleinschmidt@gmail.com> |
|---|---|
| Date | 2014-08-13 08:02 -0700 |
| Message-ID | <ca2557eb-e693-4b40-94e2-11c509ac636b@googlegroups.com> |
| In reply to | #29654 |
On Wednesday, August 13, 2014 12:37:10 AM UTC-7, Steven Simpson wrote: > On 13/08/14 01:39, Knute Johnson wrote: > > > I'm having a problem getting the JPopupMenu from an Action that is > > > generated by the JPopupMenu. The error is a ClassCastException and it > > > is saying that what is returned by ActionEvent.getSource() cannot be > > > cast to a JPopupMenu because it is a JPopupMenu$1. JPopupMenu$1 is an > > > anonymous class and shouldn't be returned by ActionEvent.getSource(). > > > > Why not? The JPopupMenu.add(Action) method creates a JMenuItem (of type > > JPopupMenu$1), adds it to the menu, and returns it. If you click that > > item, it will be the source. > > > > > In the end, I'm trying to get to the component that was right clicked > > > to pop up the menu. > > > > Couldn't see anything, but why not pass popupMenu as a constructor > > argument to someAction? > > Better yet, pass the target component (in your case, tf) as an argument to the someAction constructor. -- Fred K.
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <eternal@knutejohnson.com> |
|---|---|
| Date | 2014-08-13 08:43 -0700 |
| Message-ID | <lsg11r$8te$1@dont-email.me> |
| In reply to | #29656 |
On 8/13/2014 08:02, FredK wrote:
> On Wednesday, August 13, 2014 12:37:10 AM UTC-7, Steven Simpson wrote:
>> On 13/08/14 01:39, Knute Johnson wrote:
>>
>>> I'm having a problem getting the JPopupMenu from an Action that is
>>
>>> generated by the JPopupMenu. The error is a ClassCastException and it
>>
>>> is saying that what is returned by ActionEvent.getSource() cannot be
>>
>>> cast to a JPopupMenu because it is a JPopupMenu$1. JPopupMenu$1 is an
>>
>>> anonymous class and shouldn't be returned by ActionEvent.getSource().
>>
>>
>>
>> Why not? The JPopupMenu.add(Action) method creates a JMenuItem (of type
>>
>> JPopupMenu$1), adds it to the menu, and returns it. If you click that
>>
>> item, it will be the source.
>>
>>
>>
>>> In the end, I'm trying to get to the component that was right clicked
>>
>>> to pop up the menu.
>>
>>
>>
>> Couldn't see anything, but why not pass popupMenu as a constructor
>>
>> argument to someAction?
>>
>>
>
> Better yet, pass the target component (in your case, tf) as an argument
> to the someAction constructor.
>
Passing in the JComponent in this example works but not if the
JPopupMenu is going to be used on several components.
But Steven pointed me to the solution. I was thinking that the source
of the ActionEvent had to be the JPopupMenu but in fact it is the
JPopupMenu$1, that is really a JMenuItem, that is the source. From that
point it is easy to get the parent of the JMenuItem and then the
invoking JComponent that triggered the JPopupMenu.
I'm curious though, how did you figure out that the JPoupMenu$1 was the
JMenuItem?
Thanks again Steven.
knute...
C:\Users\Knute Johnson\com\knutejohnson\cookbook>java test
true
javax.swing.JPopupMenu[,0,0,99x26,invalid,layout=javax.swing.plaf.basic.DefaultM
enuLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.metal.MetalBorde
rs$PopupMenuBorder@5fa6820,flags=264,maximumSize=,minimumSize=,preferredSize=,de
siredLocationX=73,desiredLocationY=39,label=,lightWeightPopupEnabled=true,margin
=,paintBorder=true]
javax.swing.JTextField[,0,0,124x20,layout=javax.swing.plaf.basic.BasicTextUI$Upd
ateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResourc
e$CompoundBorderUIResource@29854cdd,flags=296,maximumSize=,minimumSize=,preferre
dSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextCol
or=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=java
x.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=s
un.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.Co
lorUIResource[r=184,g=207,b=229],columns=0,columnWidth=0,command=,horizontalAlig
nment=LEADING]
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JFrame {
final JPopupMenu popupMenu;
public test() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
popupMenu = new JPopupMenu();
popupMenu.add(new someAction());
JTextField tf = new JTextField("textField");
tf.setComponentPopupMenu(popupMenu);
add(tf,BorderLayout.CENTER);
pack();
setVisible(true);
}
public class someAction extends AbstractAction {
public someAction() {
putValue(NAME,"do something");
}
public void actionPerformed(ActionEvent ae) {
JComponent c = (JComponent)ae.getSource();
System.out.println(c instanceof JMenuItem);
System.out.println();
System.out.println(c.getParent());
System.out.println();
System.out.println(((JPopupMenu)c.getParent()).getInvoker());
}
}
public static void main(String... args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new test();
}
});
}
}
--
Knute Johnson
[toc] | [prev] | [next] | [standalone]
| From | Mike Amling <mamling@chaff.us> |
|---|---|
| Date | 2014-08-14 00:12 -0500 |
| Message-ID | <c52uq5F39kvU1@mid.individual.net> |
| In reply to | #29657 |
On 8/13/14 10:43 AM, Knute Johnson wrote:
> I'm curious though, how did you figure out that the JPoupMenu$1 was the
> JMenuItem?
$ javap 'javax.swing.JPopupMenu$1'
Compiled from "JPopupMenu.java"
class javax.swing.JPopupMenu$1 extends javax.swing.JMenuItem{
...
--Mike Amling
SSBkb24ndCBrbm93IGhvdyBhbnlvbmUgZWxzZSBkaWQgaXQu
[toc] | [prev] | [next] | [standalone]
| From | Steven Simpson <ss@domain.invalid> |
|---|---|
| Date | 2014-08-14 09:54 +0100 |
| Message-ID | <k2aubb-sf3.ln1@s.simpson148.btinternet.com> |
| In reply to | #29657 |
On 13/08/14 16:43, Knute Johnson wrote: > I'm curious though, how did you figure out that the JPoupMenu$1 was > the JMenuItem? The add(Action) documentation stated that some JMenuItem was created, added and returned, and that should have been the event source. The error reported that its type was JPopupMenu$1, therefore JPopupMenu$1 extends JMenuItem. The source code confirmed this. -- ss at comp dot lancs dot ac dot uk
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <eternal@knutejohnson.com> |
|---|---|
| Date | 2014-08-14 19:20 -0700 |
| Message-ID | <lsjqpj$bmu$1@dont-email.me> |
| In reply to | #29662 |
On 8/14/2014 01:54, Steven Simpson wrote: > On 13/08/14 16:43, Knute Johnson wrote: >> I'm curious though, how did you figure out that the JPoupMenu$1 was >> the JMenuItem? > > The add(Action) documentation stated that some JMenuItem was created, > added and returned, and that should have been the event source. The > error reported that its type was JPopupMenu$1, therefore JPopupMenu$1 > extends JMenuItem. The source code confirmed this. > > It appears obvious now but at the time :-) Thanks again for the pointers. -- Knute Johnson
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web