Java 在本例中,如何从目标列表中获取所有值
我发现它有两个Java 在本例中,如何从目标列表中获取所有值,java,forms,swing,netbeans,Java,Forms,Swing,Netbeans,我发现它有两个JLists。如何从目标JList获取所有值?我是Java新手,我想从第二个列表中获取所有值,以便进行一些测试来研究 import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; impor
JList
s。如何从目标JList
获取所有值?我是Java新手,我想从第二个列表中获取所有值,以便进行一些测试来研究
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
public class DualListBox extends JPanel {
private static final Insets EMPTY_INSETS = new Insets(0, 0, 0, 0);
private static final String ADD_BUTTON_LABEL = "Add >>";
private static final String REMOVE_BUTTON_LABEL = "<< Remove";
private static final String DEFAULT_SOURCE_CHOICE_LABEL = "Available Choices";
private static final String DEFAULT_DEST_CHOICE_LABEL = "Your Choices";
private JLabel sourceLabel;
private JList sourceList;
private SortedListModel sourceListModel;
private JList destList;
private SortedListModel destListModel;
private JLabel destLabel;
private JButton addButton;
private JButton removeButton;
public DualListBox() {
initScreen();
}
public String getSourceChoicesTitle() {
return sourceLabel.getText();
}
public void setSourceChoicesTitle(String newValue) {
sourceLabel.setText(newValue);
}
public String getDestinationChoicesTitle() {
return destLabel.getText();
}
public void setDestinationChoicesTitle(String newValue) {
destLabel.setText(newValue);
}
public void clearSourceListModel() {
sourceListModel.clear();
}
public void clearDestinationListModel() {
destListModel.clear();
}
public void addSourceElements(ListModel newValue) {
fillListModel(sourceListModel, newValue);
}
public void setSourceElements(ListModel newValue) {
clearSourceListModel();
addSourceElements(newValue);
}
public void addDestinationElements(ListModel newValue) {
fillListModel(destListModel, newValue);
}
private void fillListModel(SortedListModel model, ListModel newValues) {
int size = newValues.getSize();
for (int i = 0; i < size; i++) {
model.add(newValues.getElementAt(i));
}
}
public void addSourceElements(Object newValue[]) {
fillListModel(sourceListModel, newValue);
}
public void setSourceElements(Object newValue[]) {
clearSourceListModel();
addSourceElements(newValue);
}
public void addDestinationElements(Object newValue[]) {
fillListModel(destListModel, newValue);
}
private void fillListModel(SortedListModel model, Object newValues[]) {
model.addAll(newValues);
}
public Iterator sourceIterator() {
return sourceListModel.iterator();
}
public Iterator destinationIterator() {
return destListModel.iterator();
}
public void setSourceCellRenderer(ListCellRenderer newValue) {
sourceList.setCellRenderer(newValue);
}
public ListCellRenderer getSourceCellRenderer() {
return sourceList.getCellRenderer();
}
public void setDestinationCellRenderer(ListCellRenderer newValue) {
destList.setCellRenderer(newValue);
}
public ListCellRenderer getDestinationCellRenderer() {
return destList.getCellRenderer();
}
public void setVisibleRowCount(int newValue) {
sourceList.setVisibleRowCount(newValue);
destList.setVisibleRowCount(newValue);
}
public int getVisibleRowCount() {
return sourceList.getVisibleRowCount();
}
public void setSelectionBackground(Color newValue) {
sourceList.setSelectionBackground(newValue);
destList.setSelectionBackground(newValue);
}
public Color getSelectionBackground() {
return sourceList.getSelectionBackground();
}
public void setSelectionForeground(Color newValue) {
sourceList.setSelectionForeground(newValue);
destList.setSelectionForeground(newValue);
}
public Color getSelectionForeground() {
return sourceList.getSelectionForeground();
}
private void clearSourceSelected() {
Object selected[] = sourceList.getSelectedValues();
for (int i = selected.length - 1; i >= 0; --i) {
sourceListModel.removeElement(selected[i]);
}
sourceList.getSelectionModel().clearSelection();
}
private void clearDestinationSelected() {
Object selected[] = destList.getSelectedValues();
for (int i = selected.length - 1; i >= 0; --i) {
destListModel.removeElement(selected[i]);
}
destList.getSelectionModel().clearSelection();
}
private void initScreen() {
setBorder(BorderFactory.createEtchedBorder());
setLayout(new GridBagLayout());
sourceLabel = new JLabel(DEFAULT_SOURCE_CHOICE_LABEL);
sourceListModel = new SortedListModel();
sourceList = new JList(sourceListModel);
add(sourceLabel, new GridBagConstraints(0, 0, 1, 1, 0, 0,
GridBagConstraints.CENTER, GridBagConstraints.NONE,
EMPTY_INSETS, 0, 0));
add(new JScrollPane(sourceList), new GridBagConstraints(0, 1, 1, 5, .5,
1, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
EMPTY_INSETS, 0, 0));
addButton = new JButton(ADD_BUTTON_LABEL);
add(addButton, new GridBagConstraints(1, 2, 1, 2, 0, .25,
GridBagConstraints.CENTER, GridBagConstraints.NONE,
EMPTY_INSETS, 0, 0));
addButton.addActionListener(new AddListener());
removeButton = new JButton(REMOVE_BUTTON_LABEL);
add(removeButton, new GridBagConstraints(1, 4, 1, 2, 0, .25,
GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
0, 5, 0, 5), 0, 0));
removeButton.addActionListener(new RemoveListener());
destLabel = new JLabel(DEFAULT_DEST_CHOICE_LABEL);
destListModel = new SortedListModel();
destList = new JList(destListModel);
add(destLabel, new GridBagConstraints(2, 0, 1, 1, 0, 0,
GridBagConstraints.CENTER, GridBagConstraints.NONE,
EMPTY_INSETS, 0, 0));
add(new JScrollPane(destList), new GridBagConstraints(2, 1, 1, 5, .5,
1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
EMPTY_INSETS, 0, 0));
}
public static void main(String args[]) {
JFrame f = new JFrame("Dual List Box Tester");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DualListBox dual = new DualListBox();
dual.addSourceElements(new String[] { "One", "Two", "Three" });
dual.addSourceElements(new String[] { "Four", "Five", "Six" });
dual.addSourceElements(new String[] { "Seven", "Eight", "Nine" });
dual.addSourceElements(new String[] { "Ten", "Eleven", "Twelve" });
dual
.addSourceElements(new String[] { "Thirteen", "Fourteen",
"Fifteen" });
dual.addSourceElements(new String[] { "Sixteen", "Seventeen",
"Eighteen" });
dual.addSourceElements(new String[] { "Nineteen", "Twenty", "Thirty" });
f.getContentPane().add(dual, BorderLayout.CENTER);
f.setSize(400, 300);
f.setVisible(true);
}
private class AddListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object selected[] = sourceList.getSelectedValues();
addDestinationElements(selected);
clearSourceSelected();
}
}
private class RemoveListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object selected[] = destList.getSelectedValues();
addSourceElements(selected);
clearDestinationSelected();
}
}
}
class SortedListModel extends AbstractListModel {
SortedSet model;
public SortedListModel() {
model = new TreeSet();
}
public int getSize() {
return model.size();
}
public Object getElementAt(int index) {
return model.toArray()[index];
}
public void add(Object element) {
if (model.add(element)) {
fireContentsChanged(this, 0, getSize());
}
}
public void addAll(Object elements[]) {
Collection c = Arrays.asList(elements);
model.addAll(c);
fireContentsChanged(this, 0, getSize());
}
public void clear() {
model.clear();
fireContentsChanged(this, 0, getSize());
}
public boolean contains(Object element) {
return model.contains(element);
}
public Object firstElement() {
return model.first();
}
public Iterator iterator() {
return model.iterator();
}
public Object lastElement() {
return model.last();
}
public boolean removeElement(Object element) {
boolean removed = model.remove(element);
if (removed) {
fireContentsChanged(this, 0, getSize());
}
return removed;
}
}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.Insets;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.array;
导入java.util.Collection;
导入java.util.Iterator;
导入java.util.SortedSet;
导入java.util.TreeSet;
导入javax.swing.AbstractListModel;
导入javax.swing.BorderFactory;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JList;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.ListCellRenderer;
导入javax.swing.ListModel;
公共类DualListBox扩展了JPanel{
私有静态最终插入空\u插入=新插入(0,0,0,0);
私有静态最终字符串添加按钮标签=“添加>>”;
private static final String REMOVE_BUTTON_LABEL=“与您从第一个列表中获取它们的方式相同,并且有一个actionPerformed
方法可以在您的代码中执行该操作。与您从第一个列表中获取它们的方式相同,并且有一个actionPerformed
方法可以在您的代码中执行该操作
我不想得到select值;我想要它们全部
至少,您可以在SortedListModel
中添加合适的方法,并根据需要检查其结果
public Object[] toArray() {
return model.toArray(new String[0]);
}
…
System.out.println(Arrays.toString(destListModel.toArray()));
由于中介绍的泛型比泛型早,因此更好的解决方案是为SortedListModel
指定一个
private SortedListModel<String> destListModel = new SortedListModel<>();
…
class SortedListModel<E> extends AbstractListModel<E> {
private final SortedSet<E> model;
public SortedListModel() {
model = new TreeSet<>();
}
public List<E> toList() {
return new ArrayList<>(model);
}
@Override
public E getElementAt(int index) {
return (E) model.toArray()[index];
}
public void add(E element) {
if (model.add(element)) {
fireContentsChanged(this, 0, getSize());
}
}
…
}
…
System.out.println(destListModel.toList());
private SortedListModel destListModel=新的SortedListModel();
…
类SortedListModel扩展了AbstractListModel{
私有最终分类集模型;
公共分类列表模型(){
模型=新树集();
}
公众名单收费表(){
返回新的ArrayList(模型);
}
@凌驾
公共E getElementAt(int索引){
返回(E)model.toArray()[index];
}
公共无效添加(E元素){
if(模型添加(元素)){
fireContentsChanged(this,0,getSize());
}
}
…
}
…
System.out.println(destListModel.toList());
我不想得到select值;我想要它们全部
至少,您可以在SortedListModel
中添加合适的方法,并根据需要检查其结果
public Object[] toArray() {
return model.toArray(new String[0]);
}
…
System.out.println(Arrays.toString(destListModel.toArray()));
由于中介绍的泛型比泛型早,因此更好的解决方案是为SortedListModel
指定一个
private SortedListModel<String> destListModel = new SortedListModel<>();
…
class SortedListModel<E> extends AbstractListModel<E> {
private final SortedSet<E> model;
public SortedListModel() {
model = new TreeSet<>();
}
public List<E> toList() {
return new ArrayList<>(model);
}
@Override
public E getElementAt(int index) {
return (E) model.toArray()[index];
}
public void add(E element) {
if (model.add(element)) {
fireContentsChanged(this, 0, getSize());
}
}
…
}
…
System.out.println(destListModel.toList());
private SortedListModel destListModel=新的SortedListModel();
…
类SortedListModel扩展了AbstractListModel{
私有最终分类集模型;
公共分类列表模型(){
模型=新树集();
}
公众名单收费表(){
返回新的ArrayList(模型);
}
@凌驾
公共E getElementAt(int索引){
返回(E)model.toArray()[index];
}
公共无效添加(E元素){
if(模型添加(元素)){
fireContentsChanged(this,0,getSize());
}
}
…
}
…
System.out.println(destListModel.toList());
不要在此处转储所有代码。请说明使用最小代码解决问题所需的具体内容。不要在此处转储所有代码。请说明使用最小代码解决问题所需的具体内容。我看到示例使用了“Object selected[]=destList.getSelectedValues()方法;“/但是在第二个列表中,我不想得到选择值,我想要全部。我该怎么做呢?您可以使用getModel()
方法来实现这一点。请查看并搜索“getModel”一词的第一个匹配项。我看到示例使用的方法是“Object selected[]=destList.getSelectedValues();”/但是在第二个列表中,我不想得到选择的值,我想要所有的值。我该怎么做呢?您可以使用getModel()
方法来实现这一点。请查看并搜索单词“getModel”的第一个匹配项。