我有一个单元格编辑器,其中包含一个小按钮,可以双击该按钮以调出一个编辑对话框,然后是一个可用于编辑内联值的文本字段(需要弹出窗口以允许编辑其他值,只有首先显示在 JTable 中)。
当用户单击字段时,一切正常,但如果他们进入字段,他们的文本字段不会获得焦点,并且除非他们用鼠标单击它,否则他们无法编辑该字段。
我尝试摆弄 jpanel 的各种焦点方法,但没有任何区别,有人知道我做错了什么吗?
package com.jthink.jaikoz.celleditor;
import com.jthink.jaikoz.celldata.Cell;
import com.jthink.jaikoz.guielement.Focus;
import com.jthink.jaikoz.table.CellLocation;
import com.jthink.jaikoz.table.DatasheetToggleButton;
import com.jthink.jaikoz.table.datasheet.Datasheet;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class SimpleMultiRowCellEditor
extends DefaultCellEditor implements ActionListener
{
final JPanel panel;
private final DatasheetToggleButton rowCount;
Cell value;
public SimpleMultiRowCellEditor(final JTextField text)
{
super(text);
this.setClickCountToStart(1);
rowCount = new DatasheetToggleButton();
rowCount.setVisible(true);
rowCount.addActionListener(this);
panel = new JPanel();
panel.setOpaque(false);
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(rowCount);
panel.add(editorComponent);
/*panel.setFocusable(true);
panel.setFocusCycleRoot(true);
ArrayList focusOrder = new ArrayList();
focusOrder.add(editorComponent);
focusOrder.add(rowCount);
focusOrder.add(panel);
panel.setFocusTraversalPolicy(new Focus(focusOrder));
*/
}
public Component getTableCellEditorComponent(
final JTable table, final Object val, final boolean isSelected,
final int row, final int column)
{
value = (Cell) ((Cell) val).clone();
rowCount.setText(String.valueOf(value.getValues().size()));
delegate.setValue(value.getValue());
return panel;
}
public Object getCellEditorValue()
{
final String s = (String) delegate.getCellEditorValue();
value.setValue(s);
return value;
}
public void actionPerformed(final ActionEvent e)
{
this.stopCellEditing();
final CellLocation cl = Datasheet.getActiveEditSheet()
.getTable().getSelectedCellLocations().get(0);
UpdateMultiRowCellDialog.getInstanceOf().display(value,cl);
}
}
尝试将焦点监听器添加到面板,似乎没有任何区别
class PanelFocusListener implements FocusListener
{
public void focusGained(FocusEvent e)
{
System.out.println("Gained Focus");
editorComponent.requestFocusInWindow();
}
public void focusLost(FocusEvent e)
{
System.out.println("Lost Focus");
}
}
因此,在进入字段后,我键入一个键,它看起来有点像获得焦点但你不能在该字段中输入任何内容,而如果我键入 RETURN 然后我可以开始编辑该字段,按 RETURN 做什么可以让它工作?