Java showInputDialog select custom text

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Java showInputDialog select custom text



I have rename dialog for rename file


String renameTo = JOptionPane.showInputDialog(gui, "New Name", currentFile.getName());



it works this way, but I have a problem.
the problem is that I set the default value with the extension of the file
but I just want the file name to be selected.


sample : my file name = yusuf.png



I want select only yusuf like;



like this





You can't control the JTextField used by JOptionPane, you'll need to role your own implementation. You could get started by having a look at JTextComponent#setSelectionStart and JTextComponent#setSelectionEnd
– MadProgrammer
Aug 9 at 22:49



JTextField


JOptionPane


JTextComponent#setSelectionStart


JTextComponent#setSelectionEnd





"Easiest" is subjective - besides, why would we show you the hardest way to do something ... we're lazy after all ;)
– MadProgrammer
Aug 9 at 22:51




1 Answer
1



There is a lot going on inside JOptionPane, it's one of the things that makes it so powerful, it also makes it a little inflexible to.


JOptionPane



Two immediate problems are apparent...


JTextField


JOptionPane



Setting up the JTextField is actually straight forward...


JTextField


String text = "yusuf.png";
int endIndex = text.lastIndexOf(".");

JTextField field = new JTextField(text, 20);
if (endIndex > 0)
field.setSelectionStart(0);
field.setSelectionEnd(endIndex);
else
field.selectAll();



This will basically select all the text from the start of the String up to the last . or all the text if no . can be found.


String


.


.



The difficult part now is taking back focus control from the JOptionPane


JOptionPane


// Make a basic JOptionPane instance
JOptionPane pane = new JOptionPane(field,
JOptionPane.PLAIN_MESSAGE,
JOptionPane.OK_CANCEL_OPTION,
null);
// Use it's own dialog creation process, it's simpler this way
JDialog dialog = pane.createDialog("Rename");
// When the window is displayed, we want to "steal"
// focus from what the `JOptionPane` has set
// and apply it to our text field
dialog.addWindowListener(new WindowAdapter()
@Override
public void windowActivated(WindowEvent e)
// Set a small "delayed" action
// to occur at some point in the future...
// This way we can circumvent the JOptionPane's
// focus control
SwingUtilities.invokeLater(new Runnable()
@Override
public void run()
field.requestFocusInWindow();

);

);
// Put it on the screen...
dialog.setVisible(true);
dialog.dispose();
// Get the resulting action (what button was activated)
Object value = pane.getValue();
if (value instanceof Integer)
int result = (int)value;
// OK was actioned, get the new name
if (result == JOptionPane.OK_OPTION)
String newName = field.getText();
System.out.println("newName = " + newName);




And, crossing our fingers, we end up with something looking like...



Example



Personally, I'd wrap this up in a nice reusable class/method call which returned the new text or null based on the action of the user, but that's me


null



Isn't there an easier way?



Of course, I just like showing you the most difficult solution possible ... 😳 (sarcasm) ... it's kind of why I suggested wrapping it up in it's own utility class, so you can re-use it later 😉






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard