?
Solved

Control a JScrollpane  with keyboards

Posted on 2004-04-07
17
Medium Priority
?
449 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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 1000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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…
Suggested Courses
Course of the Month15 days, 3 hours left to enroll

771 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