JTable自定义单元格渲染器焦点问题
我有一张这样的桌子。第二列使用 JTextField 渲染器,第三列使用基于 JPasswordField 的渲染器和编辑器。
看起来不错。但问题是我们必须输入值并且必须按“ENTER”。在该图像中,我输入了密码,但没有按 Enter 键。所以如果我点击“保存并保存”关闭'按钮,它会显示密码字段为空的错误。
以前我只在 JTabbedPane 下使用过 JTextFields 和 JPasswordFields,效果很好。当我不得不添加越来越多的东西时,我把它改成了这样的表格。
现在我已经贴了一个标签,让人们知道他们应该按 ENTER 键。这不太好。另一个大问题。至少在 Nimbus 的外观和感觉中,我们知道该领域仍然是焦点。在Windows系统中,无论视野是否聚焦,都没有太大明显的区别。
当我单击“保存并保存”时,我需要用户名字段或密码字段来设置其值。关闭'按钮。请帮我。
I have a table like this. The second column uses a JTextField renderer and the third column uses a JPasswordField based renderer and editor.
Looks good. But the problem is We have to type the values and must hit "ENTER". In that image, I have typed my password but didn't hit 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. 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. In Windows system look, there's not much visible difference whether the field is focused or not.
I need the Username field or password field to set it's value when I click 'Save & Close' button. Please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以你的问题是,你仍在编辑单元格。所以你必须停止编辑,然后单元格才会改变。
在您的按钮上,您可以获取正在编辑的单元格
TableCellEditor cellEditor = table.getCellEditor();
然后你可以停止编辑
if(cellEditor!=null){
cellEditor.stopCellEditing();
}
然后你可以保存该值
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
告诉表在失去焦点时自动提交:
Tell the table to automatically commit when losing focus: