Here you can find the source of makeIntoMoveRowUpButton(final JTable table, JButton button)
public static void makeIntoMoveRowUpButton(final JTable table, JButton button)
//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); } } } } }); } }