可拖拽表格的swing实现(通过拖拽表格的行,实现交换行位置的交换)

swing里的table可以通过setUI(? extends  BasicTableUI )这个方法来实现对表的外观的一些控制,如下代码实现了简单的例子:

1.测试类:


import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
public class DragTableTest extends JFrame {
/**
 * table拖拽交换数据样例
 */
private static final long serialVersionUID = 1L;
private DragTable table= null;
String [] header = {"ID","name","score"};
Object [][] data  = {{1,"贾巴尔",38387},{2,"马龙",36928},{3,"kobe",32331},{4,"jordan",32292},{5,"张伯伦",31419}};
public DragTableTest () {
initView();
setLocalCenter();
}
public static void main(String[] args) {
DragTableTest tst = new DragTableTest();
tst.setVisible(true);
}
private void initView (){
this.setSize(new Dimension(400,400));
this.setTitle("可拖拽换行table:");
JScrollPane  pan = new JScrollPane(getTable());
this.getContentPane().add(pan);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private DragTable getTable(){
if(table == null){
table = new DragTable();
table.setModel(new DragTableModel(data,header));
table.setUI(new DragDropRowTableUI());
}
return table;
}

/**
 * 界面居中
 */
private void setLocalCenter(){
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
int screenHight = screenSize.height;
this.setLocation((screenWidth - this.getWidth()) / 2,
(screenHight - this.getHeight()) / 2);
}
}

2.在这里自定义的tableUI如下:
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import javax.swing.JComponent;
import javax.swing.event.MouseInputListener;
import javax.swing.plaf.basic.BasicTableUI;
import javax.swing.table.TableModel;


public class DragDropRowTableUI extends BasicTableUI {
 /**
  * @author zhen.p
  */
 private boolean draggingRow = false;
 private int startDragPoint;
 private int dyOffset;
 TableModel model=null;
    protected MouseInputListener createMouseInputListener() {
        return new DragDropRowMouseInputHandler();
    }

    public void paint(Graphics g, JComponent c) {
super.paint(g, c);


if (draggingRow) {
g.setColor(table.getParent().getBackground());
Rectangle cellRect = table.getCellRect(table.getSelectedRow(), 0,
false);
g.copyArea(cellRect.x, cellRect.y, table.getWidth(),
table.getRowHeight(), cellRect.x, dyOffset);


if (dyOffset < 0) {
g.fillRect(cellRect.x, cellRect.y
+ (table.getRowHeight() + dyOffset), table.getWidth(),
(dyOffset * -1));
} else {
g.fillRect(cellRect.x, cellRect.y, table.getWidth(), dyOffset);
}
}
    }


    class DragDropRowMouseInputHandler extends MouseInputHandler {


public void mousePressed(MouseEvent e) {
super.mousePressed(e);
startDragPoint = (int) e.getPoint().getY();
}


public void mouseDragged(MouseEvent e) {
int fromRow = table.getSelectedRow();
if (fromRow >= 0) {
draggingRow = true;
int rowHeight = table.getRowHeight();
int middleOfSelectedRow = (rowHeight * fromRow)
+ (rowHeight / 2);
int toRow = -1;
int yMousePoint = (int) e.getPoint().getY();


if (yMousePoint < (middleOfSelectedRow - rowHeight)) {
// Move row up
toRow = fromRow - 1;
} else if (yMousePoint > (middleOfSelectedRow + rowHeight)) {
// Move row down
toRow = fromRow + 1;
}


if (toRow >= 0 && toRow < table.getRowCount()) {
model = table.getModel();
if(model instanceof DragTableModel) {
//用于交换tableModel中数据的顺序,以供拖拽至后数据交换位置显示
Object[][] data = ((DragTableModel) model).getData();
Object[][] listresult = swapObjectList(data, fromRow, toRow);
((DragTableModel) model).setData(listresult);
}
table.setRowSelectionInterval(toRow, toRow);
startDragPoint = yMousePoint;
}
dyOffset = (startDragPoint - yMousePoint) * -1;
table.repaint();
}
}


        public void mouseReleased(MouseEvent e){
         super.mouseReleased(e);
         draggingRow = false;
         table.repaint();
        }
    }
    private Object[][] swapObjectList(  Object[][] origin,int fromRow,int toRow ){
    Object [] mid = new Object [origin[0].length];
    mid = origin[fromRow];
    origin[fromRow] = origin[toRow];
    origin[toRow] = mid ;
return origin;
    }
}

3.自定义的table:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JTable;




public class DragTable extends JTable  implements  ActionListener{


/**
* table
*/
private static final long serialVersionUID = 1L;
public DragTable () {

}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}


}
4.自定义的tablemodel:
import javax.swing.table.AbstractTableModel;




public class  DragTableModel extends AbstractTableModel{


/**
* table model
*/
private static final long serialVersionUID = 1L;
private Object[][] data = null;
public Object[][] getData() {
return data;
}
public void setData(Object[][] data) {
this.data = data;
}
private String [] header = null;
public DragTableModel (Object[][] data ,String [] header){
this.data = data;
this.header = header;
}
public int getRowCount() {
return data.length;

}


@Override
public int getColumnCount() {
return header.length;
}



@Override
public String getColumnName(int column) {
return header[column];
}
@Override
public Object getValueAt(int row, int column) {
if(row >= 0){
switch(column){
  case 0:
  return data[row][0];
  case 1:
  return data[row][1];
case 2:
  return data[row][2];
  
}
}
return null;
}
}


你可能感兴趣的:(swing,jtable)