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
Solved

Control a JScrollpane  with keyboards

Posted on 2004-04-07
17
442 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 62
hibernate insert example 13 34
How to configure empty element in XML Document parser? 15 36
Bot application - advice 3 38
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

840 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