gpt4 book ai didi

java - 从数组中获取每个元素并将其放入不同的 JTable 行中

转载 作者:行者123 更新时间:2023-12-01 14:46:56 25 4
gpt4 key购买 nike

如何从数组中获取每个元素并将其放入不同的 JTable 行中?

我归档的是一个如下结构的表:

             +--------------------------+------------------------+
+ Peak + Comments/Assignment +
+--------------------------+------------------------+
+ element1, element 2, ... + +
+--------------------------+------------------------+

我想要这样的东西:

             +----------+------------------------+
+ Peak + Comments/Assignment +
+----------+------------------------+
+ element1 + +
+ element2 + +
+ element3 + +
+ ... + +
+----------+------------------------+

现在我有这个(见评论):

Object[] objVal = (myJList).getSelectedValues(); //get selected values from a JList
String[] stringArray = Arrays.copyOf(objVal, objVal.length, String[].class); //put in a String Array

int[] intArray = new int[stringArray.length];
for (int i = 0; i < stringArray.length; i++) {
intArray[i] = Integer.parseInt(stringArray[i]); //parse Int
}

Arrays.sort(intArray); //sort it ascending

JFrame f = new JFrame(); //my JFrame
JPanel p = new JPanel(); //add a JPanel

DefaultTableModel modelPeaks = new DefaultTableModel(); //JTable model
JTable table = new JTable(modelPeaks); //assign the model

modelPeaks.addColumn("Peak");
modelPeaks.addColumn("Comments / Assignment");

modelPeaks.addRow(new Object[]{Arrays.toString(intArray).replace("[", "").replace("]", "")}); //add a raw with the values and remove the squere brackets

//but what I want is to add each element from the array to a separate row and to fill up just the first column not to add all elements to a row

/* example of what I want to archive
+----------+------------------------+
+ Peak + Comments/Assignment +
+----------+------------------------+
+ element1 + +
+ element2 + +
+ element3 + +
+ ... + +
+----------+------------------------+
*/


JScrollPane scrollPane = new JScrollPane(table);

p.setLayout(new FlowLayout());
p.add(scrollPane);
f.add(p);
f.setVisible(true);
f.setSize(500, 500);

感谢您的宝贵时间,如果您不明白我要存档的内容,请告诉我。

最佳答案

您可以将代码更改为

for(int i=0;i<intArray.length;i++)
{
modelPeaks.addRow(new Object[]{intArray[i],/*Comments/Assignment value*/});
}

关于java - 从数组中获取每个元素并将其放入不同的 JTable 行中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15336980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com