Java 滚动至焦点文本字段
所以,我在滚动窗格中有一堆文本字段。 当用户聚焦底部的最后一个文本字段时,会添加更多文本字段。 我的问题是,如何使滚动窗格滚动到焦点文本字段? 我的意思是,用户会按TAB或ENTER键跳转到下一个文本字段,但如果不自己滚动,他将无法看到它。 当最后一个文本字段有焦点时,我可以模拟按下向下箭头或PageDown,但那会很难看,即使它可以做我需要做的事情 我尝试了一些类似的东西,从我通过搜索找到的东西,但我无法使它工作Java 滚动至焦点文本字段,java,swing,focus,textfield,scrollpane,Java,Swing,Focus,Textfield,Scrollpane,所以,我在滚动窗格中有一堆文本字段。 当用户聚焦底部的最后一个文本字段时,会添加更多文本字段。 我的问题是,如何使滚动窗格滚动到焦点文本字段? 我的意思是,用户会按TAB或ENTER键跳转到下一个文本字段,但如果不自己滚动,他将无法看到它。 当最后一个文本字段有焦点时,我可以模拟按下向下箭头或PageDown,但那会很难看,即使它可以做我需要做的事情 我尝试了一些类似的东西,从我通过搜索找到的东西,但我无法使它工作 public void focusGained(FocusEvent e) {
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();
};
}
出现了两件事
首先,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);
}
});
}
}
导入java.awt.BorderLayout;
导入java.awt.EventQueue;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.Rectangle;
导入java.awt.event.FocusAdapter;
导入java.awt.event.FocusEvent;
导入javax.swing.JComponent;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.JTextField;
导入javax.swing.SwingUtilities;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公共类ScrollFocusedField{
公共静态void main(字符串[]args){
新的ScrollFocusedField();
}
公共滚动焦点字段(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
FocusAdapter fh=新的FocusAdapter(){
@凌驾
获得公共无效焦点(焦点事件e){
JComponent comp=(JComponent)e.getComponent();
System.out.println(“focusgound”);
矩形边界=comp.getBounds();
setLocation(SwingUtilities.convertPoint(comp,bounds.getLocation(),comp.getParent());
JComponent父项=(JComponent)comp.getParent();
scrollRectToVisible(comp.getBounds());
}
};
JPanel panel=newjpanel(newgridbagloayout());
GridBagConstraints gbc=新的GridBagConstraints();
对于(int-index=0;index<100;index++){
gbc.fill=GridBagConstraints.NONE;
gbc.weightx=0;
gbc.gridx=0;
gbc.gridy=指数;
panel.add(新JLabel(Integer.toString(index)),gbc);
JTextField=新的JTextField();
字段addFocusListener(fh);
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx=1;
gbc.gridx=1;
面板。添加(字段,gbc);
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
添加(新的JScrollPane(面板));
框架设置尺寸(200400);
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
}
出现了两件事
首先,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);
}
});
}
}
导入java.awt.BorderLayout;
导入java.awt.EventQueue;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.Rectangle;
导入java.awt.event.FocusAdapter;
导入java.awt.event.FocusEvent;
导入javax.swing.JComponent;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.JTextField;
导入javax.swing.SwingUtilities;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公共类ScrollFocusedField{
公共静态void main(字符串[]args){
新的ScrollFocusedField();
}
公共滚动焦点字段(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
FocusAdapter fh=新的FocusAdapter(){
@凌驾
获得公共无效焦点(焦点事件e){
JComponent comp=(JComponent)e.getComponent();
System.out.println(“focusgound”);
矩形边界=comp.getBounds();
setLocation(SwingUtilities.convertPoint(comp,bounds.getLocation(),comp.getParent());
JComponent父项=(JComponent)comp.getParent();
scrollRectToVisible(comp.getBounds());
}
};
JPanel panel=newjpanel(newgridbagloayout());
GridBagConstraints gbc=新的GridBagConstraints();
对于(int索引=0