• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Enter key functionality on JTextField

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello to everyone,

I have one problem with the functionality of Enter key on JTextField.
Actually i have one "Form" where i have used JPanel which is scrollable, so to fill complete form i have to use scrollbar.
Now what i want is on press of "enter key",
i) Focus should transfer to the next textfield.
ii) And at the same time it should scroll down to show the next textfield.
I have achieved the focus functionality of JtextField to transfer the focus on the next TextField but not getting solution of scrolling down.

For review i am posting my code.

This code is to move the focus to the next JTextField



Now what to add extra code to Scroll down ?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


edit And learn to use a layout manager so you can get rid of the setBounds(...), which is fragile and unmaintainable.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I take it the panel is in a JScrollPane? If so, JScrollPane has a method called getViewport() which returns its JViewport. This has a method called scrollRectToVisible. This takes a Rectangle that should be shown. Now all you need to do is take the text fields bounds (getBounds()), then translate that from relative to the panel to relative to the JViewport. In short:
Because locations are always relative to the parent you simply translate it with the parent's location. For instance, if inside the viewport the panel is at location (5,13) and inside the panel the text field is at location (10, 10), then inside the viewport the location of the text field is (15,23).

Didn't know that JComponent itself has that method.

Actually, JComponent.scrollRectToVisible does something similar. It just propagates the call to the parent after this translation.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Um, calling panel.scrollRectToVisible(textField.getBounds()) where textField is a direct child of panel, and panel is housed in a JScrollPane, suffices. No translation of coordinates is involved.

(withdrawn)
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:Um, calling panel.scrollRectToVisible(textField.getBounds()) where textField is a direct child of panel, and panel is housed in a JScrollPane, suffices. No translation of coordinates is involved.


I noticed after your post
Well, there is translation involved, but it's done in the background.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ii) And at the same time it should scroll down to show the next textfield.



Check out Scrolling a Form
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic