Scrolling JPanel works but doesnt autoscroll based on focused subcomponents

I need to create a JPanel with large amounts of JTextField components for data entry...
I want to put this in a JScrollpane so that the large amount of fields can fit on the screen.
I have this working, with scrollbars and all however,
When i tab through the textfields, the viewport does not autoscroll to show the active components
I was hoping there was an easy way to get the scrollpane to behave this way.
Any ideas?

Listen focus to force scrolling when a hidden textfield must be scrolled up to get visible.
Try this, I add 10 JTextField to a JPanel. JScollPane receive setViewPosition when focusEvent occur.
          JPanel spane = new JPanel(new GridLayout(0,1));
           jsp = new JScrollPane(spane);
           for (int i=0; i<10; i++) {
               JTextField field = new JTextField(20);
               spane.add(field);
               field.addFocusListener(new FocusAdapter() {
                   public void focusGained(FocusEvent fe) {                      
                       JTextField field = (JTextField)fe.getSource();
                       int last = jsp.getVerticalScrollBar().getValue() + jsp.getViewportBorderBounds().height;
                       if (field.getBounds().y > last) {
                         jsp.getViewport().setViewPosition(new Point(field.getBounds().x, field.getBounds().y));
           }This is working when scrolling down, you have to adapt for scrolling up.

Similar Messages

Maybe you are looking for

  • Open bat file on client machine using webutil

    Hi all. I'm on devsuite 10g. I want to run a bat file on client machine using client_host function of webutil. I have tried first to execute the bat file with mouse double-click in order to check if it is ok, and it works. Now I want to execute this

  • Int to Object casting

    This should be a simple question, I just cant seem to get it. I want to cast an int to an Object. // --- Does not work int num = 16; Object obj = num; // --- Does not work int num = 16; Object obj = (Object)num; // --- Does not work Object obj = (Obj

  • FX-5200-T128 and DVI-I

    Hi all, I have a 5200 whithout a digital output and I´m interested in a TFT with DVI-I. The manual of the card says that the card "supports" DVI-I. Can I buy a connector forn the card or something like that? Thanks,

  • Calculate future date in MM/dd/yyyy format

    Hi, I am trying to add a 56 months to a given date (MM/dd/yyyy string) and output the date in MM/dd/yyyy format. Here is my code:                SimpleDateFormat date = new SimpleDateFormat("MM/dd/yyyy");                date.parse("07/30/2000");     

  • Setting up Unity Connection to email/call cell for new vmail msg

    Hello, I have been trying to setup Unity connection so that it will send an email to Exchange or a message toa cellphone alerting a user that they have a new vmail. I went in under my user account to test (under Edit\Notifcation Devices) and added my