JTextField, how to display from specified postions

Discussion in 'Java' started by John_Woo, Aug 21, 2006.

  1. John_Woo

    John_Woo Guest

    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.

    --
    Thanks
    John
    Toronto
    John_Woo, Aug 21, 2006
    #1
    1. Advertising

  2. On 21-8-2006 20:49, John_Woo wrote:
    > 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.
    >
    > --
    > Thanks
    > John
    > Toronto
    >


    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.
    --
    Regards,

    Roland
    Roland de Ruiter, Aug 21, 2006
    #2
    1. Advertising

  3. John_Woo

    John_Woo Guest

    Roland de Ruiter wrote:
    > On 21-8-2006 20:49, John_Woo wrote:
    > > 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.
    > >
    > > --
    > > Thanks
    > > John
    > > Toronto
    > >

    >
    > 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.
    > --
    > Regards,
    >
    > Roland


    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
    John_Woo, Aug 21, 2006
    #3
  4. On 21-8-2006 21:41, John_Woo wrote:
    > Roland de Ruiter wrote:
    >> On 21-8-2006 20:49, John_Woo wrote:
    >>> 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.
    >>>
    >>> --
    >>> Thanks
    >>> John
    >>> Toronto
    >>>

    >> 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.
    >> --
    >> Regards,
    >>
    >> Roland

    >
    > 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();
    }
    --
    Regards,

    Roland
    Roland de Ruiter, Aug 21, 2006
    #4
  5. John_Woo

    John_Woo Guest

    Roland de Ruiter wrote:
    > On 21-8-2006 21:41, John_Woo wrote:
    > > Roland de Ruiter wrote:
    > >> On 21-8-2006 20:49, John_Woo wrote:
    > >>> 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.
    > >>>
    > >>> --
    > >>> Thanks
    > >>> John
    > >>> Toronto
    > >>>
    > >> 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.
    > >> --
    > >> Regards,
    > >>
    > >> Roland

    > >
    > > 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();
    > }
    > --
    > Regards,
    >
    > Roland


    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
    John_Woo, Aug 21, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter

    JTextField JPassword

    Peter, Jul 11, 2003, in forum: Java
    Replies:
    0
    Views:
    3,305
    Peter
    Jul 11, 2003
  2. me
    Replies:
    2
    Views:
    4,499
    Sudsy
    Jul 21, 2003
  3. Andrew Tucker

    JTable + JTextField

    Andrew Tucker, Jul 17, 2003, in forum: Java
    Replies:
    0
    Views:
    748
    Andrew Tucker
    Jul 17, 2003
  4. =?ISO-8859-1?Q?Peter_Valdemar_M=F8rch?=
    Replies:
    2
    Views:
    847
    =?ISO-8859-1?Q?Peter_Valdemar_M=F8rch?=
    Jul 23, 2003
  5. Replies:
    5
    Views:
    3,022
    Andrew Thompson
    Dec 26, 2005
Loading...

Share This Page