package tableTest;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class TabelTest {
JFrame jFrame = new JFrame();
private JScrollPane jScrollPane;
public void show() {
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setSize(300, 128);
final Object jtText[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 },
{ 9, 10, 11, 12 }, { 13, 14, 15, 16 }, { 17, 18, 19, 20 } };
final String jtHead[] = { “Column1”, “Column2”, “Column3”, “Column4” };
TableModel dataModel = new AbstractTableModel() {
private static final long serialVersionUID = 0L;
public int getColumnCount() {
return jtHead.length;
}
public int getRowCount() {
return jtText.length;
}
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;
}
};
final JTable jTable = new JTable(dataModel);
jScrollPane = new JScrollPane(jTable);
jScrollPane.setBounds(10, 40, 280, 90);
jFrame.add(jScrollPane);
jFrame.setVisible(true);
jTable.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
Point point = e.getPoint();
int x = jTable.rowAtPoint(point);
int y = jTable.columnAtPoint(point);
System.out.println(“x:”+x);
System.out.println(“y:”+y);
//if (x != row || y != column) {
//row = x;
//column = y;
//}
Object tip = jTable.getValueAt(x, y);
if (tip != null) {
jTable.setToolTipText(tip.toString());
}
}
});
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TabelTest tabelTest = new TabelTest();
tabelTest.show();
}
}
今天的文章java中tablemodel_java中给table中的cell加tips 以及用AbstractTableModel创建表格分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:http://bianchenghao.cn/31315.html