从数组中获取每个元素

从数组中获取每个元素

如何从数组中获取每个元素并将其放在不同的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*/});
}

09-25 21:43