2017-06-03 1 views
0

Ich möchte zwei Dropdown-Liste für Land und Stadt erstellen. Wenn der Benutzer also ein Land aus der Dropdown-Liste auswählt, sollte die andere Dropdown-Liste automatisch aktualisiert werden, um die entsprechenden Städte anzuzeigen.Dynamische Liste für Land und Stadt in 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); 
     } 
} 

Wie mache ich das?

+0

Ich finde es interessant, wie Ihre Eröffnungsstatement ist genau das gleiche wie der erste Beitrag in diesem Forum, von 2010: https://www.java-forums.org/new-java/26492-dynamic-list-country- city.html – Ray

+0

hahahaha ... wirklich? –

Antwort

1

Sie müssen einen ActionListener zum ersten Kombinationsfeld hinzufügen, um eine Aktion auszuführen, wenn ein Element ausgewählt ist. Im ActionListener möchten Sie das Modell des zweiten Kombinationsfelds durch die neuen Daten ersetzen.

Etwas wie:

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(); 
      } 
     }); 
    } 
} 

So würden Sie müssen den Code in der Action ändern, um die Städte aus der Datenbank auf dem ausgewählten Land auf Basis zu erhalten.

+0

eigentlich verbinde ich die Land und Stadt Combo-Box mit MY_SQL Datenbank. jetzt, wie kann ich dieses Problem beheben? –

+0

@muhammadsalman, 'wie kann ich dieses Problem beheben?' - Welches Problem ??? Ihnen wurde Arbeitscode gegeben, der zeigt, wie das zweite Kombinationsfeld aktualisiert wird, wenn das Element in dem ersten Kombinationsfeld geändert wird. Alles, was Sie tun müssen, ist eine Liste der Städte aus Ihrer Datenbank zu erhalten. Nur Sie kennen die Struktur Ihrer Datenbank, deshalb kann ich Ihnen mit dem SQL nicht helfen. – camickr

Verwandte Themen