gpt4 book ai didi

java - JComboBox 作为自定义 TableCellEditor

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:50:10 26 4
gpt4 key购买 nike

我有一张 table 。该表更新数据库的更改。一列由该表中的 JComboBox 编辑。单击该列中的任何单元格都会触发 tableChanged 事件。但是,需要在选择 JComboBox 的项目后触发它。如何让 tableChanged 在选择后发生?

public class JIDCellEditor extends AbstractCellEditor implements TableCellEditor {

JComboBox jComboBox;

@Override
public Object getCellEditorValue() {
return jComboBox.getSelectedItem();
}

@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
Vector vector = new Vector();
vector.add(0);
for (int i = 0; i < table.getRowCount(); i++) {
if (!vector.contains(table.getValueAt(i, 0)) && table.getValueAt(i, 3).toString().equals("Female")) {
vector.add(table.getValueAt(i, 0));
}
}
vector.remove(table.getValueAt(row, 0));
jComboBox = new JComboBox(vector);
jComboBox.setSelectedItem(value);
return jComboBox;
}
}

最佳答案

我强烈推荐使用 SwingX它有一个 ComboBoxCellEditor成分。它本质上是 Sun 为 Swing 组件应有的特性提供的孵化器。我不知道该项目是否仍在积极开发中,但它已经成熟,我已经在许多项目中使用过它。

如果出于某种原因,您不能或不想使用外部库,这里是他们的代码(修改部分以删除自定义 SwingX 功能),完整注释:

注意:库是 GPL 代码。

/*
* $Id: ComboBoxCellEditor.java 3738 2010-07-27 13:56:28Z bierhance $
*
* Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, U.S.A. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.util.EventObject;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;

/**
* <p>
* This is a cell editor that can be used when a combo box (that has been set up for automatic completion) is to be used in a JTable. The
* {@link javax.swing.DefaultCellEditor DefaultCellEditor} won't work in this case, because each time an item gets selected it stops cell
* editing and hides the combo box.
* </p>
* <p>
* Usage example:
* </p>
* <p>
*
* <pre>
* <code>
* JTable table = ...;
* JComboBox comboBox = ...;
* ...
* TableColumn column = table.getColumnModel().getColumn(0);
* column.setCellEditor(new ComboBoxCellEditor(comboBox));
* </code>
* </pre>
*
* </p>
*/
public class ComboBoxCellEditor extends DefaultCellEditor {

/**
* Creates a new ComboBoxCellEditor.
*
* @param comboBox the comboBox that should be used as the cell editor.
*/
public ComboBoxCellEditor(final JComboBox comboBox) {
super(comboBox);

comboBox.removeActionListener(this.delegate);

this.delegate = new EditorDelegate() {
@Override
public void setValue(final Object value) {
comboBox.setSelectedItem(value);
}

@Override
public Object getCellEditorValue() {
return comboBox.getSelectedItem();
}

@Override
public boolean shouldSelectCell(final EventObject anEvent) {
if (anEvent instanceof MouseEvent) {
final MouseEvent e = (MouseEvent) anEvent;
return e.getID() != MouseEvent.MOUSE_DRAGGED;
}
return true;
}

@Override
public boolean stopCellEditing() {
if (comboBox.isEditable()) {
// Commit edited value.
comboBox.actionPerformed(new ActionEvent(ComboBoxCellEditor.this, 0, ""));
}
return super.stopCellEditing();
}

@Override
public void actionPerformed(final ActionEvent e) {
ComboBoxCellEditor.this.stopCellEditing();
}
};
comboBox.addActionListener(this.delegate);
}
}

关于java - JComboBox 作为自定义 TableCellEditor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7601436/

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