Hello perhaps you could help me with a problem please.

I wish to display a table of results using a JTable. Customarily I use a 2D Object array which is initialized with the data in a manner as follows:

public Object[][] data={
        {"Biology",25},{"Additional Math", 54},{"Geography",43},{"History",21},{"Spanish", 47},
        {"Information Tech.",110},{"Technical Drawing",54},{"French",45},{"Physics",76},{"Chemistry",76},
        {"Principles of Accounts",34},{"Principles of Business",30}

    };

However this time, initialising won't work because the data that I am receiving will be subject to change, thus the 2D array needs to be created at a later point.

The added data that I will be receiving, will come from two(2) arrays, one containing Strings and the Other integers.

How would I go about filling my 2d Object array to get this done please?

My weak attempt

public Object[][] dataCreate(String[] subs, int[] tot, int max)
    {
        int i=0;
        Object[][] data = null;

        for(i=0;i< max; i++)
        {
            Object[] temp={subs[i],tot[i]};
            data[i][0]=temp;
        }

        return data;
    }

Why not use a loop to copy an element from each array into the target array?
Define the target 2D array and copy to elements 0 and 1

Hmmm. Thanks for that suggestion. I got it to work now. What I did was, tp implement a DefaultTableModel.

public String[] columns={"Subject","Total"};

public Object[][]data_2={{"",0}};


public DefaultTableModel tableModel = new DefaultTableModel(data_2,columns);

JTable totalDisplay= new JTable(tableModel);

And all that I did thereafter was add and remove rows using the TableModel variable as follows.

    public void dataCreate(String[] subs, int[] tot, int max, DefaultTableModel tableModel)
    {
        int i=0;

        tableModel.removeRow(0);
        for(i=0;i< subs.length; i++)
        {
            tableModel.insertRow(i, new Object[]{subs[i],tot[i]});

        }


    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.