电脑故障

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

JTable 单元格显示Tip


发布日期:2018/1/28
 

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

}

}

上一篇:OO设计原则总结

下一篇:jvm性能调优/垃圾回收器