Java ArrayLists 转换成 JList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3269516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java ArrayLists into JList
提问by test
OK so I'm doing a small part of my inventory. I got MOST of it down. I'm trying to add string items to an ArrayList then add that to a JList. However, I'm getting this error when I compile:
好的,所以我正在做我的库存的一小部分。我记下了大部分。我正在尝试将字符串项添加到 ArrayList,然后将其添加到 JList。但是,我在编译时收到此错误:
C:\Users\Dan\Documents\DanJavaGen\inventory.java:30: cannot find symbol
symbol : constructor JList(java.util.ArrayList<java.lang.String>)
location: class javax.swing.JList
list = new JList(arr);
It's probably some rookie mistake I am making ... :/
这可能是我犯的一些菜鸟错误......:/
Code:
代码:
import java.applet.Applet;
import java.awt.*;
import javax.swing.*;
import javax.swing.JList;
import java.awt.event.*;
import java.util.ArrayList;
import java.io.*;
import java.util.*;
public class inventory extends JApplet implements MouseListener {
public static String newline;
public static JList list;
int gold = 123;
public void init() {
ArrayList<String> arr = new ArrayList<String>();
arr.add("Hatchet");
arr.add("Sword");
arr.add("Shield");
arr.add(gold + " Gold");
System.out.println("You have " + arr.size() + " items in your inventory.");
showInventory(arr);
list = new JList(arr);
add(list);
list.addMouseListener(this);
list.setVisible(true);
}
public static void showInventory (ArrayList<String> theList) {
for (int i = 0; i < theList.size(); i++) {
System.out.println(theList.get(i));
}
}
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) {
Object index = list.getSelectedValue();
System.out.println("You have selected: " + index);
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void paint(Graphics g) {
}
}
采纳答案by Alain O'Dea
JListprovides a constructor JList(Object[])which you can call after unpacking your ArrayList<String>using toArray():
JList提供了一个构造函数JList(Object[]),您可以在使用toArray()解压ArrayList<String>后调用它:
list = new JList(arr.toArray())
回答by Romain Hippeau
Constructor SummaryJList()
Constructs a JList
with an empty, read-only, model.JList(ListModel dataModel)
Constructs a JList
that displays elements from the specified, non-null, model.JList(Object[] listData)
Constructs a JList
that displays the elements in the specified array.JList(Vector<?> listData)
Constructs a JList
that displays the elements in the specified Vector.
构造函数摘要使用空的只读模型JList()
构造 a JList
。JList(ListModel dataModel)
构造一个JList
显示来自指定的非空模型的元素。JList(Object[] listData)
构造一个JList
显示指定数组中的元素的 。JList(Vector<?> listData)
构造一个JList
显示指定 Vector 中的元素的 。
回答by Jon Sansoucie
I had trouble with the toArray()
method causing exceptions, so I built a quick generic method to convert. Maybe someone will find it useful as well. I know this is an old post but I'll bet it's still viewed from time to time. Here's the method:
我在使用toArray()
导致异常的方法时遇到了麻烦,所以我构建了一个快速的通用方法来进行转换。也许有人会发现它也很有用。我知道这是一个旧帖子,但我敢打赌它仍然时不时被查看。这是方法:
private <T> void populateArrayFromList(T[] arr, ArrayList<T> arrayList) {
System.out.println("Array size " + arr.length);
System.out.println("ArrayList size " + arrayList.size());
for (int i = 0; i < arrayList.size(); i++) {
arr[i] = arrayList.get(i);
}
}
Just create the array before you pass it in, like
只需在传入数组之前创建数组,例如
String[] arr = new String[arrayList.size()];
Then just call it from your code
然后只需从您的代码中调用它
populateArrayFromList(arr, arrayList);
回答by user4910021
I had trouble with toArray() and JList for my project as well. Did some research and tried a few things and found something that works. Hope it helps everyone:
我的项目也遇到了 toArray() 和 JList 的问题。做了一些研究并尝试了一些东西,发现了一些有用的东西。希望对大家有帮助:
ArrayList<String> i = new ArrayList<>();
String[] str = new String[i.size()];
//Assuming there is data in your list
JList<String> list = new JList<>(i.toArray(str));
回答by Lakshan
Try this:
尝试这个:
DefaultListModel listModel = new DefaultListModel();
for (int i = 0; i < arrayList().size(); i++)
{
listModel.addElement(arrayList().get(i));
}
jList1.setModel(listModel);
回答by Marat
I simply implemented the ListModel interface:
我只是简单地实现了 ListModel 接口:
package proba;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListDataListener;
public class Proba extends JFrame {
ArrayList<String> al = new ArrayList<>();
public Proba() {
al.add("1111111");
al.add("2222222");
al.add("3333333");
setLayout(new BorderLayout());
MyListModel lm = new MyListModel();
JList l = new JList(lm);
l.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane sp = new JScrollPane(l);
add(sp, BorderLayout.CENTER);
}
class MyListModel implements ListModel {
@Override
public int getSize() {
return al.size();
}
@Override
public Object getElementAt(int index) {
return al.get(index);
}
@Override
public void addListDataListener(ListDataListener l) {
}
@Override
public void removeListDataListener(ListDataListener l) {
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
Proba app = new Proba();
app.setSize(600, 480);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
});
}
}
回答by Mukesh Chauhan
if you are using drag and drop components then, name your list as "itemList" and I suppose that arr is the name of your arraylist which contains some string data then :
如果您使用拖放组件,那么将您的列表命名为“itemList”,我想 arr 是包含一些字符串数据的数组列表的名称:
`DefaultComboBoxModel model=
new DefaultComboBoxModel(arr.toArray(new String[arr.size()]));
itemList.setModel(model);`
I hope this will work for you.
我希望这对你有用。
回答by bEn
You can pass Object
superclass as type of the JList.
您可以将超Object
类作为 JList 的类型传递。
ArrayList<String> arr = new ArrayList<String>();
arr.add("Hatchet");
arr.add("Sword");
arr.add("Shield");
JList<Object> list = JList<Object>(arr.toArray());
回答by oboamg lolZZ
if you have a loop in for gaining data in mysql you can put like this
如果你有一个循环来获取 mysql 中的数据,你可以这样写
while(result.next()){
itemid = result.getInt("id");
itempice = result.getInt("price");
itemname = result.getString("itemname");
arraylist.add(itemname+" Price: "+itempice);
}
jList.setListData(arraylist.toArray());