Here you can find the source of setColumnOrder(final JTable table, final String positions)
Parameter | Description |
---|---|
table | the table to set the column positions |
positions | A string in the format "0 1 2 3" |
public static void setColumnOrder(final JTable table, final String positions)
//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); } } } }