0
如何獲取數組中的每個元素並將其放入不同的JTable行中?獲取數組中的每個元素並將其放入不同的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);
感謝您的時間, 如果您不明白我想存檔的內容,請告訴我。