Skip to main content

Moving Scroll in JScrollPane

1 reply [Last post]
ayesh
Offline
Joined: 2011-03-02
Points: 0

Hello
I have panel with many of JTextFields Vertically when I use the down arrow key, the JScrollpane can not follow the caret, any one have Idea to make the Scroll Follow the caret. this is the example...

Thanks in advance.

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

public class ScrollTesting {

private JPanel panel = new JPanel();
private JScrollPane scrollPane = new JScrollPane(panel);

private Vector fieldsVector = new Vector();
private Dimension preferredSize = new Dimension(400, 40);
private Font font = new Font("Tahoma", 1, 28);

public ScrollTesting() {

panel.setLayout(new GridLayout(100, 1));
for (int i = 0; i < 100; i++) {
fieldsVector.addElement(new JTextField());
fieldsVector.lastElement().setPreferredSize(preferredSize);
fieldsVector.lastElement().setFont(font);
panel.add(fieldsVector.lastElement());
}

enterAction();

JFrame frame = new JFrame();
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scrollPane);
frame.setVisible(true);

}

private void enterAction() {
HashSet forwardKeys = new HashSet();
forwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
forwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
forwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0));
panel.setFocusTraversalKeys(
KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, forwardKeys);
}

public static void main(String[] args) {
new ScrollTesting();
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kleopatra
Offline
Joined: 2003-06-11
Points: 0

.. and your problem is related to SwingX/SwingLabs in that? Please post general/basic questions in a general/basic Swing forum

Thanks, Jeanette