【问题标题】:Scroll to focused textfield滚动到焦点文本字段
【发布时间】:2013-12-11 01:29:21
【问题描述】:

所以,我在滚动窗格中有一堆文本字段。 当用户关注底部的最后一个时,会添加更多文本字段。 我的问题是,我怎样才能让滚动窗格滚动到焦点文本字段? 我的意思是,用户将按 TAB 或 ENTER 键跳转到下一个文本字段,但如果不滚动自己,他将无法看到它。 当最后一个文本字段有焦点时,我可以模拟按下向下箭头或 PageDown 但这会很难看,即使它会做我需要做的事情。

我通过搜索找到了类似的东西,但我无法让它工作。

public void focusGained(FocusEvent e) {
            currentview = t1;
            int cons = i - 1;
             Rectangle r = new Rectangle(t1.getX(), t1.getY(), 1, 1);
            jScrollPane1.scrollRectToVisible(r);
            if (t1.getName().equals("prod" + cons)) {

                newproduct();
            };

        }

【问题讨论】:

  • 你可以试试JComponent#scrollRectToVisible,例如t1.scrollRectToVisible(t1.getBounds())
  • 大坝!你是对的,JTextFieldscrollRectToVisible 没有将调用传递给超级实现...。您尝试了一种破解方法,请参阅答案...

标签: java swing focus textfield scrollpane


【解决方案1】:

出现了两件事。

首先,JTextField#scrollRectToVisible 已被覆盖,并做了一些与其他组件略有不同的事情……烦人……

其次,您需要转换字段相对于其父级的位置,然后使用父级的scrollRectToVisible 方法,例如...

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ScrollFocusedField {

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

    public ScrollFocusedField() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                FocusAdapter fh = new FocusAdapter() {

                    @Override
                    public void focusGained(FocusEvent e) {
                        JComponent comp = (JComponent) e.getComponent();
                        System.out.println("FocusGained");
                        Rectangle bounds = comp.getBounds();
                        bounds.setLocation(SwingUtilities.convertPoint(comp, bounds.getLocation(), comp.getParent()));

                        JComponent parent = (JComponent) comp.getParent();
                        parent.scrollRectToVisible(comp.getBounds());
                    }

                };

                JPanel panel = new JPanel(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                for (int index = 0; index < 100; index++) {
                    gbc.fill = GridBagConstraints.NONE;
                    gbc.weightx = 0;
                    gbc.gridx = 0;
                    gbc.gridy = index;
                    panel.add(new JLabel(Integer.toString(index)), gbc);

                    JTextField field = new JTextField();
                    field.addFocusListener(fh);
                    gbc.fill = GridBagConstraints.HORIZONTAL;
                    gbc.weightx = 1;
                    gbc.gridx = 1;
                    panel.add(field, gbc);
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(panel));
                frame.setSize(200, 400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

【讨论】:

    【解决方案2】:

    查看FormScroller。这是一个简单的类,您可以使用它来确保当您切换到组件时该组件是可见的。 FormScroller 可以与任何滚动窗格一起使用,无需将 FocusListeners 添加到表单上的每个组件。

    它提供了一些选项,可让您准确控制滚动的工作方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多