我创建了一个自定义单元格编辑器,它附加到我的一个JTable列。编辑器有一个JComboBox或一个JTextField作为组件。但是,当我手动编辑组合框的值并按“tab”时,新的 - 最新 - 值不会附加到JTable单元格。相反,旧值被替换。 (我已经使用另一个代码模板实现了“tab”行为,但这通常有效,因为所有其他表格单元格都已正确更新)
导致问题的组合框在“案例C”中设置。如果用户现在选择ITEM“Infinity”,则该值成功附加到JTable。但是,如果用户在JTable中手动输入值并按下“tab”,则会丢弃新值,并在表中再次呈现旧值。 但是,作为修补程序,如果用户在组合框中输入值,在“THEN”选项卡之后立即点击“ENTER”,则通过jtable正确写入值!
我的自定义单元格编辑器实现如下:
public class CustomCellEditor extends AbstractCellEditor implements TableCellEditor {
JFrame mParent = null;
JFrame mPopup = null;
String className = "";
protected int clickCountToStart = 2;
private TableCellEditor mEditor = null;
private JComboBox mComboBox = null;
public CustomCellEditor(JFrame parent, String className, JComboBox comboBox) {
mParent = parent;
mClassName = className;
mComboBox = comboBox;
}
@Override
public Component getTableCellEditorComponent(final JTable table, final Object value, final boolean isSelected, final int row, final int column) {
if ( className.equals("case A") ) {
mComboBox.setModel( new DefaultComboBoxModel(Constants.YES_NO_ARRAY) );
mComboBox.setEditable(false);
mEditor = new DefaultCellEditor(mComboBox);
}
else if ( className.equals("case B") ) {
mComboBox.setModel( new DefaultComboBoxModel(Constants.LANG_ARRAY) );
mComboBox.setEditable(false);
mEditor = new DefaultCellEditor(mComboBox);
}
else if ( className.equals("case C") ) {
// THIS is the case, when the Jcombobox become editable, so beside the pre-defined item "Constants.INFIINITY" any arbitrary input should be allowed!
mComboBox.setModel ( new DefaultComboBoxModel(new String[]{Constants.INFINITY}) ) ;
mComboBox.setEditable(true);
mEditor = new DefaultCellEditor(mComboBox);
}
else {
mEditor = new DefaultCellEditor(new JTextField());
}
return mEditor.getTableCellEditorComponent(table, value, isSelected, row, column);
}
/**
* Returns true if <code>anEvent</code> is <b>not</b> a
* <code>MouseEvent</code>. Otherwise, it returns true
* if the necessary number of clicks have occurred, and
* returns false otherwise.
*
* @param anEvent the event
* @return true if cell is ready for editing, false otherwise
* @see #setClickCountToStart
* @see #shouldSelectCell
*/
@Override
public boolean isCellEditable(EventObject anEvent) {
if (anEvent instanceof MouseEvent) {
return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart;
}
return true;
}
@Override
public Object getCellEditorValue() {
if (mEditor != null) {
return mEditor.getCellEditorValue();
}
return null;
}
}
有人知道原因是什么,在“案例C”的情况下,我没有得到最大的理由值,而是之前的表值?
谢谢
答案 0 :(得分:1)
以下示例允许您在不创建自定义编辑器的情况下使用其他编辑器。它覆盖了JTable的getCellEditor(...)
方法:
import java.awt.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class TableComboBoxByRow extends JPanel
{
List<String[]> editorData = new ArrayList<String[]>(3);
public TableComboBoxByRow()
{
setLayout( new BorderLayout() );
// Create the editorData to be used for each row
editorData.add( new String[]{ "Red", "Blue", "Green" } );
editorData.add( new String[]{ "Circle", "Square", "Triangle" } );
editorData.add( new String[]{ "Apple", "Orange", "Banana" } );
// Create the table with default data
Object[][] data =
{
{"Color", "Red"},
{"Shape", "Square"},
{"Fruit", "Banana"},
{"Plain", "Text"}
};
String[] columnNames = {"Type","Value"};
DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable(model)
{
// Determine editor to be used by row
public TableCellEditor getCellEditor(int row, int column)
{
int modelColumn = convertColumnIndexToModel( column );
if (modelColumn == 1 && row < 3)
{
JComboBox<String> comboBox1 = new JComboBox<String>( editorData.get(row));
return new DefaultCellEditor( comboBox1 );
}
else
return super.getCellEditor(row, column);
}
};
JScrollPane scrollPane = new JScrollPane( table );
add( scrollPane );
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("Table Combo Box by Row");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new TableComboBoxByRow() );
frame.setSize(200, 200);
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
您的逻辑会有所不同,因为您将编辑器基于单元格中的数据类,但基本概念可以是相同的。
答案 1 :(得分:0)
你试过这个吗?
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);