简体   繁体   English

JTable自定义单元格渲染器焦点问题

[英]JTable custom cell renderer focus problem

I have a table like this. 我有一张这样的桌子。 The second column uses a JTextField renderer and the third column uses a JPasswordField based renderer and editor. 第二列使用JTextField渲染器,第三列使用基于JPasswordField的渲染器和编辑器。

在此输入图像描述

Looks good. 看起来不错。 But the problem is We have to type the values and must hit "ENTER". 但问题是我们必须输入值并且必须点击“ENTER”。 In that image, I have typed my password but didn't hit Enter. 在该图像中,我输入了密码,但未按Enter键。 So If I click the 'Save & Close' button, it'll show me an error that password field is empty. 因此,如果我单击“保存并关闭”按钮,它将显示密码字段为空的错误。

Previously I have used only JTextFields and JPasswordFields under JTabbedPane and it worked well. 以前我在JTabbedPane下只使用了JTextFields和JPasswordFields,效果很好。 When I had to add more and more stuff, I changed it to a table like this. 当我不得不添加越来越多的东西时,我把它改成了这样的表格。

For now I have put a label to let people know that they should hit the ENTER.. This is not nice. 现在我已经贴了一个标签让人们知道他们应该点击进入..这不太好。 Another big issue. 另一个大问题。 Atleast in Nimbus Look and feel, we get an idea that that field is still in focus. 在Nimbus的Atleast外观和感觉,我们得知这个领域仍然是焦点。 In Windows system look, there's not much visible difference whether the field is focused or not. 在Windows系统外观中,无论字段是否聚焦,都没有太多明显的区别。

在此输入图像描述

I need the Username field or password field to set it's value when I click 'Save & Close' button. 当我点击“保存并关闭”按钮时,我需要用户名字段或密码字段来设置它的值。 Please help me. 请帮我。

So your problem is, that you are still editing the cell. 所以你的问题是,你还在编辑单元格。 So you have to stop the editing and then the cell will be changed. 因此,您必须停止编辑,然后更改单元格。

At your button you can get the cell who is being edited with 在您的按钮上,您可以获取正在编辑的单元格
TableCellEditor cellEditor = table.getCellEditor();
then you can stop the editing with 然后你可以停止编辑
if(cellEditor!=null){
cellEditor.stopCellEditing();
}

and then you can save the value 然后你可以保存价值

告诉表在失去焦点时自动提交:

table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

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

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