पर डिफ़ॉल्ट चयन को अचयनित करें JTextFields का उपयोग करते समय मैं एक डिफ़ॉल्ट पाठ सेट करना चाहता हूं।JTextfield
फिर मैं प्रोग्राम चलाता हूं और यह डिफ़ॉल्ट पाठ स्वचालित रूप से चुना जाएगा (कम से कम जब आपके पास केवल एक फ़ील्ड है)। दूसरे शब्दों में, यदि मैं तुरंत एक पत्र टाइप करता हूं, तो डिफ़ॉल्ट टेक्स्ट हटा दिया जाएगा और नए द्वारा प्रतिस्थापित किया जाएगा।
मेरा सवाल यह है कि मैं डिफ़ॉल्ट सेटिंग्स को इस तरह से कैसे बदल सकता हूं जो मुझे डिफ़ॉल्ट पाठ को हटाए बिना एक अक्षर टाइप करने की अनुमति देता है? मैं पत्र को डिफ़ॉल्ट पाठ के अंत में अभी जोड़ा जाना चाहूंगा।
यहाँ मेरी कोड है:
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());
}
}
}
}
मैं isFocusable(), setFocusable(), setCaterPosition() और अन्य समान तरीकों जैसी चीजों की कोशिश की है, लेकिन यहाँ मुझे लगता है कि मैं एक अलग दृष्टिकोण की जरूरत है ।
बेहतर मदद के लिए जल्दी ही, एक [SSCCE] (http://sscce.org/) पोस्ट करते हैं। –
कृपया जावा नामकरण सम्मेलनों को सीखें और उन्हें – kleopatra
पर चिपकाएं ... और _don't_ keylisteners का उपयोग करें – kleopatra