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

JTable 單元格顯示Tip

2013-11-23 18:57:20  來源: Java核心技術 

  import javaawtPoint;

  import javaawteventMouseEvent;

  import javaawteventMouseMotionListener;

  import javaxswingJFrame;

  import javaxswingJScrollPane;

  import javaxswingJTable;

  import javaxswingtableAbstractTableModel;

  import javaxswingtableTableModel;

  public class TableTip {

  private JFrame jFrame;

  private JTable jTable;

  private JScrollPane jScrollPane;

  private int row;

  private int column;

  /**

  * @param args

  */

  public static void main(String[] args) {

  TableTip tableTip = new TableTip();

  tableTipshow();

  }

  private void show() {

  jFrame = new JFrame();

  jFramesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

  jFramesetSize( );

  final Object jtText[][] = { { } { }

  { } { } { } };

  final String jtHead[] = { Column Column Column Column };

  TableModel dataModel = new AbstractTableModel() {

  private static final long serialVersionUID = L;

  public int getColumnCount() {

  return jtHeadlength;

  }

  public int getRowCount() {

  return jtTextlength;

  }

  public Object getValueAt(int rowIndex int columnIndex) {

  return jtText[rowIndex][columnIndex];

  }

  public String getColumnName(int column) {

  return jtHead[column];

  }

  public boolean isCellEditable(int row int col) {

  return false;

  }

  public void setValueAt(Object aValue int row int column) {

  jtText[row][column] = aValue;

  }

  };

  jTable = new JTable(dataModel);

  jTablesetCellSelectionEnabled(false);

  // 設置不能重新調整列寬

  jTablegetTableHeader()setResizingAllowed(false);

  // 設置不能重新排序各列

  jTablegetTableHeader()setReorderingAllowed(false);

  // 監聽鼠標移動修改TIP

  jTableaddMouseMotionListener(new MouseMotionListener() {

  @Override

  public void mouseDragged(MouseEvent e) {

  }

  public void mouseMoved(MouseEvent e) {

  Point point = egetPoint();

  int x = jTablerowAtPoint(point);

  int y = lumnAtPoint(point);

  if (x != row || y != column) {

  row = x;

  column = y;

  }

  Object tip = jTablegetValueAt(row column);

  if (tip != null) {

  jTablesetToolTipText(Row:+(row+)+ Column:+(column+)+:+tiptoString());

  }

  }

  });

  jScrollPane = new JScrollPane(jTable);

  jScrollPanesetBounds( );

  jFrameadd(jScrollPane);

  jFramesetVisible(true);

  }

  }


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