2014-01-30 25 views
0

我有3個單選按鈕。如果按鈕1被選中,那麼我想只顯示數據庫中的2個值,如果我選擇其他2,那麼我想顯示5個值。我將通過爲單選按鈕生成事件方法來完成此操作。問題是從數據庫中選擇值並將其添加到組合框中。我正在使用xampp作爲數據庫。 謝謝從數據庫動態添加jcombobox中的值

回答

0

要修改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(); 
      } 
     } 
    }); 
相關問題