2016-08-31 8 views
0

アクションに入れたいものは何ですか?コンボボックスです。私の問題は、コンボボックスからアイテムを選択すると、リストと表示されます。

私のコードより、cmbCollegeには、 "Business"、 "Computer"、 "Engineer"}のリストがあります。 cmbCollegeのアイテムを選択すると、のリストが表示されます。私はcmbCollegeから"コンピュータ"を選択した場合、例えば、それは、strComputer [] = { "CS"、 "IT"}を含有するとき、私は他のものにそれを選択し、リスト、lstDepartmentを示すであろうにstrEngineerにstrBusiness"エンジニア" "ビジネス" のように対応する部署を示しています。ここでJComboBoxとJList

は私のコードです:

DefaultListModel listModel = new DefaultListModel(); 
listModel.addElement("Jane Doe"); 
listModel.addElement("John Smith"); 
listModel.addElement("Kathy Green"); 

JListDefaultListModelを設定します。

import javax.swing.*; 
import javax.swing.event.*; 
import java.awt.*; 
import java.awt.event.*; 

public class OOP_College extends JFrame implements ActionListener, ListSelectionListener 
{ 
    String strCollege[] = {"Business","Computer", "Engineer"}; 
    String strBusiness[] = {"Management", "Marketing"}; 
    String strComputer[] = {"IT", "CS"}; 
    String strEngineer[] = {"Mechanical", "Electrical", "Electronics"}; 

    JPanel pnlCollege, pnlDepartment, pnlFaculty; 
    JLabel lblCollege, lblChoice, lblDepartment, lblFaculty; 
    JComboBox cmbCollege; 
    JList lstDepartment; 
    JButton btnAdd, btnShow; 
    JTextField tfFaculty; 
    JTextArea taFaculty; 
    GridBagConstraints gbcDepartment = new GridBagConstraints(); 

    public OOP_College() 
    { 
     // TODO Auto-generated constructor stub 
     pnlCollege = new JPanel(new FlowLayout()); 
     lblCollege = new JLabel("Collge: "); 
     cmbCollege = new JComboBox(strCollege); 
     lblChoice = new JLabel(" - Choice"); 
     pnlCollege.add(lblCollege); 
     pnlCollege.add(cmbCollege); 
     pnlCollege.add(lblChoice); 

     cmbCollege.setSelectedItem(0); 
     cmbCollege.addActionListener(this); 

     add(pnlCollege, BorderLayout.NORTH); 

     pnlDepartment = new JPanel(new GridBagLayout()); 
     lblDepartment = new JLabel("Department:"); 
     lstDepartment= new JList(); 

     gbcDepartment.gridx = 0; 
     gbcDepartment.gridy = 0; 
     pnlDepartment.add(lblDepartment, gbcDepartment); 

     gbcDepartment.gridx = 1; 
     gbcDepartment.gridy = 1; 
     pnlDepartment.add(lstDepartment, gbcDepartment); 

     add(pnlDepartment, BorderLayout.CENTER); 

     pnlFaculty = new JPanel(new FlowLayout()); 
     lblFaculty = new JLabel("Faculty Name:"); 
     tfFaculty = new JTextField(20); 
     btnAdd = new JButton("ADD"); 
     btnShow = new JButton("SHOW"); 
     taFaculty = new JTextArea(10,20); 
     taFaculty.setEnabled(false); 
     btnAdd.addActionListener(this); 
     btnShow.addActionListener(this); 
     pnlFaculty.add(lblFaculty); 
     pnlFaculty.add(tfFaculty); 
     pnlFaculty.add(btnAdd); 
     pnlFaculty.add(btnShow); 
     pnlFaculty.add(taFaculty); 

     add(pnlFaculty, BorderLayout.SOUTH); 
    } 

    public static void main(String[] args) 
    { 
     OOP_College oop = new OOP_College(); 
     oop.setSize(500,350); 
     oop.setVisible(true); 
     oop.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) 
    { 
     // TODO Auto-generated method stub 
     if(e.getSource() == cmbCollege) 
     { 
      JComboBox cb = (JComboBox) e.getSource(); 
      String strSelected = (String) cb.getSelectedItem(); 
      switch (strSelected) 
      { 
      } 
     } 
     else if(e.getSource() == btnAdd) 
     { 
      taFaculty.setText(taFaculty.getText() + tfFaculty.getText() + "\n"); 
     } 
     else if (e.getSource() == btnShow) 
     { 
     } 

    } 

    @Override 
    public void valueChanged(ListSelectionEvent e) 
    { 
     // TODO Auto-generated method stub 

    } 
} 
+0

問題を小さな例で表示できますか? http://stackoverflow.com/help/mcveを見てください。 –

答えて

0

あなたがここにDefaultListModelを使用しItemsinListを追加することができますドキュメントは click here DefaultListModel

例です。

JList list = new JList(listModel); 

となり、ロジックをonclicklistener ComboBoxに書き込むことができます。ここで

switch (strSelected) { 
     case "Computer": 
      for (int i = 0; i < strComputer.length; i++) { 
       model.addElement(strComputer[i]); 
      } 
      break; 
     case "Business": 
      for (int i = 0; i < strBusiness.length; i++) { 
       model.addElement(strBusiness[i]); 
      } 
      break; 
     case "Engineer": 
      for (int i = 0; i < strEngineer.length; i++) { 
       model.addElement(strEngineer[i]); 
      } 
      break; 
    } 

は例です:

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.DefaultListModel; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JList; 
import javax.swing.JPanel; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 
import javax.swing.event.ListSelectionEvent; 
import javax.swing.event.ListSelectionListener; 

public class s extends JFrame implements ActionListener, ListSelectionListener { 
String strCollege[] = {"Business", "Computer", "Engineer"}; 
String strBusiness[] = {"Management", "Marketing"}; 
String strComputer[] = {"IT", "CS"}; 
String strEngineer[] = {"Mechanical", "Electrical", "Electronics"}; 
DefaultListModel model; 


JPanel pnlCollege, pnlDepartment, pnlFaculty; 
JLabel lblCollege, lblChoice, lblDepartment, lblFaculty; 
JComboBox cmbCollege; 
JList lstDepartment; 
JButton btnAdd, btnShow; 
JTextField tfFaculty; 
JTextArea taFaculty; 
GridBagConstraints gbcDepartment = new GridBagConstraints(); 

public s() { 
    // TODO Auto-generated constructor stub 
    pnlCollege = new JPanel(new FlowLayout()); 
    lblCollege = new JLabel("Collge: "); 
    cmbCollege = new JComboBox(strCollege); 
    lblChoice = new JLabel(" - Choice"); 
    pnlCollege.add(lblCollege); 
    pnlCollege.add(cmbCollege); 
    pnlCollege.add(lblChoice); 

    cmbCollege.setSelectedItem(0); 
    cmbCollege.addActionListener(this); 

    add(pnlCollege, BorderLayout.NORTH); 

    model = new DefaultListModel(); 

    pnlDepartment = new JPanel(new GridBagLayout()); 
    lblDepartment = new JLabel("Department:"); 
    lstDepartment = new JList(model); 

    gbcDepartment.gridx = 0; 
    gbcDepartment.gridy = 0; 
    pnlDepartment.add(lblDepartment, gbcDepartment); 

    gbcDepartment.gridx = 1; 
    gbcDepartment.gridy = 1; 
    pnlDepartment.add(lstDepartment, gbcDepartment); 

    add(pnlDepartment, BorderLayout.CENTER); 

    pnlFaculty = new JPanel(new FlowLayout()); 
    lblFaculty = new JLabel("Faculty Name:"); 
    tfFaculty = new JTextField(20); 
    btnAdd = new JButton("ADD"); 
    btnShow = new JButton("SHOW"); 
    taFaculty = new JTextArea(10, 20); 
    taFaculty.setEnabled(false); 
    btnAdd.addActionListener(this); 
    btnShow.addActionListener(this); 
    pnlFaculty.add(lblFaculty); 
    pnlFaculty.add(tfFaculty); 
    pnlFaculty.add(btnAdd); 
    pnlFaculty.add(btnShow); 
    pnlFaculty.add(taFaculty); 

    add(pnlFaculty, BorderLayout.SOUTH); 
} 

public static void main(String[] args) { 
    s oop = new s(); 
    oop.setSize(500, 350); 
    oop.setVisible(true); 
    oop.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

@Override 
public void actionPerformed(ActionEvent e) { 
    // TODO Auto-generated method stub 
    if (e.getSource() == cmbCollege) { 
     JComboBox cb = (JComboBox) e.getSource(); 
     String strSelected = (String) cb.getSelectedItem().toString(); 
     model.removeAllElements(); 
     switch (strSelected) { 
      case "Computer": 
       for (int i = 0; i < strComputer.length; i++) { 
        model.addElement(strComputer[i]); 
       } 
       break; 
      case "Business": 
       for (int i = 0; i < strBusiness.length; i++) { 
        model.addElement(strBusiness[i]); 
       } 
       break; 
      case "Engineer": 
       for (int i = 0; i < strEngineer.length; i++) { 
        model.addElement(strEngineer[i]); 
       } 
       break; 

     } 
    } else if (e.getSource() == btnAdd) { 
     taFaculty.setText(taFaculty.getText() + tfFaculty.getText() + "\n"); 
    } else if (e.getSource() == btnShow) { 
    } 

} 

@Override 
public void valueChanged(ListSelectionEvent e) { 
    // TODO Auto-generated method stub 

    } 
} 
+0

ありがとうございます!それは本当に私の多くを助けます! :) –

0

私はあなたのコードで表示されるものと、私はあなたが選択した大学に基づいて値をlstDepartmentを移入したいと思います。 actionPerformedメソッドでは、スイッチは次のようになります

switch (strSelected) { 
    case "Business": 
     lstDepartment.setListData(strBusiness); 
     break; 
    case "Computer": 
     lstDepartment.setListData(strComputer); 
     break; 
    case "Engineer": 
     lstDepartment.setListData(strEngineer); 
     break; 
} 

選択された値に基づいて、リストは、対応するアレイからの値で更新されます。

Map<String,String[]>コンボボックス(マップキー)と関連する部門(配列)に表示される値を保存する場所を作成することをお勧めします。ベローはコードがどのように見えるかです:

// The map declaration 
private final Map<String, String[]> colleges = new HashMap<>(); 

// Populating the map and the ComboBox 
colleges.put("", new String[]{""}); // Empty combo box selection 
colleges.put("Business", new String[]{"Management", "Marketing"}); 
colleges.put("Computer", new String[]{"IT", "CS"}); 
colleges.put("Engineer", new String[]{"Mechanical", "Electrical", "Electronics"}); 
cmbCollege = new JComboBox(colleges.keySet().toArray()); 

// The refactored switch 
lstDepartment.setListData(colleges.get(strSelected)); 
+0

ありがとうございました、それは私がなりたかったように正確に動作します:) –

+0

うれしいです。あなたは受け入れられた答えに印を付けるべきです。 – Slimu