Solved

Control a JScrollpane  with keyboards

Posted on 2004-04-07
17
438 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:mayankeagle
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:mayankeagle
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:mayankeagle
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:mayankeagle
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:mayankeagle
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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:mayankeagle
ID: 10773732
Try setting the unit-increment with jsp.getVerticalScrollBar ().setUnitIncrement () before you display the Container.
0
 
LVL 30

Expert Comment

by:mayankeagle
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:mayankeagle
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:
mayankeagle 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:mayankeagle
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

743 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

11 Experts available now in Live!

Get 1:1 Help Now