Skip to main content

is it possible to let jComboBox display the object's string field in ?

2 replies [Last post]
kevinatgz
Offline
Joined: 2009-04-30

is it possible to let jComboBox display the object's string field(without Override object class's toString() method)
of th DefaultComboBoxModel Vector? if writing my own dataModel a good solution? because there are more than one combobox in the panel, is it need to implements all the combox datamodel? or only one model are enough?
thanks in advance.

example:

1.
2. /*
3. * ComboBoxDemo.java
4. *
5. * Created on May 10, 2009, 2:06:18 AM
6. */
7.
8. package swingdemo;
9.
10. import java.util.Vector;
11. import javax.swing.JComboBox;
12.
13. /**
14. *
15. * @author kevin
16. */
17. public class ComboBoxDemo extends javax.swing.JFrame {
18.
19. /** Creates new form ComboBoxDemo */
20. public ComboBoxDemo() {
21. initComponents();
22. Vector v= new Vector();
23. // Vector cityName = new Vector();
24. for(int i=0;i<10;i++){
25. City c= new City(i,"city"+i);
26. v.add(c);
27. }
28. jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(v));
29. }
30.
31. /** This method is called from within the constructor to
32. * initialize the form.
33. * WARNING: Do NOT modify this code. The content of this method is
34. * always regenerated by the Form Editor.
35. */
36. @SuppressWarnings("unchecked")
37. //
38. private void initComponents() {
39.
40. jComboBox1 = new javax.swing.JComboBox();
41. jButton1 = new javax.swing.JButton();
42.
43. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
44.
45. jComboBox1.setEditable(true);
46. jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
47. jComboBox1.addActionListener(new java.awt.event.ActionListener() {
48. public void actionPerformed(java.awt.event.ActionEvent evt) {
49. jComboBox1ActionPerformed(evt);
50. }
51. });
52.
53. jButton1.setText("jButton1");
54. jButton1.addActionListener(new java.awt.event.ActionListener() {
55. public void actionPerformed(java.awt.event.ActionEvent evt) {
56. jButton1ActionPerformed(evt);
57. }
58. });
59.
60. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
61. getContentPane().setLayout(layout);
62. layout.setHorizontalGroup(
63. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
64. .addGroup(layout.createSequentialGroup()
65. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
66. .addGroup(layout.createSequentialGroup()
67. .addGap(149, 149, 149)
68. .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE))
69. .addGroup(layout.createSequentialGroup()
70. .addGap(109, 109, 109)
71. .addComponent(jButton1)))
72. .addContainerGap(70, Short.MAX_VALUE))
73. );
74. layout.setVerticalGroup(
75. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
76. .addGroup(layout.createSequentialGroup()
77. .addGap(98, 98, 98)
78. .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
79. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 78, Short.MAX_VALUE)
80. .addComponent(jButton1)
81. .addGap(76, 76, 76))
82. );
83.
84. pack();
85. }//
86.
87. private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
88. // TODO add your handling code here:
89. JComboBox cbb = (JComboBox)evt.getSource();
90. // System.out.println(cbb.getSelectedItem().toString());
91. }
92.
93. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
94. // TODO add your handling code here:
95. City city = (City) jComboBox1.getSelectedItem();
96. System.out.println(city.id);
97. System.out.println(jComboBox1.getSelectedItem());
98.
99. }
100.
101. /**
102. * @param args the command line arguments
103. */
104. public static void main(String args[]) {
105. java.awt.EventQueue.invokeLater(new Runnable() {
106. public void run() {
107. new ComboBoxDemo().setVisible(true);
108. }
109. });
110. }
111.
112. // Variables declaration - do not modify
113. private javax.swing.JButton jButton1;
114. private javax.swing.JComboBox jComboBox1;
115. // End of variables declaration
116.
117. }
118.
119.

/* * ComboBoxDemo.java * * Created on May 10, 2009, 2:06:18 AM */ package swingdemo; import java.util.Vector; import javax.swing.JComboBox; /** * * @author kevin */ public class ComboBoxDemo extends javax.swing.JFrame { /** Creates new form ComboBoxDemo */ public ComboBoxDemo() { initComponents(); Vector v= new Vector(); // Vector cityName = new Vector(); for(int i=0;i<10;i++){ City c= new City(i,"city"+i); v.add(c); } jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(v)); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // private void initComponents() { jComboBox1 = new javax.swing.JComboBox(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jComboBox1.setEditable(true); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); jComboBox1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox1ActionPerformed(evt); } }); jButton1.setText("jButton1"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(149, 149, 149) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGap(109, 109, 109) .addComponent(jButton1))) .addContainerGap(70, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(98, 98, 98) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 78, Short.MAX_VALUE) .addComponent(jButton1) .addGap(76, 76, 76)) ); pack(); }// private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: JComboBox cbb = (JComboBox)evt.getSource(); // System.out.println(cbb.getSelectedItem().toString()); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: City city = (City) jComboBox1.getSelectedItem(); System.out.println(city.id); System.out.println(jComboBox1.getSelectedItem()); } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new ComboBoxDemo().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JComboBox jComboBox1; // End of variables declaration }

1.
2.
3. package swingdemo;
4.
5. /**
6. *
7. * @author kevin
8. */
9. class City {
10. int id;
11. String cityName;
12. public City(int id ,String name){
13. this.id=id;
14. this.cityName =name;
15. }
16. // @Override
17. // public String toString(){
18. // return this.cityName;
19. // }
20. }
21.

Message was edited by: kevinatgz

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tarbo
Offline
Joined: 2006-12-18

I had a similar problem where I wanted a class of objects to behave/show differently in a JComboBox, but where I couldn't afford to change the toString() method (due to established semantics). The solution here was to use a new Renderer for the combo box.

All of the types that required special treatment were retrofitted to expose a method through their interface that returned a different string than toString() : toHumanReadableString(). The cell renderer checked the type of object it had to render and, if instanceof that interface, called toHumanReadableString() instead.

That may be easier/cleaner than changing your underlying data model.

Hope this helps,
Jonathan

kevinatgz
Offline
Joined: 2009-04-30

thanks,guy.
i've solved this by extends the jcombobox :
public class KeyValJComboBox extends JComboBox{
...

and,

public class KeyValueComboBoxModel extends AbstractListModel implements MutableComboBoxModel, Serializable {
Vector objects,keyObjects;
Object selectedObject,seletedKeyObject;

....

using a keyObjects to store the keys.