2011-06-05 68 views
3

我想2個jcomboboxes結合起來。 1組合框用於顯示費用類別。第二個組合框是從文本文件中讀取文件以顯示產品的類型。如果我更改第一個組合框,我希望第二個組合框將根據用戶在第一個組合框中選擇的內容進行更改。JComboBox的另一個變化JComboBox的

是否有任何機會,我仍然可以從文本文件加載其他組合框?該子項不會是數組,而是與之前相同,因爲它位於cboperson的代碼底部。

編輯的代碼:

private JComboBox cboCategory; 
private JComboBox cboPerson; 
private JComboBox cboItem; 
public String itemChange = "groceries.txt"; 

public ExpenditureTracker() {...... 


    String[] items = {"Select Item", "Groceries", "Bills", "Travelling", "Leasure", "Other"}; 
    mainComboBox = new JComboBox(items); 
    mainComboBox.addActionListener(this); 
    mainComboBox.addItemListener(this); 
    //prevent action events from being fired when the up/down arrow keys are used 
    //mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE); 
    mainComboBox.setBounds(113, 138, 85, 20); 
    importPanel.add(mainComboBox); 


    subComboBox = new JComboBox();// Create sub combo box with multiple models 
    subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4 
    subComboBox.addItemListener(this); 
    subComboBox.setBounds(113, 188, 85, 20); 
    importPanel.add(subComboBox); 


    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); 
    String[] subItems4 = {"Select Fruit", "Apple", "Orange", "Banana"}; 
    subItems.put(items[4], subItems3); 
    String[] subItems5 = {"Select Fruit", "Apple", "Orange", "Banana"}; 
    subItems.put(items[5], subItems3); 


    loadDataTocboPerson(); 
} 




private void loadDataToCboPerson() { 
    Scanner fileReader = new Scanner(getClass().getResourceAsStream(
      itemChange)); 
    try { 
     DefaultComboBoxModel model = new DefaultComboBoxModel(); 
     while (fileReader.hasNextLine()) { 
      model.addElement(fileReader.nextLine()); 
     } 
     cboItem.setModel(model); 
    } finally { 
     fileReader.close(); 
    } 
} 
+0

你調用loadDataToCboItem()?你應該在displaySelectedItem()方法中從你的actionPerformed方法中調用它。 – 2011-06-05 21:52:12

+0

請再編輯你的答案。代碼格式不正確,不清楚這些方法屬於哪個組合框。 – toto2 2011-06-05 21:54:36

回答

4

例如

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

public class ComboBoxTwo extends JFrame implements ActionListener, ItemListener { 

    private static final long serialVersionUID = 1L; 
    private JComboBox mainComboBox; 
    private JComboBox subComboBox; 
    private Hashtable<Object, Object> subItems = new Hashtable<Object, Object>(); 

    public ComboBoxTwo() { 
     String[] items = {"Select Item", "Color", "Shape", "Fruit"}; 
     mainComboBox = new JComboBox(items); 
     mainComboBox.addActionListener(this); 
     mainComboBox.addItemListener(this); 
     //prevent action events from being fired when the up/down arrow keys are used 
     //mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE); 
     getContentPane().add(mainComboBox, BorderLayout.WEST); 
     subComboBox = new JComboBox();// Create sub combo box with multiple models 
     subComboBox.setPrototypeDisplayValue("XXXXXXXXXX"); // JDK1.4 
     subComboBox.addItemListener(this); 
     getContentPane().add(subComboBox, BorderLayout.EAST); 
     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); 
//  mainComboBox.setSelectedIndex(1); 
    } 

    @Override 
    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)); 
     } 
    } 

    @Override 
    public void itemStateChanged(ItemEvent e) { 
     if (e.getStateChange() == ItemEvent.SELECTED) { 
      if (e.getSource() == mainComboBox) { 
       if (mainComboBox.getSelectedIndex() != 0) { 
        FirstDialog firstDialog = new FirstDialog(ComboBoxTwo.this, 
          mainComboBox.getSelectedItem().toString(), "Please wait, Searching for ..... "); 
       } 
      } 
     } 
    } 

    private class FirstDialog extends JDialog { 

     private static final long serialVersionUID = 1L; 

     FirstDialog(final Frame parent, String winTitle, String msgString) { 
      super(parent, winTitle); 
      setModalityType(Dialog.ModalityType.APPLICATION_MODAL); 
      JLabel myLabel = new JLabel(msgString); 
      JButton bNext = new JButton("Stop Processes"); 
      add(myLabel, BorderLayout.CENTER); 
      add(bNext, BorderLayout.SOUTH); 
      bNext.addActionListener(new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent evt) { 
        setVisible(false); 
       } 
      }); 
      javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        setVisible(false); 
       } 
      }); 
      t.setRepeats(false); 
      t.start(); 
      setLocationRelativeTo(parent); 
      setSize(new Dimension(400, 100)); 
      setVisible(true); 
     } 
    } 

    public static void main(String[] args) { 
     JFrame frame = new ComboBoxTwo(); 
     frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
} 
+0

你好,這是完美的沒有任何機會,串子項可以從文本文件中拉我的其他組合框??? – Petr 2011-06-06 01:47:06

+0

這是完美的沒有任何機會,我可以像以前一樣從文本文件中加載它們?私人無效loadDataToCboItem(){ \t \t掃描器的FileReader =新掃描儀(的getClass()的getResourceAsStream( \t \t \t \t itemChange)。); \t \t嘗試{ \t \t \t DefaultComboBoxModel model = new DefaultComboBoxModel(); \t \t \t而(fileReader.hasNextLine()){ \t \t \t \t model.addElement(fileReader.nextLine()); \t \t \t} \t \t \t subComboBox.setModel(model); \t \t} finally { \t \t \t fileReader.close(); \t \t} \t},只是將其更改爲子項,並添加有文本文件????非常感謝。 – Petr 2011-06-06 01:51:27

+0

@Petr:請編輯您的問題以包含新代碼示例。那看起來像是狗的早餐! – 2011-06-06 02:33:29

1

一方面,不==比較字符串,而是使用了equals或equalsIgnoreCase方法。例如,

更改此:

if (item == "Groceries") { 

這樣:

if ("Groceries".equalsIgnoreCase(item.toString())) { 

你要調用toString()的項目,以確保您比較字符串的字符串。在做任何這些之前,您還需要確保該項目不爲空。

0
if(jComboBox1.getSelectedItem() == "First Choice"){ 
    jComboBox2.removeAllItems(); 
    jComboBox2.addItem("First Choice Item 1"); 
} 
if(jComboBox1.getSelectedItem() == "Another Choice"){ 
    jComboBox2.removeAllItems(); 
    jComboBox2.addItem("Another Choice Item 1"); 
} 

或者你可以包括在陣列中的項目,如果它是太多了。