0

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

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

4

1 回答 1

1

您可以将代码更改为

for(int i=0;i<intArray.length;i++)
{
    modelPeaks.addRow(new Object[]{intArray[i],/*Comments/Assignment value*/}); 
}
于 2013-03-11T11:55:01.320 回答