Java Code Examples for javax.swing.JTextField#setSelectionEnd()

The following examples show how to use javax.swing.JTextField#setSelectionEnd() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RenameItemDialog.java    From sc2gears with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new RenameItemDialog.
 * @param node node to be renamed
 * @param file file associated with the node
 */
public RenameItemDialog( final DefaultMutableTreeNode node, final File file ) {
	super( "navigationTree.popup.renameDialog.title", (ImageIcon) null );
	
	final Box box = Box.createVerticalBox();
	box.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) );
	
	final String currentName = node.getUserObject().toString();
	box.add( new JLabel( Language.getText( "navigationTree.popup.renameDialog.enterNewNameFor", currentName ) ) );
	
	final JTextField newNameTextField = new JTextField( currentName );
	newNameTextField.setSelectionStart( 0 );
	newNameTextField.setSelectionEnd( currentName.length() );
	box.add( newNameTextField );
	
	getContentPane().add( box, BorderLayout.CENTER );
	
	final JPanel buttonsPanel = new JPanel();
	final JButton renameButton = new JButton();
	GuiUtils.updateButtonText( renameButton, "navigationTree.popup.renameDialog.renameButton" );
	renameButton.addActionListener( new ActionListener() {
		@Override
		public void actionPerformed( final ActionEvent event ) {
			dispose();
			if ( !file.renameTo( new File( file.getParent(), newNameTextField.getText() + GeneralUtils.getFileExtension( file ) ) ) )
				GuiUtils.showErrorDialog( Language.getText( "navigationTree.popup.renameDialog.failedToRename", currentName, newNameTextField.getText() ) );
			else
				MainFrame.INSTANCE.refreshNavigationTree();
		}
	} );
	buttonsPanel.add( renameButton );
	final JButton cancelButton = createCloseButton( "button.cancel" );
	buttonsPanel.add( cancelButton );
	getContentPane().add( GuiUtils.wrapInPanel( buttonsPanel ), BorderLayout.SOUTH );
	
	packAndShow( newNameTextField, false );
}