Java Utililty Methods JTable Select

List of utility methods to do JTable Select

Description

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

Method

voidcopyTableSelectionsToJList(JList list, JTable listTbl)
Synchronisiert eine JList mit einem JTable.
list.setSelectedIndices(new int[0]);
int rows = listTbl.getRowCount();
Vector sel = new Vector();
for (int i = 0; i < rows; i++) {
    if (listTbl.getSelectionModel().isSelectedIndex(i)) {
        sel.add(new Integer(listTbl.convertRowIndexToModel(i)));
int tmp[] = new int[sel.size()];
for (int i = 0; i < sel.size(); i++)
    tmp[i] = ((Integer) sel.get(i)).intValue();
list.setSelectedIndices(tmp);
JCheckBoxcreateJCheckBoxForTable(boolean selected)
It creates a new check box for inclusion in a table and sets its "is selected" attribute to the given value.
JCheckBox checkBox = new JCheckBox();
checkBox.setOpaque(true);
checkBox.setHorizontalAlignment(SwingConstants.CENTER);
checkBox.setForeground(TABLE_COMPONENT_FG_COLOR);
checkBox.setBackground(TABLE_COMPONENT_BG_COLOR);
checkBox.setSelected(selected);
return checkBox;
voiddisableSelection(JTable table)
Disable any selections on a table.
table.setRowSelectionAllowed(false);
table.setColumnSelectionAllowed(false);
table.setCellSelectionEnabled(false);
table.setFocusable(false);
IntegergetSelectedCase(JTable table)
get Selected Case
int selectedRow = table.getSelectedRow();
if (selectedRow >= 0) {
    return (int) table.getModel().getValueAt(selectedRow, 0);
} else {
    return null;
Integer[]getSelectedObject(JTable table)
get Selected Object
Integer[] selected = new Integer[2];
int selectedRow = table.getSelectedRow();
if (selectedRow >= 0) {
    selected[0] = (int) table.getModel().getValueAt(selectedRow, 0);
    selected[1] = (int) table.getModel().getValueAt(selectedRow, 1);
    return selected;
} else {
    return null;
...
ColorgetSelectionBackground(JTable t)
get Selection Background
return t.getSelectionBackground();
voidinvertSelect(JTable table)
invert Select
int[] temp = table.getSelectedRows();
ArrayList<Integer> selectedRows = new ArrayList<Integer>();
for (int index = 0; index < temp.length; index++) {
    selectedRows.add(temp[index]);
ListSelectionModel selectionModel = table.getSelectionModel();
selectionModel.clearSelection();
for (int index = 0; index < table.getRowCount(); index++) {
...
voidinvertSelection(JTable table)
invert Selection
for (int rowCount = table.getRowCount() - 1; rowCount >= 0; rowCount--) {
    if (table.isRowSelected(rowCount)) {
        table.removeRowSelectionInterval(rowCount, rowCount);
    } else {
        table.addRowSelectionInterval(rowCount, rowCount);
voidmoveSelectionDown(JTable table)
move Selection Down
int selectedRow = table.getSelectedRow();
int selectedRowCount = table.getSelectedRowCount();
int rowCount = table.getRowCount();
int targetRow;
if (rowCount == 0) {
    return;
} else if (rowCount == 1) {
    targetRow = 0;
...
voidselectPositions(final JTable table, final int index0, final int index1)
select Positions
invokeLater(new Runnable() {
    public void run() {
        table.getSelectionModel().setSelectionInterval(index0, index1);
});