【问题标题】:adding values in jcombobox dynamically from database从数据库动态添加 jcombobox 中的值
【发布时间】:2014-02-23 04:20:17
【问题描述】:

我有 3 个单选按钮。如果选择按钮 1,那么我只想显示数据库中的 2 个值,如果我选择其他 2 个,那么我想显示 5 个值。我将通过为单选按钮生成一个事件方法来做到这一点。问题是从数据库中选择值并将其添加到组合框中。我正在为数据库使用 xampp。 谢谢

【问题讨论】:

    标签: java database xampp jcombobox


    【解决方案1】:

    要修改 UI 组件,请使用 UIThreads EventQueue.invokeLater 或 SwingUtilities.invokeLater。 看看这个示例代码:

    SwingUtilities.invokeLater(new Runnable() {
    
            @Override
            public void run() {
                try {
                    Socket socket = new Socket("127.0.0.1", 6677);
    
                    ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
                    ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
    
                    out.writeObject(some_data_for_send_to_server_socket);
                    out.flush();
    
                    ArrayList<String> data =(ArrayList<String>)in.readObject();
                    if (!data.isEmpty()){
                        for(String s:data){
                            yourComboBox.addItem(s);
                        }
                    }
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2014-05-17
      • 1970-01-01
      • 2013-08-06
      • 1970-01-01
      • 1970-01-01
      • 2016-08-11
      • 2011-11-15
      • 1970-01-01
      相关资源
      最近更新 更多