在 jTable 上使用 Enter 键就像 Tab 键一样

Use Enter Key Act Like Tab Key on jTable(在 jTable 上使用 Enter 键就像 Tab 键一样)
本文介绍了在 jTable 上使用 Enter 键就像 Tab 键一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中被困 2 天,我无法实现如何使 ENTER KEYTAB KEY 我试过 key listener 但是 ENTER KEY 具有 JTable 的默认功能,因此它无法正常工作,它会继续向下移动.我 google 它发现我需要使用 键绑定 但我无法实现它.

I'm stuck from 2 days in my project i cant implement how to make the ENTER KEY act like TAB KEY i tried key listener but the ENTER KEY have a default feature for JTable so it's not working it's keep moving down. I google it discovered that i need to use key binding but i'm unable to implement it.

谁能在 JTable 上给我一个完整的代码示例???请需要你的帮助.

Can anyone give me a full coded example of this on a JTable ??? Please need you help.

提前致谢

推荐答案

基本的转变是使用键绑定 API,在大多数情况下,这将允许您覆盖许多组件上的默认行为键.

The basic twist would be to use the key bindings API, which will allow you to override, in most cases, the default behaviour keys on many components.

此示例基本上将相同的命名操作应用于 EnterTab 键,这样可以通过使用单个 Action 轻松修改它们的行为.

This example basically applies the same named action to the Enter and Tab keys, this makes it easy to modify their behaviour through the use of a single Action.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class Test101 {

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

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

                JTable table = new JTable();
                InputMap im = table.getInputMap();
                im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Action.NextCell");
                im.put(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "Action.NextCell");

                ActionMap am = table.getActionMap();
                am.put("Action.NextCell", new NextCellActioin(table));

                DefaultTableModel model = new DefaultTableModel(10, 10);
                table.setModel(model);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class NextCellActioin extends AbstractAction {

        private JTable table;

        public NextCellActioin(JTable table) {
            this.table = table;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            int col = table.getSelectedColumn();
            int row = table.getSelectedRow();

            int colCount = table.getColumnCount();
            int rowCount = table.getRowCount();

            col++;
            if (col >= colCount) {
                col = 0;
                row++;
            }

            if (row >= rowCount) {
                row = 0;
            }

            table.getSelectionModel().setSelectionInterval(row, row);
            table.getColumnModel().getSelectionModel().setSelectionInterval(col, col);
        }

    }

}

我记得 Tab 的功能是通过焦点管理器更改默认焦点行为来控制的

The functionality of Tab is controlled by changing the default focus behaviour through the focus manager as I recall

这篇关于在 jTable 上使用 Enter 键就像 Tab 键一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How can create a producer using Spring Cloud Kafka Stream 3.1(如何使用Spring Cloud Kafka Stream 3.1创建制片人)
Insert a position in a linked list Java(在链接列表中插入位置Java)
Did I write this constructor properly?(我是否正确地编写了这个构造函数?)
Head value set to null but tail value still gets displayed(Head值设置为空,但仍显示Tail值)
printing nodes from a singly-linked list(打印单链接列表中的节点)
Control namespace prefixes in web services?(控制Web服务中的命名空间前缀?)