2017-06-03 92 views
0

我想爲國家和城市創建兩個下拉列表。因此,如果用戶從下拉列表中選擇一個國家,則應該自動刷新另一個下拉列表以顯示其相應的城市。GUI中國家和城市的動態列表

public void fillCombo(){ 
     String sql = " select * from sallytimes.table_name " ; 
     try { 
      ps = con.prepareStatement(sql); 
      rs = ps.executeQuery(sql); 

      while (rs.next()){ 
       jComboBox_Country.addItem(rs.getString("_id")); 
       jComboBox_City.addItem(rs.getString("city"));  
      } 
     } catch (SQLException ex) { 
      Logger.getLogger(Location.class.getName()).log(Level.SEVERE, null, ex); 
     } 
} 

我該如何處理?

+0

我覺得很有意思你的開場白是怎麼完全一樣,在這個論壇的第一篇文章,從2010年:https://www.java-forums.org/new-java/26492-dynamic-list-country- city.html – Ray

+0

haha​​haha ...真的嗎? –

回答

1

你需要一個ActionListener添加到第一個組合框,當一個項目被選中來執行操作。在ActionListener中,您想用新數據替換第二個組合框的模型。

喜歡的東西:

import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
import javax.swing.*; 
import javax.swing.plaf.basic.*; 

public class ComboBoxTwo extends JPanel implements ActionListener 
{ 
    private JComboBox<String> mainComboBox; 
    private JComboBox<String> subComboBox; 
    private Hashtable<String, String[]> subItems = new Hashtable<String, String[]>(); 

    public ComboBoxTwo() 
    { 
     String[] items = { "Select Item", "Color", "Shape", "Fruit" }; 
     mainComboBox = new JComboBox<String>(items); 
     mainComboBox.addActionListener(this); 

     // prevent action events from being fired when the up/down arrow keys are used 
     mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE); 
     add(mainComboBox); 

     // Create sub combo box with multiple models 

     subComboBox = new JComboBox<String>(); 
     subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4 
     add(subComboBox); 

     JButton arrow = SwingUtils.getDescendantOfType(JButton.class, subComboBox, "Text", ""); 
     Dimension d = arrow.getPreferredSize(); 
     System.out.println(arrow.getClass()); 
     System.out.println(d); 
     d.width = 35; 
     arrow.setPreferredSize(d); 

     String[] subItems1 = { "Select Color", "Red", "Blue", "Green" }; 
     subItems.put(items[1], subItems1); 

     String[] subItems2 = { "Select Shape", "Circle", "Square", "Triangle" }; 
     subItems.put(items[2], subItems2); 

     String[] subItems3 = { "Select Fruit", "Apple", "Orange", "Banana" }; 
     subItems.put(items[3], subItems3); 
    } 

    public void actionPerformed(ActionEvent e) 
    { 
     String item = (String)mainComboBox.getSelectedItem(); 
     Object o = subItems.get(item); 

     if (o == null) 
     { 
      subComboBox.setModel(new DefaultComboBoxModel()); 
     } 
     else 
     { 
      subComboBox.setModel(new DefaultComboBoxModel((String[])o)); 
     } 
    } 

    private static void createAndShowUI() 
    { 
     try 
     { 
//   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     } 
     catch (Exception e) { } 
     JFrame frame = new JFrame("SSCCE"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new ComboBoxTwo()); 
     frame.setLocationByPlatform(true); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       createAndShowUI(); 
      } 
     }); 
    } 
} 

所以,你會需要修改的ActionListener的代碼即可獲得基於所選國家數據庫中的城市。

+0

實際上我使用MY_SQL數據庫連接國家和城市組合框。 現在,我該如何解決這個問題? –

+0

@muhammadsalman,'我該如何解決這個問題?' - 什麼問題?您已獲得工作的代碼演示瞭如何當第一組合框中的項目更改更新第二個組合框。所以你需要做的就是從你的數據庫中獲取一個城市列表。只有你知道你的數據庫的結構,所以我不能幫你的SQL。 – camickr