Link to home
Start Free TrialLog in
Avatar of RobCSP
RobCSP

asked on

Control a JScrollpane with keyboards

Hi experts,

I have a JTextarea,inside a JscrollPane, the content exceeds the size of the area, for what I want that the user could see the whole content without need to use the mouse, that is to say, that could move the scroll by means of the keyboard.


Avatar of Mayank S
Mayank S
Flag of India image

Shall we assume that the JTextArea is ineditable? Because otherwise, you can use the Pg Down or the down-arrow buttons to go to the bottom of the text-area if the cursor is inside it.
Avatar of Webstorm
Webstorm

Hi RobCSP,

>> I have a JTextarea,inside a JscrollPane
Why do you need to put the JTextArea inside a JScrollPane ? JTextArea already have scrollbars.

The java.awt.TextArea internally handles scrolling. JTextArea is different in that it doesn't manage scrolling, but implements the swing Scrollable interface. This allows it to be placed inside a JScrollPane if scrolling behavior is desired, and used directly if scrolling is not desired.
You just need to set teh focus to the JScrollPane for doing that.

JScrollPane jsp = new JScrollPane ( t ) ; // t is an ineditable JTextArea
getContentPane ().add ( jsp ) ;
show () ;
jsp.requestFocus () ; // sets the focus to the JScrollPane

- then, you can try scrolling up and down with the Up/ Down arrow keys
Avatar of RobCSP

ASKER

Shall we assume that the JTextArea is ineditable? Yes

JTextArea already have scrollbars.
Are you sure???  

Keystroke-mappings are available for JScrollPane: I know that,but in this way you move the cursor, ok when you move the cursor,the scroll moves, but it produces strange jumps, that´s the reason why my boss(not me) want that the user control the scroll with the key arrows.

Can i add a keyListener to the JScrollPane and control the area displayed by it????

i come back in 2 hours thnx for the help


 

You don't need to add any key-listener. Try out the requestFocus () example that I posted. Should work (works for me). JTextArea does not manage scrolling on its own, unlike TextArea. That's why it implements Scrollable, so that you can add it to a JScrollPane if scrolling is desired.
Avatar of RobCSP

ASKER

I try it, ok the  scroll works but this is not what i want because the jumps still happening, i want to control directly the scroll.

Test with a text that exceed the text area horizontal and vertically and you will see my problem.

thnx
Try setting the unit-increment with jsp.getVerticalScrollBar ().setUnitIncrement () before you display the Container.
Is my understanding of 'jump' correct? Do you mean that when you press the down-key, it scrolls a long way down the JTextArea? Is that correct?
By the way, there is a significant difference in the amount it scrolls down if you use the Down button and the Page-Down button.
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RobCSP

ASKER

Hi, i try with setUnitIncrement but the problem persist, my text area display text obtained from a text file

/************text example*********/

\n\n\n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\nOPERACION              CONT                   TIPO                   ANUL                   MONEDA                                        CAMBIO                 CANTIDAD               EUROS                  NOMBRE                 DNI                    RESIDENCIA                                    FECHA                  USUARIO                \n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n67                                            VENTA DE BILLETES      SI                                                                                                                                        wqrwe                  wqeqw                  qweqe qweqeqw eqeq                            2004_04_06  13:30:13   Rob                    \n                                                                                            DOLAR USA                                     768                    0,8046408296           954.46                 \n\n\n\n\n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\nOPERACION              CONT                   TIPO                   ANUL                   MONEDA                                        CAMBIO                 CANTIDAD               EUROS                  NOMBRE                 DNI                    RESIDENCIA                                    FECHA                  USUARIO                \n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n68                                            VENTA DE BILLETES      SI                                                                                                                                        wqrwe                  wqeqw                  qweqe qweqeqw eqeq                            2004_04_06  13:30:13   Rob                    \n                                                                                            DOLAR USA                                     768                    0,8046408296           954.46                 \n\n\n\n\n\n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\nOPERACION              CONT                   TIPO                   ANUL                   MONEDA                                        CAMBIO                 CANTIDAD               EUROS                  NOMBRE                 DNI                    RESIDENCIA                                    FECHA                  USUARIO                \n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n69                                            VENTA DE BILLETES      SI                                                                                                                                        wqrwe                  wqeqw                  qweqe qweqeqw eqeq                            2004_04_06  13:30:13   Rob                    \n                                                                                            DOLAR USA                                     768                    0,8046408296           954.46                 \n\n\n\n\n\n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\nOPERACION              CONT                   TIPO                   ANUL                   MONEDA                                        CAMBIO                 CANTIDAD               EUROS                  NOMBRE                 DNI                    RESIDENCIA                                    FECHA                  USUARIO                \n--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n70                                            VENTA DE BILLETES      SI                                                                                                                                        wqrwe                  wqeqw                  qweqe qweqeqw eqeq                            2004_04_06  13:30:13   Rob                    \n                                                                                            DOLAR USA                                     768                    0,8046408296           954.46                 \n





/************ end of text example************/


Because of the blank lines, if i move the cursor the "jumps" begin, that the reason why i want to know if i can control directly the displayed area of a JscrollPane.

i see the links, but i don´t find it useful.

Well, the setUnitIncrement () method is perhaps meant for that purpose only. There is also one setBlockIncrement () but I don't know what it does - never tried it. Try printing the value of unit-increment by getUnitIncrement (). Does it give something close to 10? Try setting it to 1.