问题描述
我需要 JXTable 具有 RXTable .做一个简单的覆盖就可以了,但是RXTable的双击功能不适用于JXTable.使用按钮操作"模式时可以,但是使用F2或双击JXTable中的某些内容时,会与RXTable发生冲突,并删除选择内容,因此我将保留默认行为.是由于在内部使用GenericEditor还是其他原因?
I want the capabilities of JXTable with the "select all on edit" behavior of RXTable. Doing a simple override would be fine but the double click function of the RXTable doesn't work with JXTable. When using the Button Action mode it's fine, but when using F2 or a double click something in JXTable clashes with RXTable and remove the selection so I'm left with default behavior. Is it because of the GenericEditor that it uses internally or is it something else?
如何让JXTable在F2上选择全部或双击编辑?
How can I get JXTable to select all on F2 or double click edit?
看起来只有在模型为类型Integer定义了列时,才会发生这种情况.当为字符串"或对象"列定义它时,它可以按预期工作.
It looks like this only happens when the model has the column defined for type Integer. It works as expected when it is defined for a String or Object column.
由于kleopatra的修复,我能够更改selectAll方法,以便它处理JFormattedTextFields和所有编辑情况.由于原始代码可以在类型上进行编辑,因此我仅在其他情况下使用了此修复程序.这就是我最后得到的.
Thanks to kleopatra's fix I was able to alter the selectAll method so it handles JFormattedTextFields and all cases of editing. Since the original code worked on type to edit I just used the fix for other cases. Here is what I ended up with.
用以下内容替换RXTable中的selectAll:
Replace selectAll in RXTable with the following:
/*
* Select the text when editing on a text related cell is started
*/
private void selectAll(EventObject e)
{
final Component editor = getEditorComponent();
if (editor == null
|| ! (editor instanceof JTextComponent
|| editor instanceof JFormattedTextField))
return;
if (e == null)
{
((JTextComponent)editor).selectAll();
return;
}
// Typing in the cell was used to activate the editor
if (e instanceof KeyEvent && isSelectAllForKeyEvent)
{
((JTextComponent)editor).selectAll();
return;
}
// If the cell we are dealing with is a JFormattedTextField
// force to commit, and invoke selectall
if (editor instanceof JFormattedTextField) {
invokeSelectAll((JFormattedTextField)editor);
return;
}
// F2 was used to activate the editor
if (e instanceof ActionEvent && isSelectAllForActionEvent)
{
((JTextComponent)editor).selectAll();
return;
}
// A mouse click was used to activate the editor.
// Generally this is a double click and the second mouse click is
// passed to the editor which would remove the text selection unless
// we use the invokeLater()
if (e instanceof MouseEvent && isSelectAllForMouseEvent)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
((JTextComponent)editor).selectAll();
}
});
}
}
private void invokeSelectAll(final JFormattedTextField editor) {
// old trick: force to commit, and invoke selectall
editor.setText(editor.getText());
SwingUtilities.invokeLater(new Runnable() {
public void run() {
editor.selectAll();
}
});
}
推荐答案
适用于三种选择类型中的两种的快速攻克
a quick hack working for two of the three selection types
// in selectAll(EventObject) special case the formatted early
if (editor instanceof JFormattedTextField) {
invokeSelectAll(editor);
return;
}
private void invokeSelectAll(final JFormattedTextField editor) {
// old trick: force to commit, and invoke selectall
editor.setText(editor.getText());
SwingUtilities.invokeLater(new Runnable() {
public void run() {
editor.selectAll();
}
});
}
通过如何获得焦点时在JFormattedTextField中选择所有文本?-通过键入开始编辑时不处理这种情况,在这种情况下,内容不会被删除(对于普通文本字段而言),而是新键已添加.
was remembered of the trick by How to select all text in a JFormattedTextField when it gets focus? - doesn't handle the case when starting edit by typing, in this case the content is not deleted (as for a normal textfield) but the new key is added.
这篇关于结合JXTable和RXTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!