How can I create a JTextfield
that can be queried but not updated?
views:
298answers:
3
+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
2009-06-25 15:22:17
+1
A:
A quick search turned up this example:
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
2009-06-25 15:22:40
+1 for the SWT sample.
kd304
2009-06-25 15:23:48
-1 for SWT [Text != JTextField]
KitsuneYMG
2009-06-26 12:12:06
A:
Depends on the UI framework. Try setting
xx.setEnabled(false);
or
xx.setEditable(false);
Kosi2801
2009-06-25 15:25:54
setting the text field to be disabled usually precludes copying from the field. I think the other answers have this covered better.
Paul Fisher
2009-06-25 15:27:40