JTextField: text is scrolled to the right when the text is too large
Hi,
I have a JTextField with a text value that is too large to be displayed for the current size. The field is enabled but not editable.
It seems impossible to show the start of the text, I've tried to set the caret position, scrolloffset, inserting an empty string at position 0 in the document to force positioning the field to the left.
Whatever I do, the end keeps being visible, while the start remains hidden.
Please find below a textual representation of the problem and how I would like to see it.
Problem]
This is the string I would like to display, which is far too long
[] = JTextField
I would like it to display]
This is the string I would like to display, which is far too long
[] = JTextField
Thanks in advance,
Jeroen Muis
[821 byte] By [
jermuia] at [2007-10-3 9:20:43]

> Unfortunately not....
>
> I exactly tried that specific call.
Works fine for me:
public static void createAndShowGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField text = new JTextField(5);
text.setText("1234567890123456790");
text.setCaretPosition(0);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(text);
frame.pack();
frame.setVisible(true);
}
You must be doing something else in your code that is messing it up.
Yes, setCaretPosition(0) has always worked for me as well.
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.
Thanks for your quick replies and your sample (which I should have tried before, I admit ... I was so lost in this, I just could not thing clear anymore I guess... )
I did find the problem / solution for it. I just want to share it, so others (and maybe you as well) do not find themselves trapped in a situation like this in the future)
The problem was caused due to a focus event. It automatically sets the caret when the focus is gained. (even though the field is not editable !)
Before the troubled field received focus the previous field in the focus cycle is set and this field sets text in the troubled field when the focused is lost. Because I typically use the keyboard to navigate between controls the next field which gets focus is (surprise surprise) the field we are discussing and Swing sets the caret position....
Once again, thanks !
I don't believe it changes the caret position when the component gains focus. It justs paints the caret so you can see where it currently is in the text field. So it you set your text on the field and then set the caret to the start it should appear at the start every time you tab to the field.
If you use you mouse to click on the text field, then the caret position will change to where ever you clicked.
This is the behaviour of an editable text field. Don't know if its any different for non-editable.