set Value to JTable - Java Swing

Java examples for Swing:JTable

Description

set Value to JTable

Demo Code


//package com.java2s;

import javax.swing.JTable;

import javax.swing.table.TableColumnModel;

public class Main {
    public static void setValue(JTable table, Object header, Object value) {
        int row = table.getSelectedRow();

        setValue(table, header, row, value);
    }/*www .jav a2s . co m*/

    public static void setValue(JTable table, Object header, int row,
            Object value) {
        TableColumnModel model = table.getTableHeader().getColumnModel();
        int count = model.getColumnCount();

        for (int i = 0; i < count; i++) {
            if (model.getColumn(i).getHeaderValue().equals(header)) {
                table.setValueAt(value, row, i);
                break;
            }
        }
    }

    public static void setValue(JTable table, Object headerValue,
            Object headerCell, Object cell, Object value) {
        TableColumnModel model = table.getTableHeader().getColumnModel();
        int count = model.getColumnCount();
        int row = table.getRowCount();

        for (int i = 0; i < count; i++) {
            if (model.getColumn(i).getHeaderValue().equals(headerCell)) {
                for (int j = 0; j < row; j++) {
                    if (cell.equals(table.getValueAt(j, i))) {
                        setValue(table, headerValue, j, value);
                        break;
                    }
                }

                break;
            }
        }
    }
}

Related Tutorials