Я бы настоятельно рекомендовал использовать SwingX с компонентом ComboBoxCellEditor . По сути, это инкубатор Sun, который должен иметь компоненты Swing. Я понятия не имею, все еще ли активно разрабатывается проект, но он зрелый, и я использовал его во многих проектах.
Если по какой-либо причине вы не можете или не хотите использовать внешнюю библиотеку, вот их код (с частями, измененными для удаления пользовательских функций SwingX), комментарии не повреждены:
Примечание: библиотека написана под GPL.
<code>/*
* $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>
*
*
*
* /
открытый класс ComboBoxCellEditor extends DefaultCellEditor {
/ **
* Создает новый ComboBoxCellEditor.
*
* @param comboBox comboBox, который должен использоваться в качестве редактора ячеек.
* /
public ComboBoxCellEditor (окончательный JComboBox comboBox) {
супер (COMBOBOX);
comboBox.removeActionListener (this.delegate);
this.delegate = new EditorDelegate () {
@Override
public void setValue (окончательное значение объекта) {
comboBox.setSelectedItem (значение);
}
@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;
}
вернуть истину;
}
@Override
public boolean stopCellEditing () {
if (comboBox.isEditable ()) {
// Фиксировать отредактированное значение.
comboBox.actionPerformed (новый ActionEvent (ComboBoxCellEditor.this, 0, ""));
}
return super.stopCellEditing ();
}
@Override
public void actionPerformed (final ActionEvent e) {
ComboBoxCellEditor.this.stopCellEditing ();
}
};
comboBox.addActionListener (this.delegate);
}
}