2016-05-14 134 views
2

我有人[]與三個人(P1,P2,P3)。人員類別有兩個屬性名稱電子郵件共享相同的模型JComboBoxes之間的兩個

我想補充一個JComboBox中和人[]的所有名稱中的所有電子郵件JComboBox的另一個。

我用下面的代碼。

Person p1 = new Person("Smith", "[email protected]"); 
    Person p2 = new Person("Tom", "[email protected]"); 
    Person p3 = new Person("John","[email protected]"); 

    Person[] per_arr = new Person[] { p1, p2, p3}; 

    JFrame frame = new JFrame(); 
    JPanel panel = new JPanel(); 
    JComboBox<String> combo1 = new JComboBox<String>(); 
    JComboBox<String> combo2 = new JComboBox<String>(); 

    for (Person p : per_arr) { 
     combo1.addItem(p.getName()); 
     combo2.addItem(p.getEmail()); 
    } 
    panel.add(combo1); 
    panel.add(combo2); 
    frame.setContentPane(panel); 
    frame.pack(); 
    frame.setVisible(true); 

但我不想這樣用。我想用兩個相同型號的組合框。我試圖與DefaultComboBoxModel和覆蓋getElementAt()方法如下所示。

public class MyModel extends DefaultComboBoxModel<Object> { 

public MyModel(Object[] items) { 
    super(items); 
} 

@Override 
public Object getElementAt(int index) { 
    if (super.getElementAt(index) instanceof Person) { 
     return (Person)super.getElementAt(index); 
    } else { 
     return null; 
    } 
} 

}

以上的ComboBoxModel給我只有Person對象。

問題是如何在一個JComboBox中添加Person []的所有名稱,以及使用相同ComboBoxModel的另一個JComboBox 中的所有電子郵件。

回答

5

使用ComboBoxModel一個實例和JComboBox,每個具有相同的模型兩個實例。讓每一個JComboBox有一個自定義renderer顯示爲JComboBox所需Person屬性。

在下面的例子中,每個組合獲取自己實現該strategy pattern單個渲染器的實例,傳遞一個Function<Person, String>,其選擇時渲染器被稱爲正確的屬性:

DefaultComboBoxModel model = new DefaultComboBoxModel(…); 
JComboBox<String> combo1 = new JComboBox<>(model); 
combo1.setRenderer(new PersonRenderer(Person::getName)); 
JComboBox<String> combo2 = new JComboBox<>(model); 
combo2.setRenderer(new PersonRenderer(Person::getEmail)); 

image

import java.awt.Component; 
import java.awt.EventQueue; 
import java.awt.FlowLayout; 
import java.util.function.Function; 
import javax.swing.DefaultComboBoxModel; 
import javax.swing.DefaultListCellRenderer; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JList; 

/** 
* @see http://stackoverflow.com/a/37222598/230513 
*/ 
public class ComboRendererTest { 

    private void display() { 
     JFrame f = new JFrame("Test"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setLayout(new FlowLayout()); 
     DefaultComboBoxModel model = new DefaultComboBoxModel(new Person[]{ 
      new Person("Alpher", "[email protected]"), 
      new Person("Bethe", "[email protected]"), 
      new Person("Gammow", "[email protected]")}); 
     JComboBox<String> combo1 = new JComboBox<>(model); 
     combo1.setRenderer(new PersonRenderer(Person::getName)); 
     JComboBox<String> combo2 = new JComboBox<>(model); 
     combo2.setRenderer(new PersonRenderer(Person::getEmail)); 
     f.add(combo1); 
     f.add(combo2); 
     f.pack(); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    private static class PersonRenderer extends DefaultListCellRenderer { 

     Function<Person, String> personAttribute; 

     public PersonRenderer(Function<Person, String> personAttribute) { 
      this.personAttribute = personAttribute; 
     } 

     @Override 
     public Component getListCellRendererComponent(JList<?> list, Object 
      value, int index, boolean isSelected, boolean cellHasFocus) { 
      JLabel l = (JLabel) super.getListCellRendererComponent(
       list, value, index, isSelected, cellHasFocus); 
      Person p = (Person) value; 
      l.setText(personAttribute.apply(p)); 
      return l; 
     } 
    } 

    private static class Person { 

     private final String name; 
     private final String email; 

     public Person(String name, String email) { 
      this.name = name; 
      this.email = email; 
     } 

     public String getName() { 
      return name; 
     } 

     public String getEmail() { 
      return email; 
     } 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new ComboRendererTest()::display); 
    } 
} 
+1

感謝您的詳細解釋....你幫了我很多:-) –