概述
请尝试这个例子.
它应该让您输入上,下,左,右的值u,d,l,r来浏览表格.
希望这会给您一个有关如何做的想法.
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class Test extends JFrame {
private JTable table;
private TableModel tableModel;
public Test() {
tableModel = new DefaultTableModel(5, 5);
table = new JTable(tableModel);
table.setColumnSelectionAllowed(true);
getContentPane().add(table);
Action handleEnter = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
table.getCellEditor().stopCellEditing(); // store user input
int row = table.getSelectedRow();
int col = table.getSelectedColumn();
String val = String.valueOf(table.getValueAt(row, col)).toLowerCase();
if (val.equals("u"))
--row;
else if (val.equals("d"))
++row;
else if (val.equals("l"))
--col;
else if (val.equals("r"))
++col;
if ( row >= 0 && row < tableModel.getRowCount()
&& col >= 0 && col < tableModel.getColumnCount()) {
table.changeSelection(row, col, false, false);
table.editCellAt(row, col);
}
}
};
// replace action for ENTER, since next row would be selected automatically
table.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "handleEnter");
table.getActionMap().put("handleEnter", handleEnter);
}
public static void main(String[] args) {
Test test = new Test();
test.setSize(800, 600);
test.setVisible(true);
}
}
最后
以上就是危机火车为你收集整理的java控制页面焦点_java-如何在JTable中控制焦点的全部内容,希望文章能够帮你解决java控制页面焦点_java-如何在JTable中控制焦点所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复