Java Utililty Methods JTable Data

List of utility methods to do JTable Data

Description

The list of methods to do JTable Data are organized into topic(s).

Method

Object[]getValueBySelectedRow(JTable table, int rows[], int col)
get Value By Selected Row
DefaultTableModel model = (DefaultTableModel) table.getModel();
Object[] ret = new Object[rows.length];
for (int i = 0; i < rows.length; i++)
    ret[i] = model.getValueAt(rows[i], col);
return ret;
String[]retrieveSelectedValuesFromTable(JTable table, int column)
This method retrieves the selected values of a table in the tabbed pane and returns them in a string-array
int[] rows = table.getSelectedRows();
if (rows.length < 1) {
    return null;
String[] value = new String[rows.length];
for (int cnt = 0; cnt < rows.length; cnt++) {
    value[cnt] = table.getValueAt(rows[cnt], column).toString();
return value;
voidselectRows(JTable table, String[] values, int column)
Selects rows which have one of the values in the input array in the specified column.
if ((values != null) && (table != null)) {
    int columnCount = table.getColumnCount();
    if ((column >= 0) && (column < columnCount)) {
        Map valueRowMapping = new HashMap();
        int rowCount = table.getRowCount();
        for (int row = 0; row < rowCount; row++) {
            valueRowMapping.put(table.getValueAt(row, column), new Integer(row));
        table.clearSelection();
        for (int i = 0; i < values.length; i++) {
            if ((values[i] != null) && (valueRowMapping.containsKey(values[i]))) {
                int row = ((Integer) valueRowMapping.get(values[i])).intValue();
                table.addRowSelectionInterval(row, row);
voidselectValueInTable(JTable table, String value, int column)
This method selects a certain row that contains the value value in the column column in the jTable table .
for (int cnt = 0; cnt < table.getRowCount(); cnt++) {
    String val = table.getValueAt(cnt, column).toString();
    if (val.equals(value)) {
        table.getSelectionModel().setSelectionInterval(cnt, cnt);
        table.scrollRectToVisible(table.getCellRect(cnt, column, false));
        return;
voidsetModelValueAt(JTable table, Object value, int row, String columnTitle)
set Model Value At
int column = getColumnIndex(table, columnTitle);
if (column != COLUMN_NOT_FOUND) {
    table.getModel().setValueAt(value, row, column);
voidsetValueAt(JTable table, Object value, int row, String columnTitle)
set Value At
int column = getColumnIndex(table, columnTitle);
if (column != COLUMN_NOT_FOUND) {
    table.setValueAt(value, row, column);
voidtableColumnValueAlign(JTable table, int column, int align)
table Column Value Align
DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
rightRenderer.setHorizontalAlignment(
        align == 0 ? SwingConstants.RIGHT : align == 1 ? SwingConstants.CENTER : SwingConstants.LEFT);
table.getColumnModel().getColumn(column).setCellRenderer(rightRenderer);
voidupdateTable(Object value, int rowId, int columnId, JTable table)
update Table
if (rowId > -1 && rowId < table.getRowCount() && columnId > -1 && columnId < table.getColumnCount()) {
    table.setValueAt(value, rowId, columnId);