0

How can I get every element from an array and put it in different JTable rows?

What I archive is a Table structured like this:

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

and I want something like this:

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

For now I have this (see comments):

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);

Thanks for your time, Let me know if you don't understand what I want to archive.

1 Answer 1

1

You can change code to

for(int i=0;i<intArray.length;i++)
{
    modelPeaks.addRow(new Object[]{intArray[i],/*Comments/Assignment value*/}); 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.