【问题标题】:Refresh JComboBox with SwingX AutoCompleteDecorator使用 SwingX AutoCompleteDecorator 刷新 JComboBox
【发布时间】:2014-12-13 02:16:07
【问题描述】:

我在 JComboBox 上使用 SwingX AutoCompleteDecorator。一切正常,除了我希望我的用户被允许更改我的对象的名称,该名称也显示在组合框中。问题是我可以刷新我的组合框,但自动完成装饰器中显示的字符串与图片中显示的一样:

刷新组合框的代码如下所示:

try {
   Aannemer a = getNewAannemer();
   MainController.getInstance().updateAannemer(a);
   aannemerBox.revalidate();
   aannemerBox.repaint();
} catch (Exception ex) {
   //...
}

当我从组合框中重新选择对象时,字符串会更新。 我还尝试为组合框使用个性化的渲染器和编辑器。

有什么想法可以刷新组合框中显示的字符串吗?

【问题讨论】:

    标签: java swing jcombobox swingx


    【解决方案1】:

    使用当前代码,很难判断出了什么问题。以下代码对我来说很好用

    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 );
        } );
      }
    }
    
    • 自动完成按预期工作
    • 我可以输入新项目
    • 当使用添加按钮时,我可以添加新项目并且自动完成功能很好

    简而言之,我无法重现您的问题。请在您的问题中发布一段代码,以便我们重现问题。

    【讨论】:

      猜你喜欢
      • 2013-07-31
      • 2012-06-23
      • 2012-02-28
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多