tags:

views:

298

answers:

3

How can I create a JTextfield that can be queried but not updated?

+5  A: 
setEditable(false);

Using the above method on a JTextField will make it so that a user can't edit the text, but it can still be copied or you can pull the value with getText().

jjnguy
+1  A: 

A quick search turned up this example:

Create a read-only text field

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class ReadOnlyTextExample {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));


    // Create a read-only text field
    new Text(shell, SWT.READ_ONLY | SWT.BORDER).setText("Read Only");


    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}
Andrew Hare
+1 for the SWT sample.
kd304
-1 for SWT [Text != JTextField]
KitsuneYMG
A: 

Depends on the UI framework. Try setting

xx.setEnabled(false);

or

xx.setEditable(false);
Kosi2801
setting the text field to be disabled usually precludes copying from the field. I think the other answers have this covered better.
Paul Fisher
thanks for the hint, Paul
Kosi2801