Make JTextField not editable in Java

Description

The following code shows how to make JTextField not editable.

Example


/* w  w  w  .j a  va 2  s. c o m*/
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class Main {
  public static void main(String[] args) {
    JFrame f = new JFrame("Editability Example");

    String firstFieldText = "An editable text field";
    JTextField firstField = new JTextField(firstFieldText, 20);

    f.setLayout(new FlowLayout());
    f.getContentPane().add(firstField);

    firstField.setEditable(false);

    f.pack();
    f.setVisible(true);

  }

}

The code above generates the following result.

Make JTextField not editable in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer