我强烈建议使用具有
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 L