2016-10-31 1 views
-1

터키에 두 번째 콤보를 채우는 방법은 무엇입니까? 첫 번째 콤보 칠면조를 선택할 때, Ankara Izmir는 두 번째 콤보로 채웠다.두 번째 콤보 상자 채우기 방법

String[] s = {"Turkey", "Rusia", "Italia"}; 
     for (String string : s) { 
      comboBox.addItem(string); 
     } 
     String x = String.valueOf(comboBox.getSelectedItem()); 
     if (x.equalsIgnoreCase("Turkey")) { 
      String[] s1 = {"Ankara", "Izmir"}; 
      for (String ss : s1) { 
       comboBox1.addItem(ss); 
      } 
     } 
+1

다른 모델의 배열을 변경할 수있는 ItemListener에서 다음 배열의 필요한 수를 준비하는 DeafultComboBoxModel (들)로 플레이해야하고 마지막으로 다른 JComboBox에, 아무것도 다른 모델을 추가 그렇지 않으면 여기에 몇 번 btw – mKorbel

+0

제발 무슨 뜻이야 "?" – mKorbel

+0

예를 보여주십시오. –

답변

0
 //Add this import 
     import javax.swing.DefaultComboBoxModel; 

     DefaultComboBoxModel dcm = new DefaultComboBoxModel();   

     String[] s = {"Turkey", "Rusia", "Italia"}; 
     // set the datamodel for the second combo box 

     jComboBox2.setModel(dcm); 
     // in the action perform method of the compbo box, add this code 


     private void comboBoxActionPerformed(java.awt.event.ActionEvent evt) {  

    String x = String.valueOf(comboBox.getSelectedItem()); 
    if (x.equalsIgnoreCase("Turkey")) { 
     String[] s1 = {"Ankara", "Izmir"}; 
     for (String ss : s1) { 
      dcm.addElement(ss); 
     } 
    jComboBox2.setModel(dcm); 

    } 
+0

고맙습니다. –

+0

새 데이터를 추가 할 때 데이터가 MySQL 데이터베이스에 저장되도록하려면 어떻게해야합니까? 이 자바 스윙을 사용하여? –

+0

제대로 Google 검색 후 별도의 질문으로 질문하십시오. – Ogbe

1

당신은 항목을 선택하면 작업을 수행하는 최초의 콤보 상자에 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(); 
      } 
     }); 
    } 
} 
관련 문제