I have a screen with JSrollPane
and several JTextFields
. When changing textfield using the tab key, JScrollPane
does not match if the textfield that receives the focus is "down the screen."
I wanted it when I shifted the focus to a textfield that was not appearing, the JScrollPane
followed and went down or up depending on which textfield I changed the focus.
Code to generate the screen, it is a very simple screen with only the same textfield:
package newpackage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class GridLayoutButtonsTest extends JFrame {
private JPanel contentPane;
private int qtButton = 0;
public static void main(String[] args) {
GridLayoutButtonsTest frame = new GridLayoutButtonsTest();
frame.setVisible(true);
}
public GridLayoutButtonsTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(500, 200));
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JScrollPane scroll = new JScrollPane();
scroll.setPreferredSize(new Dimension(464, 139));
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 1, 10, 10));
for (int i = 0; i < 15; i++) {
panel.add(gerarField());
}
scroll.setViewportView(panel);
contentPane.add(scroll);
pack();
}
public JTextField gerarField() {
qtButton++;
JTextField NewField = new JTextField(String.valueOf(qtButton));
return NewField;
}
}