Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deselect default selection on JTextfield

When using JTextFields i like to set a default text.

Then i run the program and this default text will automatically be selected (at least when you have only one field). In other words, if I type a letter right away, the default text will be deleted and replaced by the new one.

My question is how I can change the default settings in a way that allows me to type a letter without deleting the default text? I would like the letter to just be added at the end of the default text.

Here's my code:

    public class ButtonsNText extends JPanel implements ActionListener, KeyListener {

private JTextField TextLine;
private JToggleButton UpperCaseButton;
private JToggleButton LowerCaseButton;
private JCheckBox ContinuousButton;
private ButtonGroup myButtonGroup;

public ButtonsNText(){
    TextLine = new JTextField(10);
    add(TextLine); TextLine.setName("TextLine");
    TextLine.setText("default text"); 
    TextLine.setCaretPosition(TextLine.getText().length());
    TextLine.addKeyListener(this);
    myButtonGroup = new ButtonGroup();
    UpperCaseButton = new JToggleButton("Upper case");
    add(UpperCaseButton);UpperCaseButton.setName("UpperCaseButton");
    LowerCaseButton = new JToggleButton("Lower case");
    add(LowerCaseButton); LowerCaseButton.setName("LowerCaseButton");
    ContinuousButton = new JCheckBox("Continuous?");
    add(ContinuousButton); ContinuousButton.setName("ContinuousButton");
    myButtonGroup.add(UpperCaseButton); myButtonGroup.add(LowerCaseButton);
    UpperCaseButton.addActionListener(this);
    LowerCaseButton.addActionListener(this);
    ContinuousButton.addActionListener(this);

}
public static void main(String[] args) {
    JFrame frame = new JFrame("Hello world example");
    frame.add(new ButtonsNText());
    frame.pack();
    frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource() == UpperCaseButton){
        TextLine.setText(TextLine.getText().toUpperCase());
    }
    else if(e.getSource() == LowerCaseButton){
        TextLine.setText(TextLine.getText().toLowerCase());
    }
}
@Override
public void keyReleased(KeyEvent k) {
    if(ContinuousButton.isSelected()){
        if(UpperCaseButton.isSelected()){
            int pos = TextLine.getCaretPosition();
            TextLine.setText(TextLine.getText().toUpperCase());
            TextLine.setCaretPosition(pos);
        }
        else if(LowerCaseButton.isSelected()){
            int pos = TextLine.getCaretPosition();
            TextLine.setText(TextLine.getText().toLowerCase());
            TextLine.setCaretPosition(pos);

        }
    }
    int key = k.getKeyCode();
    if(key == KeyEvent.VK_ENTER){
        if(UpperCaseButton.isSelected()){
            TextLine.setText(TextLine.getText().toUpperCase());
        }
        else if(LowerCaseButton.isSelected()){
            TextLine.setText(TextLine.getText().toLowerCase());
        }
    }
}

}

I have tried things like isFocusable(), setFocusable(), setCaterPosition() and other similar methods, but here I think I need a different approach.

like image 759
Ole-M Avatar asked Feb 09 '12 15:02

Ole-M


People also ask

How do you make a JTextField not editable?

In order to create a non editable JTextField , all you have to do is: Create a class that extends JFrame . Create a new JTextField . Use setEditable(false) that sets the specified boolean to indicate whether or not this textfield should be editable.

How do I disable JTextField?

TextField are editable by default. The code setEditable(false) makes the TextField uneditable. It is still selectable and the user can copy data from it, but the user cannot change the TextField's contents directly. The code setEnabled(false), disables this TextField.

How do I allow only numbers in JTextField?

By default, a JTextField can allow numbers, characters, and special characters. Validating user input that is typed into a JTextField can be difficult, especially if the input string must be converted to a numeric value such as an int. In the below example, JTextField only allows entering numeric values.

Which method is used to select all text in the JTextField?

selectAll() is what you need.


1 Answers

Just add one FocusListener for focus Gained, that will do for you along with tfield2.setCaretPosition(tfield2.getDocument().getLength()); Here see the code for your help :

import java.awt.event.*;

import javax.swing.*;

public class TextFieldExample extends JFrame
{
    public TextFieldExample()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel contentPane = new JPanel();
        JTextField tfield = new JTextField(10);
        final JTextField tfield2 = new JTextField(10);
        tfield2.setText("default text");
        tfield2.addFocusListener(new FocusListener()
        {
            public void focusGained(FocusEvent fe)
            {
                tfield2.setCaretPosition(tfield2.getDocument().getLength());
            }

            public void focusLost(FocusEvent fe)
            {
            }
        });

        contentPane.add(tfield);
        contentPane.add(tfield2);

        setContentPane(contentPane);

        pack();
        setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new TextFieldExample();
            }
        });
    }
}
like image 149
nIcE cOw Avatar answered Oct 13 '22 00:10

nIcE cOw