How to make scrollpane scroll with textfield focus

I have a JScrollPane that contains a JPanel with a grid of Labels and JTextFields for data entry. When I tab through the textfields down the panel - reaching fields that are out of view, the scroll pane does scroll down fields with the cursor - keeping the cursor in view.
Is there a way to do this? I've tried some things, but I have not been able to get this working.

here you go..
package com.u2d.focustest;
import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusAdapter;
public class KeepCursorInView extends JFrame
   public KeepCursorInView()
      JPanel contentPane = (JPanel) getContentPane();
      JPanel fieldPanel = new JPanel(new GridLayout(12, 3));
      JScrollPane scrollPane = new JScrollPane(fieldPanel);
      contentPane.add(scrollPane, BorderLayout.CENTER);
      for (int i=0; i<36; i++)
         addField(fieldPanel);
   private void addField(final JPanel panel)
      final JTextField tf = new JTextField();
      panel.add(tf);
      tf.addFocusListener(new FocusAdapter()
         public void focusGained(FocusEvent e)
            Rectangle bounds = tf.getBounds();
            panel.scrollRectToVisible(bounds);
   public static void main(String[] args)
      JFrame f = new KeepCursorInView();
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setBounds(100,100,400,400);
      f.setVisible(true);
}

Similar Messages

  • How to make SMOOTH scrolling with Jtable in Swing automatically ?

    Hi All,
    I'm coding for a price board program, with all records I designed a table for them. So now I want to scroll automatically for all records on that table for customers viewing them. The problem that I can make it scroll smoothly ! Currently I use this block code :
    Thread scollTable;
    int scrollCount = 38;
    public void scrollTable(final int records) {
    if (scollTable == null) {
    scollTable = new Thread() {
    @Override
    public void run() {
    try {
    while (true) {
    if (scrollCount == 38) {
    jTable1.setRowSelectionInterval(scrollCount, scrollCount);
    scrollRowToVisible(scrollCount, jTable1);
    Thread.sleep(4000);
    jTable1.setRowSelectionInterval(scrollCount, scrollCount);
    scrollRowToVisible(scrollCount, jTable1);
    Thread.sleep(1000);
    scrollCount++;
    if (scrollCount == records) {
    scrollCount = 0;
    Thread.sleep(4000);
    jTable1.setRowSelectionInterval(scrollCount, scrollCount);
    scrollRowToVisible(scrollCount, jTable1);
    scrollCount = 38;
    continue;
    } catch(Exception ex) {
    if (!scollTable.isAlive()) {
    //scollTable.setPriority(7);
    scollTable.start();
    public void scrollRowToVisible(int row, JTable table) {
    Rectangle cellRect = table.getCellRect(row, 0, true);
    Rectangle visibleRect = table.getVisibleRect();
    cellRect.x = visibleRect.x;
    cellRect.width = visibleRect.width;
    table.scrollRectToVisible(cellRect);
    The problem I think the progam can not run smoothly with scroll :
    scrollRowToVisible(scrollCount, jTable1);
    Thread.sleep(1000);
    scrollCount++;
    Any helps,
    Thanks.
    Edited by: onlysang2004 on Aug 4, 2009 2:22 AM

    Could you please tell me more detail ? I studied about Swing recently ! I found a link for that javax.swing.Timer : http://forums.sun.com/thread.jspa?threadID=5366085&messageID=10603235#10603235 but how can I use ? In my price board screem doesn't have ActionListener object or button to listen to submit. Can I create an ActionListener object ? Please help me more, how to run smooth scrolling automatically !
    public class ViewElectricHaSTC extends JFrame {
    Thread scollTable;
    public ViewElectricHaSTC(Options options) throws Exception {
    scrollTable(options.getRecord());
    int scrollCount = 38;
    public void scrollTable(final int records) {
    if (scollTable == null) {
    scollTable = new Thread() {
    @Override
    public void run() {
    try {
    while (true) {
    if (scrollCount == 38) {
    //jTable1 created with initComponents method
    jTable1.setRowSelectionInterval(scrollCount, scrollCount);
    scrollRowToVisible(scrollCount, jTable1);
    Thread.sleep(4000);
    jTable1.setRowSelectionInterval(scrollCount, scrollCount);
    scrollRowToVisible(scrollCount, jTable1);
    Thread.sleep(1000);
    scrollCount++;
    if (scrollCount == records) {
    scrollCount = 0;
    Thread.sleep(4000);
    jTable1.setRowSelectionInterval(scrollCount, scrollCount);
    scrollRowToVisible(scrollCount, jTable1);
    scrollCount = 38;
    continue;
    } catch(Exception ex) {
    if (!scollTable.isAlive()) {
    //scollTable.setPriority(7);
    scollTable.start();
    public void scrollRowToVisible(int row, JTable table) {
    Rectangle cellRect = table.getCellRect(row, 0, true);
    Rectangle visibleRect = table.getVisibleRect();
    cellRect.x = visibleRect.x;
    cellRect.width = visibleRect.width;
    table.scrollRectToVisible(cellRect);
    Edited by: onlysang2004 on Aug 4, 2009 7:23 PM

  • How to make parallax scroll with play audio event in specific areas

    Hello there, i would like to learn how can i play audio while scrol is in specific areas that i choosed. For example think that stage is 1920*5000 i want play audio when scroll is in 3000. I am using this code for parallax
    yepnope({
    load:"http://cdn.edgecommons.org/an/1.4.0/js/min/EdgeCommons.js",
    complete: function(){
    EC.debug("Commons ready: "+EC.VERSION);
    EC.centerStage(sym);
    EC.Parallax.setup(sym);
    so please help me how can i integrate this thing to my project. Thanks

    This is your third posting on this topic.
    If you need further help then you need to create a [Short, Self Contained, Compilable and Executable, Example Program (SSCCE)|http://homepage1.nifty.com/algafield/sscce.html], that demonstrates the incorrect behaviour.
    We don't have access to your database so the code can't be executed.

  • How to make a JScrollPane not getting Focus?

    How to make a JScrollPane not getting Focus?
    When i tab out from a textfield inside a scroll pane focus is going to ScrollPane .And if i press tab once more then only focus is going to other textField which is outside the scrollpane.
    For me when i press tab from a text field inside a scrollPane ,i should go to textfield out side the scroll pane.
    satish

    Hi,
    I've the same problem, that I have to double click on tab
    to step from a textfield with a scrollpane to the next textfield in my panel.
    I tried to implement a FocusListener on my JScrollPane, but without success.
    Can you tell me in detail how to implement this listener ?
    Kind regards
    Andy Kanzlers

  • Flash cs3: how to make moving/scrolling photo menu left and right?

    http://media.moma.org/subsites/2008/miro/flashsite/index.html
    - click continue, then relative size link
    How can I do this with lash cs3: how to make moving/scrolling
    photo menu left and right?
    When you move left and right and paintings move. and when you
    click on a painting, you see the the title and it links to another
    page on the site.

    scene_mc start at x = -550... x wide is 2164 px
    now I have this and its working...
    navL_mc.addEventListener(MouseEvent.MOUSE_OVER, navL_Over);
    navL_mc.addEventListener(MouseEvent.MOUSE_OUT, navL_Out);
    function navL_Over(e:MouseEvent){
              if(scena_mc.x == 0){
                                  scena_mc.x == 0;
              else if(scena_mc.x <= -10){
                                  scena_mc.x += 10;
                                  navL_mc.addEventListener(Event.ENTER_FRAME, runL);
    function runL(e:Event):void{
              if(scena_mc.x == 0){
                                  scena_mc.x == 0;
              else if(scena_mc.x <= -10){
                                  scena_mc.x += 10;
    function navL_Out(e:MouseEvent){
              navL_mc.removeEventListener(Event.ENTER_FRAME, runL);
    navR_mc.addEventListener(MouseEvent.MOUSE_OVER, navR_Over);
    navR_mc.addEventListener(MouseEvent.MOUSE_OUT, navR_Out);
    function navR_Over(e:MouseEvent){
              if(scena_mc.x == -1164){
                                  scena_mc.x == -1164;
              else if(scena_mc.x >= -1154){
                                  scena_mc.x -= 10;
                                  navR_mc.addEventListener(Event.ENTER_FRAME, runR);
    function runR(e:Event):void{
              if(scena_mc.x == -1164){
                                  scena_mc.x == -1164;
              else if(scena_mc.x >= -1154){
                                  scena_mc.x -= 10;
    function navR_Out(e:MouseEvent){
              navR_mc.removeEventListener(Event.ENTER_FRAME, runR);

  • How to make a scroll bar!!!

    i need to know how to make a scroll bar in flash cs3! can
    anyone help me with this??

    http://learnola.com/2008/10/27/flash-tutorial-create-a-custom-scrollbar-with-actionscript/

  • How to make text columns with adobe muse

    Hi,How to make text columns with adobe muse (like InDesign)?

    Multiple columns can be acheived with CSS - http://www.w3schools.com/css/css3_multiple_columns.asp
    div
    -moz-column-count:3; /* Firefox */
    -webkit-column-count:3; /* Safari and Chrome */
    column-count:3;
    I'm surprised that Muse does not support text columns yet, but perhaps the custom CSS can be added in style tags on page properties. Haven't tried it, but don't see why it wouldn't work.

  • How to make turning page with ibooks author?

    How to make turning page with ibooks author?

    This question has been answered on this thread
    https://discussions.apple.com/message/17981772#17981772#17981772
    Best regards.
    Alex

  • How to make a scroll animation constantly fixed after scrolling? I want the animation to Start when I scroll down, but I don't want the animated thing to disappear when I scroll up again.

    How to make a scroll animation constantly fixed after scrolling? I want the animation to Start when I scroll down, but I don't want the animated thing to disappear when I scroll up again.
    I know how to start the animation by scrolling down, but I simply want it to stay there after it. Could you help me please, guys?

    Hi Tom,
    There is no native feature in Muse to achieve this.
    Regards,
    Aish

  • How to make this work with Firefox, HELP!

    Downloading for Real-player, after watching the full movie, I click download and it has to reread the movie from the internet. When using explorer, after downloading the movie, it reads it from memory, which makes it a fast download. How to make this work with Firefox, I like not to use Microsoft products, and I really like Firefox 7.0.1!!!! HELP!

    -> click '''Firefox''' button and click '''Options''' (OR File Menu -> Options)
    * Advanced panel -> Network tab
    * place Checkmark on '''Override Automatic Cache Management''' -> under '''Limit Cache''' specify a large size of space
    * Remove Checkmark from '''Tell me when websites asks to store data for offline use'''
    * click OK on Options window
    * Restart Firefox
    Check and tell if ts working.

  • How to make email link with a button with AC2 in flash cs3?

    How to make email link with a button with AC2 in flash cs3?
    I wrote this, but it does not work:
    btn_emailinfo.on (release) {
    getURL("mailto:"[email protected]");
    }

    I am guessing you put that on a frame?
    If so, the syntax is as follows:
    btn_emailinfo.onRelease = function(){
    getURL("mailto:[email protected]");
    Though, if you are placing it directly on the button itself,
    the syntax is:
    on(release){
    getURL("mailto:[email protected]");
    }

  • I reset my phone and it now receives calls that were meant for my husband. I know how to fix this with messaging and facetime, but can't seem to find how to make it stop with the calls. Please help.

    I reset my phone and it now receives calls that were meant for my husband. I know how to fix this with messaging and facetime, but can't seem to find how to make it stop with the calls. Please help.

    It may be due to Continuity
    The following quote is from  Connect your iPhone, iPad, and iPod touch using Continuity
    Turn off iPhone cellular calls
    To turn off iPhone Cellular Calls on a device, go to Settings > FaceTime and turn off iPhone Cellular Calls.

  • In JSF, how to make a menu with access control?

    In JSF, how to make a menu with access control?
    The access control can be guided by programming, database or other means if possible?
    Thanks

    I want to make a dvd menu in iMovie because i don't have IDVD and can't find anywhere to download it?
    For making DVDs I would recommend iMovie 06 and iDVD 09 both readily available on Amazon or eBay.  Shop for iLife 06 and iLife 09.
    You can make menus and chapters with any version of iMovie except the latest one. There's nothing wrong with iMovie 11 either but I prefer iMovie 06.
    By using iMovie 06 and iDVD 09 I make DVDs with professional moving menus with very little effort. They look almost as good as Hollywood.

  • How to make slide images with Dreamweaver CS6?

    How to make slide images with Dreamweaver CS6? Please teach me.

    Hello
    in addition to Jon's hint, I'll send you some links to nice sliders (have fun with the different representations ):
    http://sandbox.scriptiny.com/javascript-slideshow/
    http://jquery.malsup.com/cycle/
    http://wowslider.com/best-jquery-slider-crystal-linear-demo.html
    http://www.jcoverflip.com/demo
    http://www.jacksasylum.eu/ContentFlow/
    http://addyosmani.com/blog/jqueryuicoverflow/
    You only need to use the source code to implant these shows there where you want. In my eyes it would be the best that you use first a very new and blank DW file to perform (maybe in LiveView) the one or the other.
    Hans-Günter

  • How to make the panel with scrollpane transparent?

    Thanks Camicker for your help, it solves the problem but i got a new problem i.e. i want to make that panel with scrollbar transparent for this i have tried setOpaqe(false) for both scrollpane and panel but it doesn't work. do u have any solution?

    I think camickr (not Camicker) already told you to set the opacity of the JScrollPane's viewport.
    Spend just a few minutes with the JScrollPane API and you'll find an exploded view showing the components that make up a scroll pane.
    db

Maybe you are looking for

  • Can't Figure Out Why iPhone 5 Battery Will Not Last

    New to the community and looking for some extra insight into my problems. Cliffs Notes: iPhone 5 won't last more than 4-5 hours on a single charge. When I got my iPhone 5 on launch day I set it up as a new phone and did not restore from a backup. Onl

  • Can anyone decode this Kernal Panic. I've had 6 over the last 2 weeks

    Thanks for looking MacPro 32 gigs RAM 8 core  NVIDIA Quadro FX 5600 1536 MB OS X 10.9.3 (13D65) Sometimes it'll shut down in the middle of the night while rendering, sometimes while I'm authoring 3d or editing 2d ..and if you decipher the bomb, I'd b

  • OneStep DVD doesn't play

    I have recently run into a problem that has never happened before. I use one-step DVD to copy home movie DV tapes to DVD from my Sony camera. Recently, the DVD is made but when I try to play it back I get a still image of one of the default iDVD temp

  • HELP photoshop to dreamweaver problems

    I put together a page in PS, I opened in web setting, put the images on, sliced it up and saved it for web and devices as  an html. But then when I opened it in DW it was just loads of empty squares... any idea why? 1. photoshop 2. Dreamweaver

  • How to set the source pin of the counter

    I am programming using PCI-6251 DAQ card with the board BNC2110 and Labview 7.1. I just want to write a program to count the frequencies of two signals from outside. In Measurement and Automation, I could use the edge counting by selecting the edge s