Solved

Control a JScrollpane  with keyboards

Posted on 2004-04-07
17
439 Views
Last Modified: 2008-02-01
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.


0
Comment
Question by:RobCSP
  • 10
  • 3
17 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 10773431
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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10773479
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10773482
Hi RobCSP,

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

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10773524
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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10773547
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
0
 

Author Comment

by:RobCSP
ID: 10773639
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


 

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10773666
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:RobCSP
ID: 10773697
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
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10773732
Try setting the unit-increment with jsp.getVerticalScrollBar ().setUnitIncrement () before you display the Container.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10773783
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?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10773855
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.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 250 total points
ID: 10773895
0
 

Author Comment

by:RobCSP
ID: 10774835
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.

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10780793
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Printing to a specific printer tray (HP 806dn printer) 3 83
Python Assistance 7 73
web services creation SOAP vs REST 5 20
Configure a Bean in an XML file 4 12
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now