• Post Reply Bookmark Topic Watch Topic
  • New Topic

JComboBox with Objects

 
Patrik Nilsson
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, gurus,

I have a JComboBox where I want to show the name of a person but use the employeeId to do updates, deletes.

I have mostly used AWT components before where I show a String and use the index of that String to find out what id it belongs to.

Now I want to try to add an object to the model instead to use proper MVC.
My problem now is that I don't get anything of value since it is calling the toString method.

I know that I can override toString but I have several components and it will be a bit messy since I need to find out which combobox was selected in order to find out the appropriate toString method to call.

Is this the correct way to handle the problem or is there a better way?

Thanks in advance!
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Patrik Nilsson
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all thanks for replying!

My problem now is that I have 20 different properties that can be chosen in different comboboxes. Do I have to implement 20 different toString methods to assign the correct value then(or 1 toString method with 20 different cases) or is there a better way?
 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComboObjects implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JComboBox cb = (JComboBox)e.getSource();
ComboObject co = (ComboObject)cb.getSelectedItem();
System.out.println("co = " + co + "\n" + // calls toString method
"co.name = " + co.name + "\n" +
"co.id = " + co.id);
}

private JPanel getContent()
{
String[] names = { "John Monroe", "John Paul Jones", "Alexander Hamilton" };
int[] ids = { 1002, 15, 390 };
ComboObject[] cos = new ComboObject[names.length];
for(int j = 0; j < cos.length; j++)
cos[j] = new ComboObject(names[j], ids[j]);
JComboBox combo = new JComboBox(cos);
combo.addActionListener(this);
JPanel panel = new JPanel();
panel.add(combo);
return panel;
}

public static void main(String[] args)
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new ComboObjects().getContent(), "North");
f.setSize(240,100);
f.setLocation(200,200);
f.setVisible(true);
}
}

class ComboObject
{
String name;
String phone;
int id;

public static final int DISPLAY_NAME = 1;
public static final int DISPLAY_PHONE = 2;

/**
The p2D variable is sent
to the constructor --- so to get a
combo box showing the persons number ...

new ComboObject("jay","123-123-1234",ComboObject.DISPLAY_PHONE);

**/
public ComboObject(String name, int id, int p2d)
{
this.name = name;
this.id = id;
propertyToDisplay= p2d;
}

public String toString()
{
if(propertyToDisplay == DISPLAY_NAME)
return name;
else if(propertyToDisplay == DISPLAY_PHONE)
return phone;
else
return "I DONT UNDERSTAND THAT PROPERTY" ;
}
}
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a much simpler solution than the one proposed above (as far as I can tell): set a custom renderer on the comoboxes.



This way, you can put the ids directly into the combobox, and just need to customize the createLabelForId method to create the string you actually want to have displayed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!