2011-11-30 65 views
22

我想通過值而不是索引在JComboBox中設置選定的索引。怎麼做?示例如何通過值設置選擇的索引JComboBox

public class ComboItem { 

    private String value; 
    private String label; 

    public ComboItem(String value, String label) { 
     this.value = value; 
     this.label = label; 
    } 

    public String getValue() { 
     return this.value; 
    } 

    public String getLabel() { 
     return this.label; 
    } 

    @Override 
    public String toString() { 
     return label; 
    } 
} 

JComboBox test = new JComboBox(); 
test.addItem(new ComboItem(0, "orange")); 
test.addItem(new ComboItem(1, "pear")); 
test.addItem(new ComboItem(2, "apple")); 
test.addItem(new ComboItem(3, "banana")); 
test.setSelectedItem("banana"); 

好吧,我修改了我的問題了一下。我忘記了我的JComboBox內有一個自定義項目,這使得它更加困難。我不能做setSelectedItem,因爲我在每個項目中都有一個ComboItem。那麼,我該如何做到這一點?

+1

不使用包裝的物品。而是實現一個自定義的ListCellRenderer,它將組件映射到它的字符串表示 – kleopatra

回答

36

setSelectedItem("banana")。你可以通過閱讀the javadoc找到它。

編輯:既然你改變了問題,我會改變我的答案。

如果要選擇具有「香蕉」標籤的項目,那麼有兩種解決方案:

  1. 迭代通過項目找到具有給定的一個(或一個索引)標籤,然後調用在ComboItemsetSelectedItem(theFoundItem)(或setSelectedIndex(theFoundIndex)
  2. 覆蓋equalshashCode,使得具有相同名稱的兩個ComboItem實例是相等的,並且簡單地使用setSelectedItem(new ComboItem(anyNumber, "banana"));
+0

Im很抱歉,我錯過了那個明顯的功能,但我更新了我的問題,因爲我忘記了我的組合框中沒有「正常」項目 –

+3

第二個解決方案非常有用,謝謝。 –

5

爲什麼不取一個集合(可能是一個Map,如HashMap),並將其用作實現ComboBoxModel接口的組合框模型類的核心?然後你可以通過他們的關鍵字串而不是ints輕鬆地訪問你的組合框的項目。

例如...

import java.util.HashMap; 
import java.util.Map; 

import javax.swing.ComboBoxModel; 
import javax.swing.event.ListDataListener; 

public class MyComboModel<K, V> implements ComboBoxModel { 
    private Map<K, V> nucleus = new HashMap<K, V>(); 

    // ... any constructors that you want would go here 

    public void put(K key, V value) { 
     nucleus.put(key, value); 
    } 

    public V get(K key) { 
     return nucleus.get(key); 
    } 

    @Override 
    public void addListDataListener(ListDataListener arg0) { 
     // TODO Auto-generated method stub 

    } 

    // ... plus all the other methods required by the interface 
} 
4

例如

enter image description here

import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.SwingUtilities; 

public class ComboboxExample { 

    private JFrame frame = new JFrame("Test"); 
    private JComboBox comboBox = new JComboBox(); 

    public ComboboxExample() { 
     createGui(); 
    } 

    private void createGui() { 
     comboBox.addItem("One"); 
     comboBox.addItem("Two"); 
     comboBox.addItem("Three"); 
     JButton button = new JButton("Show Selected"); 
     button.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       JOptionPane.showMessageDialog(frame, "Selected item: " + comboBox.getSelectedItem()); 
       javax.swing.SwingUtilities.invokeLater(new Runnable() { 

        @Override 
        public void run() { 
         comboBox.requestFocus(); 
         comboBox.requestFocusInWindow(); 
        } 
       }); 
      } 
     }); 
     JButton button1 = new JButton("Append Items"); 
     button1.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       appendCbItem(); 
      } 
     }); 
     JButton button2 = new JButton("Reduce Items"); 
     button2.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       reduceCbItem(); 
      } 
     }); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLayout(new GridLayout(4, 1)); 
     frame.add(comboBox); 
     frame.add(button); 
     frame.add(button1); 
     frame.add(button2); 
     frame.setLocation(200, 200); 
     frame.pack(); 
     frame.setVisible(true); 
     selectFirstItem(); 
    } 

    public void appendCbItem() { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       comboBox.addItem("Four"); 
       comboBox.addItem("Five"); 
       comboBox.addItem("Six"); 
       comboBox.setSelectedItem("Six"); 
       requestCbFocus(); 
      } 
     }); 
    } 

    public void reduceCbItem() { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       comboBox.removeItem("Four"); 
       comboBox.removeItem("Five"); 
       comboBox.removeItem("Six"); 
       selectFirstItem(); 
      } 
     }); 
    } 

    public void selectFirstItem() { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       comboBox.setSelectedIndex(0); 
       requestCbFocus(); 
      } 
     }); 
    } 

    public void requestCbFocus() { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       comboBox.requestFocus(); 
       comboBox.requestFocusInWindow(); 
      } 
     }); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       ComboboxExample comboboxExample = new ComboboxExample(); 
      } 
     }); 
    } 
} 
11
public static void setSelectedValue(JComboBox comboBox, int value) 
    { 
     ComboItem item; 
     for (int i = 0; i < comboBox.getItemCount(); i++) 
     { 
      item = (ComboItem)comboBox.getItemAt(i); 
      if (item.getValue().equalsIgnoreCase(value)) 
      { 
       comboBox.setSelectedIndex(i); 
       break; 
      } 
     } 
    } 

希望這有助於:)

6

您應該使用模型

comboBox.getModel().setSelectedItem(object); 
1

上來後,只需撥打comboBox.updateUI()摹comboBox.setSelectedItem或comboBox.setSelectedIndex或comboModel.setSelectedItem

+0

您能詳細解釋一下您提供的解決方案嗎? – abarisone

2

設置時,組合框是由一些類的構造函數填充選定的項目(如@milosz發佈)正確的做法:

combobox.getModel().setSelectedItem(new ClassName(parameter1, parameter2)); 

在你情況下,代碼如下:

test.getModel().setSelectedItem(new ComboItem(3, "banana")); 
1
public boolean preencherjTextCombox(){ 
     int x = Integer.parseInt(TableModelo.getModel().getValueAt(TableModelo.getSelectedRow(),0).toString()); 

     modeloobj = modelosDAO.pesquisar(x); 
     Combmarcass.getModel().setSelectedItem(modeloobj.getMarca()); 
     txtCodigo.setText(String.valueOf(modeloobj.getCodigo())); 
     txtDescricao.setText(String.valueOf(modeloobj.getDescricao())); 
     txtPotencia.setText(String.valueOf(modeloobj.getPotencia())); 

     return true; 
    } 
相關問題