JTable - 复合编辑器焦点
我有一个由多个组件组成的自定义编辑器。类似于:
class MyCellEditor extends AbstractCellEditor implements TableCellEditor {
JTextArea textArea;
JButton button;
JPanel panel;
MyCellEditor() {
textArea = new JTextArea();
button = new JButton();
panel = new JPanel(new BorderLayout());
panel.add(textArea, BorderLayout.CENTER);
panel.add(button, BorderLayout.EAST);
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
textArea.setText((String) value);
return panel;
}
public Object getCellEditorValue() {
return textArea.getText();
}
}
我希望内部 textArea
在编辑开始时抓住焦点。当我单击单元格时,它工作得很好,但当我使用键盘导航表格并开始在此单元格中键入时,它就不行了。
我该如何解决这个问题?
I have a custom editor composed of several components. Something like:
class MyCellEditor extends AbstractCellEditor implements TableCellEditor {
JTextArea textArea;
JButton button;
JPanel panel;
MyCellEditor() {
textArea = new JTextArea();
button = new JButton();
panel = new JPanel(new BorderLayout());
panel.add(textArea, BorderLayout.CENTER);
panel.add(button, BorderLayout.EAST);
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
textArea.setText((String) value);
return panel;
}
public Object getCellEditorValue() {
return textArea.getText();
}
}
I want the inner textArea
to grab focus when editing starts. It works just fine when I click the cell, but not when I navigate the table with keyboard and start typing in this cell.
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我前段时间遇到了同样的问题,花了很长时间才找到解决方案。使用 focuslistener 和其他东西尝试了很多,但似乎没有什么真正按照我想要的方式工作,直到我发现这个 Santhosh Kumar 的有用文章。
写得很好,应该可以解决你的问题。
I had the same problem some time ago and took me ages to find a solution. Tried a lot with focuslistener and stuff, but nothing really seemed to work the way I wanted it to until I found this useful article by Santhosh Kumar.
Its well written and should fix your problem.