简体   繁体   English

如何改变对JTable的关注?

[英]How can change the focus on the JTable?

I have built simple java swing application. 我已经构建了简单的java swing应用程序。 Now in my JPanel, I have some JTextField and one JTable. 现在在我的JPanel中,我有一些JTextField和一个JTable。 So I have implemented a my custom focus, if the focus is on the JTextFiled1 and I press ENTER, the focus automatically change on the JTextFiled2 etc. The last element that receive the focus is JTable. 因此,我实现了一个自定义焦点,如果焦点位于JTextFiled1上并按ENTER,焦点将自动更改为JTextFiled2等。接收焦点的最后一个元素是JTable。 Now I don't want that the focus is on the first column but on the fourth column, so I have this code: 现在我不希望焦点在第一列,而是在第四列,所以我有以下代码:

textDescrizione.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0), "myTabAction");
textDescrizione.getActionMap().put("myTabAction", new AbstractAction("myTabAction") {
    public void actionPerformed(ActionEvent e) {
        tableConti.requestFocus();
        tableConti.editCellAt(0,4);
    }
});

this code works, I have the focus on my fouth cell, but if I try to press right button on my keyboard, the focus is not of the five column but on the first column. 此代码有效,我将焦点放在我的第四单元格上,但是如果我尝试在键盘上按向右按钮,则焦点不在第五列,而是在第一列。 How can I change this? 我该如何更改?

Another question is, I have implemented a keyListener on my JTable, so if I try to press F5 when JTable have the focus, I call a method. 另一个问题是,我在JTable上实现了keyListener,因此,如果在JTable具有焦点时尝试按F5键,则会调用一个方法。 Also thi works but the focus remains on the cell. 这也可以,但是重点仍然放在单元上。

This is the code: 这是代码:

keyListener keyListener

public class MyKeyListenerSalvataggio extends KeyAdapter{
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_F5){
            salva();
        }

    }
};

tableConti.addKeyListener(new MyKeyListenerSalvataggio());

After that I have press F5, the method salva() finish this is the situation: 在我按F5之后,方法salva()完成这种情况:

在此处输入图片说明

As you can see, I'm not able to clean the value of the table, and delete the focus on the cell. 如您所见,我无法清除表的值并删除单元格上的焦点。

You need to tell the table to stop editing the cell. 您需要告诉表格停止编辑单元格。 See Table Stop Editing for a couple of approaches: 请参阅表格停止编辑以获取以下两种方法:

  1. In the ActionListener you stop the cell editing, or 在ActionListener中,停止单元格编辑,或者
  2. You can set a property of the table to stop editing when it loses focus 您可以设置表格的属性以在失去焦点时停止编辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM