Java JTable Row makeIntoMoveRowUpButton(final JTable table, JButton button)

Here you can find the source of makeIntoMoveRowUpButton(final JTable table, JButton button)

Description

make Into Move Row Up Button

License

BSD License

Declaration

public static void makeIntoMoveRowUpButton(final JTable table, JButton button) 

Method Source Code

//package com.java2s;
// LICENSE:      This file is distributed under the BSD license.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

public class Main {
    public static void makeIntoMoveRowUpButton(final JTable table, JButton button) {
        button.addActionListener(new ActionListener() {
            @Override//from   w w  w .  ja  v  a  2  s  .  c  o  m
            public void actionPerformed(ActionEvent e) {
                int rowIndex = table.getSelectedRow();
                if (rowIndex > 0) {
                    ((DefaultTableModel) table.getModel()).moveRow(rowIndex, rowIndex, rowIndex - 1);
                    table.setRowSelectionInterval(rowIndex - 1, rowIndex - 1);
                }
            }
        });
        enableOnTableEvent(table, button);
    }

    private static void enableOnTableEvent(final JTable table, final JComponent component) {
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    if (component.getParent().isEnabled()) {
                        if (table.getSelectedRowCount() > 0) {
                            component.setEnabled(true);
                        } else {
                            component.setEnabled(false);
                        }
                    }
                }
            }
        });
    }
}

Related

  1. hasRows(JTable table)
  2. insertRow(final JTable table, final int index, Object... data)
  3. isFirstToLastRow(TableModelEvent e)
  4. isRowInsert(TableModelEvent e)
  5. jtable$convertRowIndexToModel(JTable table, int index)
  6. packRows(JTable table, int start, int end, int margin)
  7. removeTableRows(DefaultTableModel model)
  8. rowToModelIndex(JTable table, int row)
  9. setRowLines(JTable table, int row, int lines)