package com
import java
import java
import java
import java
import java
import java
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
/**
* 自定義jtable
* @author dl
*/
public class JMyTable extends JTable {
public int m_nRow =
public int[] m_nRows = null; /**當前選中的多個序號*/
public JMyTableModel model;
public RowSorter sorter;
public JMyTable(JMyTableModel model) {
super(model);
setUI(new MyTableUI());
this
sorter = new TableRowSorter(model);
setRowSorter(sorter);
setColumnSelectionAllowed(false);
setRowSelectionAllowed(true);
setSelectionMode(ListSelectionModel
setSelectionMode(ListSelectionModel
setDefaultRenderer(Object
}
class MyRender extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(JTable table
boolean isSelected
super
isSelected
//斑馬線
if ((row %
setBackground(new java
} else {
setBackground(java
}
//選中反藍
if (isSelected) {
setBackground(java
}
//字體設置
Font font = new Font(
setFont(font);
return this;
}
}
/**實現windows式多選習慣*/
class MyTableUI extends BasicTableUI {
protected MouseInputListener createMouseInputListener() {
return new MyMouseInputHandler();
}
class MyMouseInputHandler extends MouseInputHandler {
private boolean ignoreDrag = false;
public void mousePressed(MouseEvent e) {
if (e
table
}
Point origin = e
int row = table
int column = lumnAtPoint(origin);
if (row !=
if (table
ignoreDrag = true;
} else {
super
}
}
}
public void mouseClicked(MouseEvent e) {
super
}
public void mouseDragged(MouseEvent e) {
if (!ignoreDrag) {
super
} else {
table
DnDConstants
}
}
public void mouseReleased(MouseEvent e) {
if (ignoreDrag) {
super
ignoreDrag = false;
}
super
}
}
}
/**
* <br>
* 方法說明
* 輸入參數
* 返回類型
*/
public void valueChanged(ListSelectionEvent e) {
// System
super
m_nRows = getSelectedRows();
if(m_nRows
// 未選中
m_nRows = null;
m_nRow =
return;
}
if (m_nRows
// 選中一個
m_nRows = null;
m_nRow = getSelectedRow();
if (m_nRow !=
m_nRow = nvertRowIndexToModel(m_nRow);
} else {
//選中多個
m_nRow =
for (int i =
if (m_nRows[i] !=
m_nRows[i] = nvertRowIndexToModel(m_nRows[i]);
}
}
repaint();
}
/**
*<br>方法說明
*<br>輸入參數
*<br>返回類型
*/
public String getToolTipText() {
Point pt = getMousePosition();
if (pt == null)
return null;
int row = rowAtPoint(pt);
if (row ==
return null;
int index = nvertRowIndexToModel(row);
if (index ==
return null;
Dimension tableSize = getSize();
int width = tableSize
if (width >
width =
if (width <
width =
String strText =
+ new Integer(width
+
+
+ model
return strText;
}
}
package com
import java
import javax
/**
* 自定義jtablemodel
* @author dl
*/
public class JMyTableModel extends AbstractTableModel {
protected Vector all = new Vector(); /**所有數據的列表*/
/**定義表頭*/
protected String[] columnNames;
/**字段類型數組*/
protected Class[] types;
/**字段可否編輯標記數組*/
protected boolean[] canEdit;
/**
*<br>方法說明
*<br>輸入參數
*<br>返回類型
*/
public int getColumnCount() {
return columnNames
}
/**
*<br>方法說明
*<br>輸入參數
*<br>返回類型
*/
public String getColumnName(int col) {
return columnNames[col];
}
/**
*<br>方法說明
*<br>輸入參數
*<br>返回類型
*/
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
/**
*<br>方法說明
*<br>輸入參數
*<br>返回類型
*/
public boolean isCellEditable(int rowIndex
return canEdit [columnIndex];
}
/**
*<br>方法說明
*<br>輸入參數
*<br>返回類型
*/
public Object getValueAt(int rowIndex
// TODO Auto
return ((Vector)all
}
/**
*<br>方法說明
*<br>輸入參數
*<br>返回類型
*/
public Object getValueAt(int rowIndex) {
// TODO Auto
return all
}
/**
*<br>方法說明
*<br>輸入參數
*<br>返回類型
*/
public void empty() {
all
fireTableDataChanged();
}
/**
*<br>方法說明
*<br>輸入參數
*<br>返回類型
*/
public void setRs(Vector vct) {
all
fireTableDataChanged();
}
/**
*<br>方法說明
*<br>輸入參數
*<br>返回類型
*/
public int getRowCount() {
// TODO Auto
return all
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26509.html