Swing Combo Boxes

N

newsguy

Hi all;

I'm having a problem setting a combo box model. I have one that works:
stateComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
"AR Arkansas", "AK Alaska" ,ect...}))

Now what I would like is to have an array with just certain states in it,
that would be customizable by the user. But my code :

stateComboBox.setModel.setModel(states[]);

Doesn't work. I know there's a simple solution but what is it?

Thanks for your time.

Willie
 
M

moulio

Are you abbreviating the second line of code, because it is far
different from the first. It seems that if you follow the pattern of
the first it should be:

stateComboBox.setModel(new javax.swing.DefaultComboBoxModel(states[]))
;

.... instead of:

stateComboBox.setModel.setModel(states[]);

On a different tack, what about using just one DefaultComboBoxModel,
instead of instanciating a temporary one when you set the model. You
can remove or add elements of it.

Probably not the most helpful post, but I hope it gives you some ideas

-Thomas
 
M

Mike

newsguy said:
Now what I would like is to have an array with just certain states in it,
that would be customizable by the user. But my code :

stateComboBox.setModel.setModel(states[]);

Doesn't work. I know there's a simple solution but what is it?

Why don't you just create your own combo box model class for it?
All you have to do is to override some methods in DefaultComboBoxModel.

Ex:

private class MyComboBoxModel
extends DefaultComboBoxModel
{

private ArrayList content; // this is the model of combo ( it can not
be an ArrayList - you can define your own data structure )


public MyComboBoxModel(ArrayList content)
{
this.content=content;
}

public Object getElementAt(int index)
{
String return_value;

// getting value from model ( somehow )

return return_value; ( if your combo is for displaying text
information )
}

public int getSize()
{
return content.size();
}

// here you can add some methods to manage the model

}

All is imple enough. :)
 
R

Rogan Dawes

newsguy said:
Hi all;

I'm having a problem setting a combo box model. I have one that works:
stateComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
"AR Arkansas", "AK Alaska" ,ect...}))

Now what I would like is to have an array with just certain states in it,
that would be customizable by the user. But my code :

stateComboBox.setModel.setModel(states[]);

Doesn't work. I know there's a simple solution but what is it?

Thanks for your time.

Willie

Dunno if this is immediately relevant, but I thought I'd put it out here
anyway.

The only difference between a Swing ListModel and a Swing ComboBoxModel
is the concept of a selected item.

Here is a wrapper for a ListModel that implements a ComboBoxModel. This
means that you can use whatever implementation of ListModel that you
prefer to provide a ComboBoxModel.



import javax.swing.AbstractListModel;
import javax.swing.ListModel;
import javax.swing.ComboBoxModel;

import javax.swing.event.ListDataListener;
import javax.swing.event.ListDataEvent;

public class ListComboBoxModel
extends AbstractListModel
implements ComboBoxModel {

private ListModel _list;
private Object _selected = null;

/** Creates a new instance of ListComboBoxModel */
public ListComboBoxModel(ListModel list) {
_list = list;
_list.addListDataListener(new MyListener());
}

public Object getElementAt(int index) {
return _list.getElementAt(index);
}

public Object getSelectedItem() {
return _selected;
}

public int getSize() {
return _list.getSize();
}

public void setSelectedItem(Object anItem) {
if (_selected != null) {
fireContentsChanged(this, 0, getSize());
}
_selected = anItem;
if (_selected != null) {
fireContentsChanged(this, 0, getSize());
}
}

private class MyListener implements ListDataListener {

public void contentsChanged(ListDataEvent e) {
fireContentsChanged(
ListComboBoxModel.this,
e.getIndex0(),
e.getIndex1()
);
}

public void intervalAdded(ListDataEvent e) {
fireIntervalAdded(
ListComboBoxModel.this,
e.getIndex0(),
e.getIndex1()
);
}

public void intervalRemoved(ListDataEvent e) {
fireIntervalRemoved(
ListComboBoxModel.this,
e.getIndex0(),
e.getIndex1()
);
}

}
}

Rogan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top