The attached code produces a popup menu in a simple JFrame. It works and
seems straight forward.
When I modify the code to add a JTextArea, the right-click causes an
error.
I commented all lines that I added with: # Modification
The line that is presently commented out will cause the error when
uncommented.
What do I need to change to get the Right-Click Popup Menu to work with
the JTextArea?
Well JTextArea almost certainly has its own event handling…do you
want the right clicks to go to the JTextArea or only to the main
window?
- Charlie
Ahh yes that’s the problem.
Your event handler assumes that the target of the event will always
have a getMenu method. JTextArea does not have such a method, so when
you right click on JTextArea, it inherits the right-click handling
from the main window, and the event is sent with your JTextArea as the
target of the event.
What do you want to happen?
- Charlie
On Thu, Jul 21, 2011 at 6:03 PM, Charles Oliver N.
Hi Charlie,
Thanks for the reply. For some reason, I’m not getting email notices of
activity on this posting, so I only just now saw your responses.
What I want is a simple window displaying HTML text. For the example
that I supplied with my original posting, I used the a JTextArea, but as
you will see in the newly attached code, I actually use a JEditorPane.
When I right-click in the text area, I want a popup menu to be
displayed, allowing me to select some menu options.
I see from your post that the JTextArea does not handle a getMenu
method, which is what JRuby’s error message indicated. The following
error message shows that it also does not work for the JEditorPane.
Exception in thread “AWT-EventQueue-0”
org.jruby.exceptions.RaiseException: (NoMethodError) undefined method
`get_menu’ for #Java::JavaxSwing::JEditorPane:0x5290288e
What must I do to be able to get a popup menu in my JEditorPane?
A second issue that I noticed, is that the event always replies “false”
to the “isPopupTrigger” question, regardless which button I click
(Ubuntu 11.04).
puts e.popup_trigger?
The attachment, One.rb is my “experiment with it” file, so it is a bit
of a mess.
By the way, I bought your book “Using JRuby” when it first came out and
that got me started with my change-over from MRI Ruby + wxRuby, to JRuby
- Swing.
Thanks for your help!
Gary
On Fri, Jul 22, 2011 at 9:51 AM, Gary H. [email protected]
wrote:
I see from your post that the JTextArea does not handle a getMenu
method, which is what JRuby’s error message indicated. The following
error message shows that it also does not work for the JEditorPane.Exception in thread “AWT-EventQueue-0”
org.jruby.exceptions.RaiseException: (NoMethodError) undefined method
`get_menu’ for #Java::JavaxSwing::JEditorPane:0x5290288eWhat must I do to be able to get a popup menu in my JEditorPane?
I think associating a menu with the Window itself doesn’t help with
child components. What you probably want to do is register a listener
on the editor pane and/or text pane that just holds a reference to the
menu and can start it up, like Sébastien suggests.
FWIW, this kinda gets to be a Swing question, and I’m not a Swing
expert, so there’s probably a “standard” way that people handle this
sort of thing.
A second issue that I noticed, is that the event always replies “false”
to the “isPopupTrigger” question, regardless which button I click
(Ubuntu 11.04).
puts e.popup_trigger?
I’m not sure about this one. I’m not familiar with that property.
The attachment, One.rb is my “experiment with it” file, so it is a bit
of a mess.By the way, I bought your book “Using JRuby” when it first came out and
that got me started with my change-over from MRI Ruby + wxRuby, to JRuby
- Swing.
Well, that’s great! I hope we’re able to help you through your early
issues here. You may also be interested in some of the Swing or SWT
wrapper libraries available for JRuby. There’s at least a partial list
of them here: GUIFrameworks · jruby/jruby Wiki · GitHub
- Charlie
Hi Sébastien,
Your code solved my problem! Thank you so much.
Charlie, Thanks for your help as well!
Regarding the “.isPopupTrigger()” issue that I also mentioned, I found
that the issue was that it was reporting “false” because the mouse
button has been released. In order to get a meaningful answer to that
“question”, you have to use the “mousePressed” alternative, instead of
the “mouseReleased” alternative, for detecting the right-click. The code
below works correctly.
Note that this forum’s display format has caused the
“e.isPopupTrigger()” to be displayed on the next line, but in the
program, it is on the same line as the “if”.
class MouseAction < MouseAdapter
attr_accessor :popupMenu
def mousePressed e
@popupMenu.show(e.getComponent, e.getX, e.getY) if
e.isPopupTrigger()
end
end
Thanks again!
Gary
Hi Gary,
The source of the event was previously Example, which does have a
getMenu method returning your popup menu. Now that the source is
JTextArea, getMenu will not be found.
You could define the popup menu as an instance var to your MouseAction:
class MouseAction < MouseAdapter
attr_accessor :popupMenu
def mouseReleased e
if e.getButton == e.button:
@popupMenu.show(e.getComponent, e.getX, e.getY)
end
end
end
and then set the popup menu before adding the listener to the textarea:
display = JTextArea.new
mouseAction = MouseAction.new
mouseAction.popupMenu = @menu
display.addMouseListener(mouseAction)
self.add display
Regards,
Sébastien.