Deselect default selection on JTextfield
Asked Answered
T

3

7

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.

Tanberg answered 9/2, 2012 at 15:26 Comment(3)
For better help sooner, post an SSCCE.Kevel
... and don't use keylistenersAvelinaaveline
@Tanberg : Why you writing if(key == KeyEvent.VK_ENTER) when you had made one ActionListener for your JTextField. Stop using KeyEvents with Swing, KEY BINDING is to be used with Swing.Tenuis
T
4

for @Pete and will be deleted

import java.awt.*;
import javax.swing.*;
import javax.swing.text.DefaultCaret;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

public class TestTextComponents extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTextField jTextField1;
    private JTextField jTextField2;

    public TestTextComponents() {
        initComponents();
    }

    private void initComponents() {
        jTextField1 = new JTextField();
        jTextField2 = new JTextField();
        getContentPane().setLayout(new FlowLayout());
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("Text component persistent selection");
        setResizable(false);
        getContentPane().add(new JLabel(
                "Please skip between text fields and watch persistent selection: "));
        jTextField1.setText("jTextField1");
        getContentPane().add(jTextField1);
        jTextField2.setText("jTextField2");
        getContentPane().add(jTextField2);
        jTextField1.setCaret(new HighlightCaret());
        jTextField2.setCaret(new HighlightCaret());
        //Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
       // setBounds((screenSize.width - 600) / 2, (screenSize.height - 70) / 2, 600, 70);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestTextComponents().setVisible(true);
            }
        });
    }
}

class HighlightCaret extends DefaultCaret {

    private static final Highlighter.HighlightPainter unfocusedPainter =
            new DefaultHighlighter.DefaultHighlightPainter(new Color(230, 230, 210));
    private static final long serialVersionUID = 1L;
    private boolean isFocused;

    @Override
    protected Highlighter.HighlightPainter getSelectionPainter() {
        return isFocused ? super.getSelectionPainter() : unfocusedPainter;
    }

    @Override
    public void setSelectionVisible(boolean hasFocus) {
        if (hasFocus != isFocused) {
            isFocused = hasFocus;
            super.setSelectionVisible(false);
            super.setSelectionVisible(true);
        }
    }
}
Tale answered 9/2, 2012 at 16:54 Comment(4)
This is very very nice. Please don't delete this answer! /PetePeay
@Tale : Congrats for reaching a milestone of 50K + :-)Tenuis
@Gagandeep Bali thanks, I'm stayed here (from 45k+) only as editor and up_voter :-), in 99pct of *** I'm leaving questions without my answerTale
@Tale : Ahha, your inputs are always so good from knowledge perspective, Hope to see you answering soon like before :-)Tenuis
T
6

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();
            }
        });
    }
}
Tenuis answered 9/2, 2012 at 16:9 Comment(1)
+1 for using getDocument().getLength() instead of getText().length().Stalingrad
H
4

How about if you moved the caret to the end?

txt.setCaretPosition(txt.getText().length());
Hemicycle answered 9/2, 2012 at 15:31 Comment(2)
"I have tried that" Try posting an SSCCE that shows us your best attempt. After all, we are not mind readers.Kevel
Use txt.getDocument().getLength() instead of txt.getText().length().Stalingrad
T
4

for @Pete and will be deleted

import java.awt.*;
import javax.swing.*;
import javax.swing.text.DefaultCaret;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;

public class TestTextComponents extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTextField jTextField1;
    private JTextField jTextField2;

    public TestTextComponents() {
        initComponents();
    }

    private void initComponents() {
        jTextField1 = new JTextField();
        jTextField2 = new JTextField();
        getContentPane().setLayout(new FlowLayout());
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("Text component persistent selection");
        setResizable(false);
        getContentPane().add(new JLabel(
                "Please skip between text fields and watch persistent selection: "));
        jTextField1.setText("jTextField1");
        getContentPane().add(jTextField1);
        jTextField2.setText("jTextField2");
        getContentPane().add(jTextField2);
        jTextField1.setCaret(new HighlightCaret());
        jTextField2.setCaret(new HighlightCaret());
        //Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
       // setBounds((screenSize.width - 600) / 2, (screenSize.height - 70) / 2, 600, 70);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestTextComponents().setVisible(true);
            }
        });
    }
}

class HighlightCaret extends DefaultCaret {

    private static final Highlighter.HighlightPainter unfocusedPainter =
            new DefaultHighlighter.DefaultHighlightPainter(new Color(230, 230, 210));
    private static final long serialVersionUID = 1L;
    private boolean isFocused;

    @Override
    protected Highlighter.HighlightPainter getSelectionPainter() {
        return isFocused ? super.getSelectionPainter() : unfocusedPainter;
    }

    @Override
    public void setSelectionVisible(boolean hasFocus) {
        if (hasFocus != isFocused) {
            isFocused = hasFocus;
            super.setSelectionVisible(false);
            super.setSelectionVisible(true);
        }
    }
}
Tale answered 9/2, 2012 at 16:54 Comment(4)
This is very very nice. Please don't delete this answer! /PetePeay
@Tale : Congrats for reaching a milestone of 50K + :-)Tenuis
@Gagandeep Bali thanks, I'm stayed here (from 45k+) only as editor and up_voter :-), in 99pct of *** I'm leaving questions without my answerTale
@Tale : Ahha, your inputs are always so good from knowledge perspective, Hope to see you answering soon like before :-)Tenuis

© 2022 - 2024 — McMap. All rights reserved.