问题描述
我想使用String列表作为Java中jComboBox中各种选项的来源。
请参阅下文对于我的回答...考虑到这是未经测试的,只是一个例子。
你需要创建一个自定义的ComboBoxModel类似Chandru说,
使用 setModel()
方法在您的JComboBox上设置ComboBoxModel并使用((CustomComboBoxModel< String>)jComboBox.getModel (listOfThings);
这样的:
import java.util.List;
import javax.swing.ComboBoxModel;
/ **
*自定义实现{@code ComboBoxModel}以允许向列表中添加
*元素的列表。
* /
public interface CustomComboBoxModel< T> extends ComboBoxModel {
void add(List< T> elementsToAdd);
List< T> getElements();
}
p>
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractListModel;
/ **
*默认实现CustomComboBoxModel - 未测试。
* /
public class DefaultCustomComboBoxModel< T> extends AbstractListModel实现CustomComboBoxModel< T> {
List< T>对象;
T selectedObject;
/ **
*构造一个空的DefaultCustomComboBoxModel对象。
* /
public DefaultCustomComboBoxModel(){
objects = new ArrayList< T>();
}
/ **
*构造一个使用
*初始化的DefaultCustomComboBoxModel对象对象数组。
*
* @param items一个Object对象数组
* /
public DefaultCustomComboBoxModel(final T items []){
objects = new ArrayList< T> );
int i,c;
for(i = 0,c = items.length; i objects.add(items [i]);
}
if(getSize()> 0){
selectedObject = objects.get(0);
}
}
//实现javax.swing.ComboBoxModel
/ **
*设置所选项目的值。所选项目可以为空。
*确保{@code anObject}是T的实例,否则将抛出
* ClassCastException。
*< p>
* @param anObject组合框值或无选择的null。
* /
@Override
public void setSelectedItem(Object anObject){
if((selectedObject!= null&!selectedObject.equals(anObject))
|| selectedObject == null& anObject; = null){
selectedObject =(T)anObject;
fireContentsChanged(this,-1,-1);
}
}
//实现javax.swing.ComboBoxModel
@Override
public T getSelectedItem(){
return selectedObject;
}
//实现javax.swing.ListModel
@Override
public int getSize(){
return objects.size();
}
//实现javax.swing.ListModel
@Override
public T getElementAt(int index){
if(index> = 0 &&&< objects.size()){
return objects.get(index);
} else {
return null;
}
}
/ **
*返回列表中指定对象的索引位置。
*
* @param anObject
* @返回一个表示索引位置的int,其中0是
*第一个位置
* /
public int getIndexOf(T anObject){
return objects.indexOf(anObject);
}
//实现javax.swing.MutableComboBoxModel
public void addElement(T anObject){
objects.add(anObject);
fireIntervalAdded(this,objects.size() - 1,objects.size() - 1);
if(objects.size()== 1&& and selectedObject == null& an amp; anObject!= null){
setSelectedItem(anObject);
}
}
//实现javax.swing.MutableComboBoxModel
public void insertElementAt(T anObject,int index){
objects.add , 一个东西);
fireIntervalAdded(this,index,index);
}
//实现javax.swing.MutableComboBoxModel
public void removeElementAt(int index){
if(getElementAt(index)== selectedObject){
if(index == 0){
setSelectedItem(getSize()== 1?null:getElementAt(index + 1));
} else {
setSelectedItem(getElementAt(index - 1));
}
}
objects.remove(index);
fireIntervalRemoved(this,index,index);
}
//实现javax.swing.MutableComboBoxModel
public void removeElement(T anObject){
int index = objects.indexOf(anObject);
if(index!= -1){
removeElementAt(index);
}
}
/ **
*清空列表。
* /
public void removeAllElements(){
if(objects.size()> 0){
int firstIndex = 0;
int lastIndex = objects.size() - 1;
objects.clear();
selectedObject = null;
fireIntervalRemoved(this,firstIndex,lastIndex);
} else {
selectedObject = null;
}
}
@Override
public void add(List< T> elementsToAdd){
objects.addAll(elementsToAdd);
fireContentsChanged(this,-1,-1);
}
@Override
public List< T> getElements(){
return objects;
}
}
I wanted to use a String list as a source of various options in jComboBox in Java. Can you tell which method to use
Thanks
See Below for my answer... take into account this is untested and merely an example.
You need to create a custom implmentation of ComboBoxModel like Chandru said,Then set the ComboBoxModel on your JComboBox using the setModel()
method and add elements using ((CustomComboBoxModel<String>)jComboBox.getModel()).add(listOfThings);
Something like this:
import java.util.List;
import javax.swing.ComboBoxModel;
/**
* Custom Implementation of {@code ComboBoxModel} to allow adding a list of
* elements to the list.
*/
public interface CustomComboBoxModel<T> extends ComboBoxModel {
void add(List<T> elementsToAdd);
List<T> getElements();
}
and then implement the interface using something like this:
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractListModel;
/**
* Default Implementation of CustomComboBoxModel - untested.
*/
public class DefaultCustomComboBoxModel<T> extends AbstractListModel implements CustomComboBoxModel<T> {
List<T> objects;
T selectedObject;
/**
* Constructs an empty DefaultCustomComboBoxModel object.
*/
public DefaultCustomComboBoxModel() {
objects = new ArrayList<T>();
}
/**
* Constructs a DefaultCustomComboBoxModel object initialized with
* an array of objects.
*
* @param items an array of Object objects
*/
public DefaultCustomComboBoxModel(final T items[]) {
objects = new ArrayList<T>();
int i, c;
for (i = 0, c = items.length; i < c; i++) {
objects.add(items[i]);
}
if (getSize() > 0) {
selectedObject = objects.get(0);
}
}
// implements javax.swing.ComboBoxModel
/**
* Set the value of the selected item. The selected item may be null.
* Make sure {@code anObject} is an instance of T otherwise a
* ClassCastException will be thrown.
* <p>
* @param anObject The combo box value or null for no selection.
*/
@Override
public void setSelectedItem(Object anObject) {
if ((selectedObject != null && !selectedObject.equals(anObject))
|| selectedObject == null && anObject != null) {
selectedObject = (T) anObject;
fireContentsChanged(this, -1, -1);
}
}
// implements javax.swing.ComboBoxModel
@Override
public T getSelectedItem() {
return selectedObject;
}
// implements javax.swing.ListModel
@Override
public int getSize() {
return objects.size();
}
// implements javax.swing.ListModel
@Override
public T getElementAt(int index) {
if (index >= 0 && index < objects.size()) {
return objects.get(index);
} else {
return null;
}
}
/**
* Returns the index-position of the specified object in the list.
*
* @param anObject
* @return an int representing the index position, where 0 is
* the first position
*/
public int getIndexOf(T anObject) {
return objects.indexOf(anObject);
}
// implements javax.swing.MutableComboBoxModel
public void addElement(T anObject) {
objects.add(anObject);
fireIntervalAdded(this, objects.size() - 1, objects.size() - 1);
if (objects.size() == 1 && selectedObject == null && anObject != null) {
setSelectedItem(anObject);
}
}
// implements javax.swing.MutableComboBoxModel
public void insertElementAt(T anObject, int index) {
objects.add(index, anObject);
fireIntervalAdded(this, index, index);
}
// implements javax.swing.MutableComboBoxModel
public void removeElementAt(int index) {
if (getElementAt(index) == selectedObject) {
if (index == 0) {
setSelectedItem(getSize() == 1 ? null : getElementAt(index + 1));
} else {
setSelectedItem(getElementAt(index - 1));
}
}
objects.remove(index);
fireIntervalRemoved(this, index, index);
}
// implements javax.swing.MutableComboBoxModel
public void removeElement(T anObject) {
int index = objects.indexOf(anObject);
if (index != -1) {
removeElementAt(index);
}
}
/**
* Empties the list.
*/
public void removeAllElements() {
if (objects.size() > 0) {
int firstIndex = 0;
int lastIndex = objects.size() - 1;
objects.clear();
selectedObject = null;
fireIntervalRemoved(this, firstIndex, lastIndex);
} else {
selectedObject = null;
}
}
@Override
public void add(List<T> elementsToAdd) {
objects.addAll(elementsToAdd);
fireContentsChanged(this, -1, -1);
}
@Override
public List<T> getElements() {
return objects;
}
}
这篇关于使用字符串列表作为组合框的源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!