JTextField, how to display from specified postions

J

John_Woo

Hi,

let's say

JTextField f = new JTextField(100);

f.setText( "more than 100 letters..| <inside is 100 letters end");

in a case setSize(400,200), or supposed the wide-size for this f is
just 100-letters length.

what displayed is that, f displays the 100 letters which backford from
the end of the string,

like "inside is 100 letters end";

I'm wondering, how to make it display from a specified postion?

I've tried

javax.swing.BoundedRangeModel m = f.getHorizontalVisibility();
m.setValueIsAdjusting(true);
m.setValue(10); //display from postion 10 to the end.

but it changed nothing.
 
R

Roland de Ruiter

Hi,

let's say

JTextField f = new JTextField(100);

f.setText( "more than 100 letters..| <inside is 100 letters end");

in a case setSize(400,200), or supposed the wide-size for this f is
just 100-letters length.

what displayed is that, f displays the 100 letters which backford from
the end of the string,

like "inside is 100 letters end";

I'm wondering, how to make it display from a specified postion?

I've tried

javax.swing.BoundedRangeModel m = f.getHorizontalVisibility();
m.setValueIsAdjusting(true);
m.setValue(10); //display from postion 10 to the end.

but it changed nothing.

In these situations I've been using
f.setCaretPosition(0);
to "scroll" the field back to the beginning of the a very long text.

Of course setCaretPosition changes the position of the input caret. So
if you use f.setCaretPosition(10), a very long text just inserted into
the field would "scroll" back to the 10th character. Then when the user
types a character or digit on the keyboard (without using cursor keys
before), it will be inserted after the 10th character. Don't know if
this behavior/side-effect is what you want.
 
J

John_Woo

Roland said:
In these situations I've been using
f.setCaretPosition(0);
to "scroll" the field back to the beginning of the a very long text.

Of course setCaretPosition changes the position of the input caret. So
if you use f.setCaretPosition(10), a very long text just inserted into
the field would "scroll" back to the 10th character. Then when the user
types a character or digit on the keyboard (without using cursor keys
before), it will be inserted after the 10th character. Don't know if
this behavior/side-effect is what you want.

Thanks lots, it worked for me.

Can you tell why
javax.swing.BoundedRangeModel m = f.getHorizontalVisibility();
m.setValueIsAdjusting(true);
m.setValue(10); //display from postion 10 to the end.

not working?

John
 
R

Roland de Ruiter

Thanks lots, it worked for me.

Can you tell why
javax.swing.BoundedRangeModel m = f.getHorizontalVisibility();
m.setValueIsAdjusting(true);
m.setValue(10); //display from postion 10 to the end.

not working?

John
Seems to me a pixel offset rather than a character offset.
Method getHorizontalVisibility returns the same BoundedRangeModel
instance as the get/setScrollOffset methods are using. From the source
of JTextField:
public BoundedRangeModel getHorizontalVisibility() {
return visibility;
}

/**
* Gets the scroll offset, in pixels.
*
* @return the offset >= 0
*/
public int getScrollOffset() {
return visibility.getValue();
}
 
J

John_Woo

Roland said:
Seems to me a pixel offset rather than a character offset.
Method getHorizontalVisibility returns the same BoundedRangeModel
instance as the get/setScrollOffset methods are using. From the source
of JTextField:
public BoundedRangeModel getHorizontalVisibility() {
return visibility;
}

/**
* Gets the scroll offset, in pixels.
*
* @return the offset >= 0
*/
public int getScrollOffset() {
return visibility.getValue();
}

I changed by two ways:

A. f.setScrollOffset(10); //50, 100,1000, or
B. javax.swing.BoundedRangeModel m = f.getHorizontalVisibility();
m.setValueIsAdjusting(true);
m.setValue(10);//50,100,1000

the f.getScrollOffset() in both methods;
always return 0 and didn't display the first part of string

you know why?

Thanks
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top