熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

自定義jtable 實現排序 windows式多選習慣

2022-06-13   來源: Java核心技術 

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

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26509.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.