gpt4 book ai didi

Java Swing : Custom Cell Editor does not return most recent value

转载 作者:行者123 更新时间:2023-12-01 12:45:34 25 4
gpt4 key购买 nike

我创建了一个自定义单元格编辑器,它附加到我的 JTable 列之一。该编辑器具有 JComboBox 或 JTextField 作为组件。但是,当我手动编辑组合框的值并按“tab”时,新的(最新的)值不会附加到 JTable 单元格。相反,旧值将被替换。 (我已经使用另一个代码模板实现了“选项卡”行为,但这通常有效,因为所有其他表格单元格都已正确更新)

导致问题的组合框设置在“case C”中。如果用户现在选择项目“Infinity”,则该值将成功附加到 JTable。但是,如果用户在 JTable 中手动输入一个值并按“tab”,则新值将被丢弃,旧值将再次呈现在表中。但是,作为修补程序,如果用户在组合框中输入值,并在然后选项卡后立即点击“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”的情况下我没有得到最多原因值而是得到上一个表值的原因是什么吗?

谢谢

最佳答案

这里是一个示例,允许您使用不同的编辑器而无需创建自定义编辑器。它重写了 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();
}
});
}
}

您的逻辑会有所不同,因为您将编辑器基于单元格中数据的类,但基本概念可以是相同的。

关于Java Swing : Custom Cell Editor does not return most recent value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24732840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com