問題描述
我在我的項目中被困 2 天,我無法實現如何使 ENTER KEY 像 TAB 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.
此示例基本上將相同的命名操作應用于 Enter 和 Tab 鍵,這樣可以通過使用單個 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 鍵一樣的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!