我在SwingX AutoCompleteDecorator上使用JComboBox。除了我希望我的用户被允许更改我的对象的名称(也显示在组合框中的名称)之外,一切都很好。问题是,我可以刷新我的组合框,但是自动完成装饰器显示的字符串与图中所示相同:

刷新组合框的代码如下所示:
try {
Aannemer a = getNewAannemer();
MainController.getInstance().updateAannemer(a);
aannemerBox.revalidate();
aannemerBox.repaint();
} catch (Exception ex) {
//...
}当我从组合框中重新选择对象时,字符串将更新。我还尝试使用个性化渲染器和组合框编辑器。
有什么想法吗?我怎样才能刷新组合框中显示的字符串呢?
发布于 2014-10-17 21:56:03
用当前的代码,很难判断出出了什么问题。下面的代码对我来说很好
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.awt.BorderLayout;
import java.awt.EventQueue;
public class AutoCompleteCombobox {
public static void main( String[] args ) {
EventQueue.invokeLater( () -> {
JFrame frame = new JFrame( "TestFrame" );
JComboBox<String> comboBox = new JComboBox<>( );
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>( );
model.addElement( "First" );
model.addElement( "Second" );
comboBox.setModel( model );
comboBox.setEditable( true );
AutoCompleteDecorator.decorate( comboBox );
frame.getContentPane().add( comboBox );
JButton button = new JButton( "Add item" );
button.addActionListener( e -> {
String selectedItem = ( String ) comboBox.getSelectedItem();
if ( comboBox.getSelectedIndex() == -1 ){
model.addElement( selectedItem );
}
} );
frame.getContentPane().add( button, BorderLayout.SOUTH );
frame.pack();
frame.setVisible( true );
frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
} );
}
}总之,我不能重复你的问题。请在你的问题中贴出一段代码,这样我们就可以重现问题了。
https://stackoverflow.com/questions/26425992
复制相似问题