Jscrollpane track which component is focused

Asked

Viewed 128 times

0

I have a screen with a JSrollPane and several JTextFields. When I change textfield using the key tab, the JScrollPane does not track if the textfield that receives the focus is "below the screen".

I wanted that when I changed the focus to a textfield that is not appearing, the JScrollPane accompany and descend or rise depending on which textfield I change focus to.

Code to generate the screen, is a very simple screen only with 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;
    }
}

1 answer

1


As withdrawn of this question in Soen, you can register a PropertyChangeListener. "focusOwner" is the name of a property within the Keyboardfocusmanager class where the owner of the current focus is registered.

This code snippet will be firing whenever a component in the active window gains focus, and if this new component receiving focus (fieldFocate) is a JTextComponent and belonging to the panel that holds the text fields, will move the visible area of the scroll to it through the method scrollRectToVsisible(), along with the focus.

KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener("focusOwner",
                new PropertyChangeListener() {

                    @Override
                    public void propertyChange(PropertyChangeEvent evt) {
                        if (!(evt.getNewValue() instanceof JTextComponent)) {
                            return;
                        }
                        JTextComponent campoFocado = (JTextComponent) evt.getNewValue();
                        if (scroll.isAncestorOf(campoFocado)) {
                            panel.scrollRectToVisible(campoFocado.getBounds());
                        }
                    }
                });

See working:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.