
simple program for text field cannot not be edited in java swing

import javax.swing.*;
import java.awt.event.*;
class TextFieldExample{
public static void main(String[] args){
JFrame f=new JFrame();
JTextField text=new JTextField(20);
text.setText("Hello World!");
text.setEditable(false);
text.setBounds(10,10,150,20);
f.setLayout(null);
f.add(text);
f.setVisible(true);
f.setSize(300,100);
}
}

import javax.swing.*;
import java.awt.event.*;
class TextFieldExample{
public static void main(String[] args){
JFrame f=new JFrame();
final JTextField text=new JTextField(20);
text.addKeyListener(new KeyAdapter(){
public void keyReleased(KeyEvent keyEvent) {
if((text.getText()).equals("")){
text.setEditable(true);
}
else{
text.setEditable(false);
}
}
});
text.setBounds(10,10,150,20);
f.setLayout(null);
f.add(text);
f.setVisible(true);
f.setSize(300,100);
}
}

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Form extends JFrame
{
JButton ADD;
JPanel panel;
JLabel label1,label2;
final JTextField text1,text2;;
Form(){
label1 = new JLabel();
label1.setText("Name:");
text1 = new JTextField(20);
label2 = new JLabel();
label2.setText("Address:");
text2 = new JTextField(20);
ADD=new JButton("View");
panel=new JPanel(new GridLayout(3,2));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(ADD);
add(panel);
setTitle("FORM");
ADD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value1=text1.getText();
String value2=text2.getText();
JFrame f=new JFrame("View Data");
JPanel p=new JPanel(new GridLayout(2,2));
JLabel l1=new JLabel("Name");
JTextField t1 = new JTextField(20);
JLabel l2=new JLabel("Address");
JTextField t2 = new JTextField(20);
t1.setText(value1);
t1.setEditable(false);
t2.setText(value2);
t2.setEditable(false);
p.add(l1);
p.add(t1);
p.add(l2);
p.add(t2);
f.add(p);
f.setVisible(true);
f.pack();
}
});
}
}
class FormDemo
{
public static void main(String arg[])
{
try
{
Form frame=new Form();
frame.pack();
frame.setVisible(true);
}
catch(Exception e)
{
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.