0

When I open a JFileChooser to choose a file/directory, I know that you can use CTRL-V to paste the file path into the textfield. Alternatively, is there a way to enable a right-click in a JFileChooser to show the usual context menu for Cut/Copy/Paste so I can just right-click and paste the file path into the textfield?

Edit: Ok. I just used the Swing Utils class and received this for the value:

Jtextfield = javax.swing.plaf.metal.MetalFileChooserUI$3[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@4fe7ab59,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=35,columnWidth=0,command=,horizontalAlignment=LEADING]

I'm not exactly sure where to go from here though.. How would I even apply the context menu to this value?

  • Several approaches to adding a chooser component are shown in this possible [duplicate](http://stackoverflow.com/q/25538213/230513). – trashgod Oct 03 '14 at 14:58
  • @trashgod - This isn't necessarily a chooser component.. This is just a right-click context menu for the text field to enable Cut/Copy/Paste for a file path. – angeltari11684 Oct 03 '14 at 15:11
  • @angeltari11684, JTextField does does not support a cut/copy/paste context menu. You would need to create that yourself. You would then need to add the ment to the text field used by the file chooser. To find the text field you could use the [Swing Utils](http://tips4java.wordpress.com/2008/11/13/swing-utils/) class. – camickr Oct 03 '14 at 15:21

1 Answers1

2

How would I even apply the context menu to this value?

The same way you add a popup menu to any component.

In the case of cut/copy/paste you can take advantage of the Actions provided by the DefaultEditorKit. For example:

JPopupMenu menu = new JPopupMenu();
Action cut = new DefaultEditorKit.CutAction();
cut.putValue(Action.NAME, "Cut");
menu.add( cut );
textField.setComponentPopupMenu( menu );
camickr
  • 321,443
  • 19
  • 166
  • 288