Note that there are some explanatory texts on larger screens.

plurals
  1. PODeselect default selection on JTextfield
    text
    copied!<p>When using JTextFields i like to set a default text.</p> <p>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.</p> <p>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.</p> <p>Here's my code:</p> <pre><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()); } } } </code></pre> <p>}</p> <p>I have tried things like isFocusable(), setFocusable(), setCaterPosition() and other similar methods, but here I think I need a different approach. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload