电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

自定义jtable 实现排序 windows式多选习惯


发布日期:2019/3/31
 

package combrowser;

import javaawtComponent;

import javaawtDimension;

import javaawtFont;

import javaawtPoint;

import javaawtdndDnDConstants;

import javaawteventMouseEvent;

import javaxswingJTable;

import javaxswingListSelectionModel;

import javaxswingRowSorter;

import javaxswingeventListSelectionEvent;

import javaxswingeventMouseInputListener;

import javaxswingplafbasicBasicTableUI;

import javaxswingtableDefaultTableCellRenderer;

import javaxswingtableTableRowSorter;

/**

* 自定义jtable实现排序windows式多选习惯斑马线ToolTip

* @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());

thismodel = model;

sorter = new TableRowSorter(model);

setRowSorter(sorter);

setColumnSelectionAllowed(false);

setRowSelectionAllowed(true);

setSelectionMode(ListSelectionModelSINGLE_INTERVAL_SELECTION);

setSelectionMode(ListSelectionModelMULTIPLE_INTERVAL_SELECTION);

setDefaultRenderer(Objectclass new MyRender());

}

class MyRender extends DefaultTableCellRenderer

{

public Component getTableCellRendererComponent(JTable table Object value

boolean isSelected boolean hasFocus int row int column) {

supergetTableCellRendererComponent( table value

isSelected hasFocus row column);

//斑马线

if ((row % ) != ) {

setBackground(new javaawtColor());

} else {

setBackground(javaawtColorwhite);

}

//选中反蓝

if (isSelected) {

setBackground(javaawtColorBLUE);

}

//字体设置

Font font = new Font(宋体 FontBOLD );

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 (egetClickCount() == ) {

tableclearSelection();

}

Point origin = egetPoint();

int row = tablerowAtPoint(origin);

int column = lumnAtPoint(origin);

if (row != && column != ) {

if (tableisCellSelected(row column)) {

ignoreDrag = true;

} else {

supermousePressed(e);

}

}

}

public void mouseClicked(MouseEvent e) {

supermouseClicked(e);

}

public void mouseDragged(MouseEvent e) {

if (!ignoreDrag) {

supermouseDragged(e);

} else {

tablegetTransferHandler()exportAsDrag(table e

DnDConstantsACTION_COPY);

}

}

public void mouseReleased(MouseEvent e) {

if (ignoreDrag) {

supermousePressed(e);

ignoreDrag = false;

}

supermouseReleased(e);

}

}

}

/**

* <br>

* 方法说明得到选中的行 <br>

* 输入参数 <br>

* 返回类型

*/

public void valueChanged(ListSelectionEvent e) {

// Systemoutprintln(valueChanged);

supervalueChanged(e);

m_nRows = getSelectedRows();

if(m_nRowslength == ) {

// 未选中

m_nRows = null;

m_nRow = ;

return;

}

if (m_nRowslength == ) {

// 选中一个

m_nRows = null;

m_nRow = getSelectedRow();

if (m_nRow != )

m_nRow = nvertRowIndexToModel(m_nRow);

} else {

//选中多个

m_nRow = ;

for (int i = ; i < m_nRowslength; ++i) {

if (m_nRows[i] != )

m_nRows[i] = nvertRowIndexToModel(m_nRows[i]);

}

}

repaint();

}

/**

*<br>方法说明得到ToolTipText

*<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 = tableSizewidth;

if (width > )

width = ;

if (width < )

width = ;

String strText = <html><table width=

+ new Integer(width )

+ pt border=\\ cellpadding=\\ cellspacing=\\ style=\TABLELAYOUT: fixed\ >

+ <font face=\dialog\ size= color=black>

+ modelgetValueAt(index)toString() + </font></table></html>;

return strText;

}

}

package combrowser;

import javautilVector;

import javaxswingtableAbstractTableModel;

/**

* 自定义jtablemodel配合自定义jtable使用

* @author dl

*/

public class JMyTableModel extends AbstractTableModel {

protected Vector all = new Vector(); /**所有数据的列表*/

/**定义表头*/

protected String[] columnNames;

/**字段类型数组*/

protected Class[] types;

/**字段可否编辑标记数组*/

protected boolean[] canEdit;

/**

*<br>方法说明得到表格的列数 继承AbstractTableModel必须实现的方法

*<br>输入参数

*<br>返回类型int 列数

*/

public int getColumnCount() {

return columnNameslength;

}

/**

*<br>方法说明得到某一列的列名 继承AbstractTableModel必须实现的方法

*<br>输入参数col 列索引

*<br>返回类型String 列名

*/

public String getColumnName(int col) {

return columnNames[col];

}

/**

*<br>方法说明返回字段类型

*<br>输入参数columnIndex 列索引

*<br>返回类型Class

*/

public Class getColumnClass(int columnIndex) {

return types [columnIndex];

}

/**

*<br>方法说明设置字段是否可以编辑

*<br>输入参数rowIndex 行索引; columnIndex 列索引

*<br>返回类型boolean

*/

public boolean isCellEditable(int rowIndex int columnIndex) {

return canEdit [columnIndex];

}

/**

*<br>方法说明得到单元格的内容

*<br>输入参数rowIndex 行索引; columnIndex 列索引

*<br>返回类型boolean

*/

public Object getValueAt(int rowIndex int columnIndex) {

// TODO Autogenerated method stub

return ((Vector)allelementAt(rowIndex))elementAt(columnIndex);

}

/**

*<br>方法说明得到某一行的内容

*<br>输入参数rowIndex 行索引

*<br>返回类型boolean

*/

public Object getValueAt(int rowIndex) {

// TODO Autogenerated method stub

return allelementAt(rowIndex);

}

/**

*<br>方法说明清空表格

*<br>输入参数

*<br>返回类型void

*/

public void empty() {

allremoveAllElements();

fireTableDataChanged();

}

/**

*<br>方法说明设置结果集

*<br>输入参数temp 结果集

*<br>返回类型void

*/

public void setRs(Vector vct) {

alladdAll(vct);

fireTableDataChanged();

}

/**

*<br>方法说明得到表格的行数

*<br>输入参数

*<br>返回类型int 列数

*/

public int getRowCount() {

// TODO Autogenerated method stub

return allsize();

}

}

上一篇:输入密码后自动登录功能的实现

下一篇:类访问