Go geek!

Our Victory Is Imminent

JTable – zmiana kolejności kolumn

Skomentuj »

Przydatna metoda pozwalająca zmienić kolejność kolumn w tabeli w podstawie ich identyfikatorów

import javax.swing.JTable;
import javax.swing.table.TableColumnModel;

public class TableUtils {
	/**
	 * Zmienia kolejność kolumn w tabeli
	 */
	public static void reorderColumns(JTable table, Object... columnIdentifiers) {
		TableColumnModel model = table.getColumnModel();
		for (int newIndex = 0; newIndex < columnIdentifiers.length; newIndex++) {
			try {
				Object columnIdentifier = columnIdentifiers[newIndex];
				int oldIndex = model.getColumnIndex(columnIdentifier);
				model.moveColumn(oldIndex, newIndex);
			} catch (IllegalArgumentException e) {
				// Identyfikator kolumny jest null lub nie istnieje kolumna o
				// podanym identyfikatorze. W obu przypadkach, po prostu
				// przechodzimy dalej
			}
		}
	}
}

Written by Jarek Przygódzki

Wrzesień 30, 2011 @ 6:04 pm

Dodaj komentarz

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Zmień )

Twitter picture

You are commenting using your Twitter account. Log Out / Zmień )

Facebook photo

You are commenting using your Facebook account. Log Out / Zmień )

Connecting to %s

Follow

Otrzymuj każdy nowy wpis na swoją skrzynkę e-mail.