Java JTable Row writeRow(DefaultTableModel table, List data, int row)

Here you can find the source of writeRow(DefaultTableModel table, List data, int row)

Description

Writes data to the table starting from row.

License

LGPL

Parameter

Parameter Description
table a parameter
data a list of data containing the train id, train locations and their timing.
row starting row.

Return

true if we can schedule the train from the starting row, false otherwise.

Declaration

private static boolean writeRow(DefaultTableModel table, List<String[]> data, int row) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.List;

import javax.swing.table.DefaultTableModel;

public class Main {
    /**/*w w w.j  a v  a 2  s . c o  m*/
     * Writes data to the table starting from <code>row</code>.
     * 
     * @param table
     * @param data
     *            a list of data containing the train id, train locations and
     *            their timing.
     * @param row
     *            starting row.
     * @return <code>true</code> if we can schedule the train from the starting
     *         row, <code>false</code> otherwise.
     */
    private static boolean writeRow(DefaultTableModel table, List<String[]> data, int row) {
        String trainId = data.get(0)[0].trim();
        String[] locations = data.get(1);
        String[] time = data.get(2);

        int cols = table.getColumnCount();
        int counter = 0;
        for (int i = 0; i < locations.length; i++) {
            if (i - 1 >= 0) {
                counter += Integer.parseInt(time[i - 1].trim());
            }
            for (int j = 0; j < Integer.valueOf(time[i].trim()); j++) {
                int rows = table.getRowCount();
                int column = table.findColumn(locations[i].trim());
                if (counter + row + j >= rows) {
                    String[] tmpData = new String[cols];
                    table.insertRow(rows, tmpData);
                }
                table.setValueAt(new String(trainId), counter + row + j, column);
            }
        }
        return true;
    }
}

Related

  1. rowToModelIndex(JTable table, int row)
  2. setRowLines(JTable table, int row, int lines)
  3. setTableSize(JTable table, float percentage, int rows)
  4. setToRowBackground(Component c, JTable table, int row)
  5. updateTableSizes(JTable table, int rows)