使用TAB将焦点更改为JTable中的下一个组件 [英] Change focus to next component in JTable using TAB

查看:221
本文介绍了使用TAB将焦点更改为JTable中的下一个组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JTable的默认行为是改变焦点到下一个单元格,我想强制它将焦点移动到下一个组件(例如JTextField)按下TAB键。
我重写了 isCellEditable DefaultTableModel 方法总是返回false:

  public boolean isCellEditable(int rowIndex,int columnIndex){
return false;
}

但是它仍然没有改变焦点到下一个组件!

我应该如何让 JTable 将焦点改为下一个组件而不是下一个单元?

解决方案

如果您真的想要这样做,您需要更改表格动作地图的默认行为。
$ b

  ActionMap am = table.getActionMap(); 
am.put(selectPreviousColumnCell,new PreviousFocusHandler());
am.put(selectNextColumnCell,new NextFocusHandler());

然后你需要几个动作来处理遍历

  public class PreviousFocusHandler extends AbstractAction {
public void actionPerformed(ActionEvent evt){
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.focusPreviousComponent();


$ b $ public class NextFocusHandler extends AbstractAction {
public void actionPerformed(ActionEvent evt){
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.focusNextComponent();






$ p另一种方法是禁用底层的 $ b

  ActionMap am = table.getActionMap(); 
am.get(selectPreviousColumnCell)。setEnabled(false);
am.get(selectNextColumnCell)。setEnabled(false);

(还没有测试过)

这种方法的好处是可以根据需要启用/禁用行为,而无需维护对旧的 Actions

的引用

JTable's default behavior is changing focus to next cell and I want to force it to move focus to next component (e.g. JTextField) on TAB key pressed.
I overrided isCellEditable method of DefaultTableModel to always return false:

public boolean isCellEditable(int rowIndex, int columnIndex) {
    return false;
}

But it still doesn't change focus to next component!
How should I make JTable change focus to next component instead of next cell?

解决方案

If you really want this, you need to change the default behavior of the tables action map.

ActionMap am = table.getActionMap();
am.put("selectPreviousColumnCell", new PreviousFocusHandler());    
am.put("selectNextColumnCell", new NextFocusHandler());    

Then you need a couple of actions to handle the traversal

public class PreviousFocusHandler extends AbstractAction {
    public void actionPerformed(ActionEvent evt) {
        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.focusPreviousComponent();
    }
}

public class NextFocusHandler extends AbstractAction {
    public void actionPerformed(ActionEvent evt) {
        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.focusNextComponent();
    }
}

Another approach would be to disable the underlying Action...

ActionMap am = table.getActionMap();
am.get("selectPreviousColumnCell").setEnabled(false);
am.get("selectNextColumnCell").setEnabled(false);

(haven't tested this)

The benefit of this approach is can enable/disable the behaviour as you need it without needing to maintain a reference to the old Actions

这篇关于使用TAB将焦点更改为JTable中的下一个组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆