Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to add a traditional popup edit menu to JTextComponent but can't figure out how to do paste.  RSS feed

 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally, I was doing a JTextArea with the popup menu and then decided to do an extended JTextField with the same thing to save time on other things.

However, I found that by passing a JTextComponent object to a method like

public JTextComponent addPopupMenu(JTextComponent jtc)
{

.........

return jtc;

}


I could basically settle for both, though, admittedly, I have several options, some of which have issues.

The thing is, I was somehow able to get a PopupMenu to work for everything when I did it in the JTextArea. However, as I tried to do a textArea.getTransferHandler().getPasteAction() to add an ActionListener to a JMenuItem in a Notepad class, it was working. However, when I click that JMenuItem, it does nothing, though the keyboard shortcut does, and, also, so does my PopupMenu paste.

However, when I tried to do it directly in the JTextField without bothering with any JMenuItems then it said that I couldn't cast MenuItem to Component.

I have

paste = new MenuItem("Paste");

paste.addActionListener(tf.getTransferHandler().getPasteAction());

and it gave me that error message.

Also, I might point out, that, though it kinda worked when I did it with the JTextArea, it gave me a "non-serious" error message about a DataFlavor or something. It wasn't in green, which is normally what there is if there is a big error. I know it's associated with the paste action. Perhaps I shouldn't mess with the text area's transfer handler directly perhaps. Who knows?


When I got mad at the JTextField one and did a JPopupMenu, it now does the stuff, but won't do anything, no errors but no paste either, when I do paste, plus, I can't figure out how to set it relative to the parent that holds the text field, and I sometimes get a green error message.


I was wondering, how do I get the paste to work properly?


I preferably would like a PopupMenu as it's nicer looking and closes when it isn't being clicked on (something a JPopupMenu won't do by default.)

 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Though now I need help getting it to show up in the right spot for the JTextField, I can change it to JTextComponent later, and also close when not selected? Do I need a MouseListener or a FocusListener?

To close the JPopupMenu i mean without needing an Exit Button to close it?
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The popup will close when you select an item or click somewhere else in the window.
 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
The popup will close when you select an item or click somewhere else in the window.


Ok, how do I tell it where to appear? SetVisible(true) just puts it whereever in the backgorund and it can be lost behind other windows.

show(Component, int, int), which kinda works, though I think it may be getting deprecated, but anyway, it can throw errors if you double click while its already up in the MouseListener to get the popup to show. Using getParent() doesn't seem to work either.
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, how do I tell it where to appear?


You don't do anything (ie. you don't use a MouseListener). The popup will display automatically for you. That is the benefit of using the setComponentPopupMenu() method.
 
Paul Adcock
Ranch Hand
Posts: 48
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
Ok, how do I tell it where to appear?


You don't do anything (ie. you don't use a MouseListener). The popup will display automatically for you. That is the benefit of using the setComponentPopupMenu() method.



I don't want it to always display, only when I right click the JTextComponent. Also, there's no such method in JTextComponent. I can only apply that to a JTextField. How would I apply it to JTextComponent so I could use it in both a JTextArea and a JTextField?

Oh duh, it's in JComponent, not JTextComponent. That's why I couldn't find it. What you said will work. Thanks.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!