2015-10-09 183 views
0

我一直在尋找一種方法來獲得JComboBox,其中列表中的項目正常顯示,但在編輯字段中只顯示一個數字。可編輯JComboBox與編輯字段中的不同文本

我碰到這個代碼來(僅略作修改,以我的需求):

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

public class ComboBoxItem extends JFrame implements ActionListener { 
    public ComboBoxItem() { 
     Vector model = new Vector(); 
     model.addElement(new Item(1, "car")); 
     model.addElement(new Item(2, "plane")); 
     model.addElement(new Item(3, "train")); 
     model.addElement(new Item(4, "boat")); 

     JComboBox comboBox; 

     comboBox = new JComboBox(model); 
     comboBox.addActionListener(this); 
     getContentPane().add(comboBox, BorderLayout.NORTH); 

     comboBox = new JComboBox(model); 

     // I want the comboBox editable. 
     //comboBox.setEditable(true); 

     comboBox.setRenderer(new ItemRenderer()); 
     comboBox.addActionListener(this); 
     getContentPane().add(comboBox, BorderLayout.SOUTH); 
    } 

    public void actionPerformed(ActionEvent e) { 
     JComboBox comboBox = (JComboBox)e.getSource(); 
     Item item = (Item)comboBox.getSelectedItem(); 
     System.out.println(item.getId() + " : " + item.getDescription()); 
    } 

    class ItemRenderer extends BasicComboBoxRenderer { 
     public Component getListCellRendererComponent(
      JList list, Object value, int index, 
      boolean isSelected, boolean cellHasFocus) { 
      super.getListCellRendererComponent(list, value, index, 
       isSelected, cellHasFocus); 

      if (value != null) { 
       Item item = (Item)value; 
       setText(item.getDescription().toUpperCase()); 
      } 

      if (index == -1) { 
       Item item = (Item)value; 
       setText("" + item.getId()); 
      } 
      return this; 
     } 
    } 

    class Item { 
     private int id; 
     private String description; 

     public Item(int id, String description) { 
      this.id = id; 
      this.description = description; 
     } 

     public int getId() { 
      return id; 
     } 

     public String getDescription() { 
      return description; 
     } 

     public String toString() { 
      return description; 
     } 
    } 

    public static void main(String[] args) { 
     JFrame frame = new ComboBoxItem(); 
     frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 
    }  
} 

這種運作良好,直到我增加ComboBox可編輯comboBox.setEditable(true);

背景信息:

我打算從數據庫在編輯領域的用戶類型填充對象的彈出列表。當用戶從彈出列表中選擇一個項目時,編輯字段應該只顯示對象的id,但彈出列表應該顯示更多信息,以便用戶可以做出明智的選擇。

任何人都可以幫助我做這項工作,無論是可編輯是打開還是關閉?

回答

0

你需要的是重載組合框的默認編輯器,因爲當你設置的組合框是可編輯的,它使用編輯器呈現您在下拉列表中選擇的內容。

下面是使用BasicComboBoxEditor實現的一種方法。你只需要重寫setItem方法。

comboBox.setEditor(new ItemEditor()); 

class ItemEditor extends BasicComboBoxEditor { 
    public void setItem(Object anObject) { 
     Item item = (Item) anObject; 
     editor.setText(item.getId() + ""); 
    } 
} 
+1

非常感謝。這個答案很簡短,乾淨而且直截了當。 – Dalendrion

1

如果我正確理解您的請求,您可以使用您自己的javax.swing.ComboBoxEditor

我剛剛拿出你的例子,並添加了一個非常快速的組合框編輯器,它使用javax.swing.JTextField並將其設置爲秒組合框編輯器。

也許這個例子展示了一種處理你的問題的方法。

import java.awt.BorderLayout; 
import java.awt.Component; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.Vector; 

import javax.swing.ComboBoxEditor; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JList; 
import javax.swing.JTextField; 
import javax.swing.plaf.basic.BasicComboBoxRenderer; 

public class ComboBoxItem extends JFrame implements ActionListener { 

    public ComboBoxItem() { 
     final Vector model = new Vector(); 
     model.addElement(new Item(1, "car")); 
     model.addElement(new Item(2, "plane")); 
     model.addElement(new Item(3, "train")); 
     model.addElement(new Item(4, "boat")); 

     JComboBox comboBox; 

     comboBox = new JComboBox(model); 
     comboBox.addActionListener(this); 
     this.getContentPane().add(comboBox, BorderLayout.NORTH); 

     comboBox = new JComboBox(model); 

     // I want the comboBox editable. 
     comboBox.setEditable(true); 

     comboBox.setRenderer(new ItemRenderer()); 
     comboBox.setEditor(new MyComboBoxEditor()); ///<------- Quick'n'Dirty editor added 
     comboBox.addActionListener(this); 
     this.getContentPane().add(comboBox, BorderLayout.SOUTH); 
    } 

    @Override 
    public void actionPerformed(final ActionEvent e) { 
     final JComboBox comboBox = (JComboBox) e.getSource(); 
     final Item item = (Item) comboBox.getSelectedItem(); 
     System.out.println(item.getId() + " : " + item.getDescription()); 
    } 

    class MyComboBoxEditor implements ComboBoxEditor { 

     JTextField editor; 
     Item  editedItem; 

     @Override 
     public void addActionListener(final ActionListener l) { 

     } 

     @Override 
     public Component getEditorComponent() { 
      if (this.editor == null) { 
       this.editor = new JTextField(); 
      } 
      return this.editor; 
     } 

     @Override 
     public Object getItem() { 
      return this.editedItem; 
     } 

     @Override 
     public void removeActionListener(final ActionListener l) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void selectAll() { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void setItem(final Object anObject) { 
      this.editedItem = (Item) anObject; 
      this.editor.setText(String.valueOf(this.editedItem.getId())); 
     } 

    } 

    class ItemRenderer extends BasicComboBoxRenderer { 

     @Override 
     public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, 
       final boolean cellHasFocus) { 
      super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 
      if (value != null) { 
       final Item item = (Item) value; 
       this.setText(item.getDescription().toUpperCase()); 
      } 

      if (index == -1) { 
       final Item item = (Item) value; 
       this.setText("" + item.getId()); 
      } 

      return this; 
     } 
    } 

    class Item { 

     private final int id; 
     private final String description; 

     public Item(final int id, final String description) { 
      this.id = id; 
      this.description = description; 
     } 

     public int getId() { 
      return this.id; 
     } 

     public String getDescription() { 
      return this.description; 
     } 

     @Override 
     public String toString() { 
      return this.description; 
     } 
    } 

    public static void main(final String[] args) { 
     final JFrame frame = new ComboBoxItem(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

您可以找到線47和88

問候之間的編輯