Swing JComboBox problem.

I’m currently making a certain program. I have a JComboBox setup. Here is the code.


dropdown=new JComboBox<SpriteSheet>();

So I create a that box with spritesheet as its generic type or component or how the **** that’s called.

This is how I put objects to it.

dropdown.addItem(sheet);

The problem that causes me is this.

I think you understand what I want to do.

I would like to set like some kind of listener or something that would display the name of the object as I wish, not the actual object id or how is that called.

So it would look something like this:


dropdown.setTitleListener(new TitleListener() {
	public String getTitle(SpriteSheet sheet) {
		return "title";
	}
});

This is just pseudo-code and it doesn’t exist. I think you know what I want to achieve. I don’t even know how to name it…

You want to get the objects type in a human readable form?..

I want to be able to display any string for any object in the JComboBox. I want to be able to put different objects and be able to name them however I want, not the object name.

I mean, atm this is what it does.


renderString = object;

Which gives that objects package path, class name and object number or something.

I want it to do this:


renderString=titleListener.getTitle(object);

just override the toString method in Spritesheet
and the thing between the <> is called type argument

Thanks.

I’m still not quite sure why there isn’t something like I described (I will assume there isn’t until someone proves otherwise :D)

I would say this isn’t a good fix, a hack rather.

I think it’s what you looking for: http://tips4java.wordpress.com/2013/11/17/combo-box-with-custom-renderer/

Thanks I will look into it later.