Java JTable Column setColumnOrder(final JTable table, final String positions)

Here you can find the source of setColumnOrder(final JTable table, final String positions)

Description

Sets the column order of a table given a correctly formatted String .

License

Open Source License

Parameter

Parameter Description
table the table to set the column positions
positions A string in the format "0 1 2 3"

Declaration

public static void setColumnOrder(final JTable table,
        final String positions) 

Method Source Code

//package com.java2s;
/*/* ww  w .  ja v  a 2 s.c om*/
 * jGnash, a personal finance application
 * Copyright (C) 2001-2017 Craig Cavanaugh
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.regex.Pattern;
import javax.swing.JTable;

public class Main {
    /**
     * Pattern for splitting test using a space delimiter
     */
    private static final Pattern SPACE_DELIMITER_PATTERN = Pattern
            .compile(" ");

    /**
     * Sets the column order of a table given a correctly formatted
     * {@code String}.
     *
     * @param table the table to set the column positions
     * @param positions A string in the format "0 1 2 3"
     */
    public static void setColumnOrder(final JTable table,
            final String positions) {
        if (positions == null) {
            return;
        }
        String[] array = SPACE_DELIMITER_PATTERN.split(positions);
        int count = table.getColumnCount();
        if (array.length == count) {
            for (int i = 0; i < count; i++) {
                int index = table.convertColumnIndexToView(i);
                int position = Integer.parseInt(array[i]);
                table.getColumnModel().moveColumn(index, position);
            }
        }
    }
}

Related

  1. newTable(String name, Object[][] rowData, Object[] columnNames)
  2. pack(JTable table, boolean packColumns, boolean packRows, int padding)
  3. ponerAncho(TableColumn loColumn, int plAncho)
  4. removeColumn(int index, JTable table)
  5. selectByTyping(JTable table, javax.swing.JTextField textfield, int column)
  6. setearPrimerRegistro(JTable pTabla, JTextField pTextoDeBusqueda, int pColumna)
  7. setearTextoDeBusqueda(JTable pTabla, JTextField pTextoDeBusqueda, int pColumna)
  8. setSelectedRow(JTable table, int rowIndex, int columnIndex)
  9. setupColumns(JTable table)