我有一张桌子。该表上的更改将更新数据库。其中一列由该表中的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;
}
}发布于 2011-09-30 15:06:33
现在它起作用了。每次调用getTableCellEditorComponent方法时,都需要重新初始化JComboBox。在此JComboBox的itemstatechange中,stopCellEditing()方法必须通知侦听器在选择项目时已完成编辑。这使得TableModelListener fireTableChanged事件。(已修复)但是,当您在未进行选择的情况下单击另一个JComboBox后单击另一个JComboBox时,它也会触发该事件。(/Fixed)
编辑:以下代码是最新版本。仅当选择项目时才通知此TableModelListener。上面提到的问题已经解决了。这是因为默认的stopCellEditing()方法总是返回true。这会导致单元格编辑以意外的方式停止。它必须根据需要被重写,并且必须使用fireEditingStopped();来通知TableModelListener
public class JIDCellEditor extends AbstractCellEditor implements TableCellEditor {
private JComboBox jComboBox = new JComboBox();
boolean cellEditingStopped = false;
@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();
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(Integer.parseInt(value.toString()));
vector.add(0);
for (int i = 0; i < table.getRowCount(); i++) {
if (!vector.contains(table.getValueAt(i, 0)) && table.getValueAt(i, 3).toString().equals("Sheep")) {
vector.add(table.getValueAt(i, 0));
}
}
vector.remove(table.getValueAt(row, 0));
for (int i = 0; i < vector.size(); i++) {
}
jComboBox = new JComboBox(vector);
jComboBox.setSelectedItem(value);
jComboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
fireEditingStopped();
}
}
});
jComboBox.addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
cellEditingStopped = false;
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
cellEditingStopped = true;
fireEditingCanceled();
}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {
}
});
return jComboBox;
}
@Override
public boolean stopCellEditing() {
return cellEditingStopped;
}
}发布于 2011-09-30 02:23:19
我强烈推荐使用具有ComboBoxCellEditor组件的SwingX。它本质上是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);
}
}https://stackoverflow.com/questions/7601436
复制相似问题