当前位置: 技术问答>java相关
为什么我这样写没有表头的工具提示?
来源: 互联网 发布时间:2015-03-01
本文导语: 总是报错误! F:Program FilesXinox SoftwareJCreator LEMyProjectsbiao1.java:50: 不能解 析符号 符号:方法 setToolTipText (java.lang.String) 位置:接口 in javax.swing.table.TableCellRenderer tcr.setToolTipText(bb.getColumnName(d)); 我认为tcr是JComp...
总是报错误!
F:Program FilesXinox SoftwareJCreator LEMyProjectsbiao1.java:50: 不能解
析符号
符号:方法 setToolTipText (java.lang.String)
位置:接口 in javax.swing.table.TableCellRenderer
tcr.setToolTipText(bb.getColumnName(d));
我认为tcr是JComponent的子类并且是JLabel的实例完成了TableCellRenderer接口.我
这样理解对吗?
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class biao1 extends JFrame {
public biao1 biaomb;
JTable biaosl;
public biao1(String text) {
super (text);
biaosl=new JTable(new biaomx());
biaosl.setAutoResizeMode(biaosl.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
biaosl.setCellSelectionEnabled(true);
biaosl.getTableHeader().addMouseListener(new zt());
getContentPane().add(new JScrollPane(biaosl));
}
public static void main(String args[]) {
biao1 biaomb=new biao1("测试程序") ;
biaomb.setLocation(200,200);
biaomb.setSize(750,450);
biaomb.setVisible(true);
}
class zt implements MouseListener {
public void mouseClicked(MouseEvent e) {
JTableHeader header= (JTableHeader)(e.getSource());
TableColumnModel tcm=header.getColumnModel();
TableColumn tc=tcm.getColumn(header.columnAtPoint(e.getPoint()));
TableCellRenderer tcr=tc.getHeaderRenderer();
JTable bb=header.getTable();
int d=header.columnAtPoint(e.getPoint());
tcr.setToolTipText(bb.getColumnName(d));
}
public void mousePressed(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
}
class biaomx extends AbstractTableModel {
public String[] mz={"ID号","名字","日期","婚否","学历"};
public Object[][] values={{new Integer(1),"zwh23", new
GregorianCalendar(1987, Calendar.JANUARY, 6).getTime(),new Boolean(true),"大
专"},{new Integer(2),"我会",new GregorianCalendar(2000,3,3),new
Boolean(true),"大专"},{new Integer(3),"该房",new
GregorianCalendar(2001,3,3),new Boolean(true),"大专"}};
public int getRowCount(){
return values.length;
}
public int getColumnCount() {
return mz.length;
}
public Object getValueAt(int row,int column) {
return values[row][column];
}
public String getColumnName(int column) {
return mz[column];
}
}
F:Program FilesXinox SoftwareJCreator LEMyProjectsbiao1.java:50: 不能解
析符号
符号:方法 setToolTipText (java.lang.String)
位置:接口 in javax.swing.table.TableCellRenderer
tcr.setToolTipText(bb.getColumnName(d));
我认为tcr是JComponent的子类并且是JLabel的实例完成了TableCellRenderer接口.我
这样理解对吗?
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class biao1 extends JFrame {
public biao1 biaomb;
JTable biaosl;
public biao1(String text) {
super (text);
biaosl=new JTable(new biaomx());
biaosl.setAutoResizeMode(biaosl.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
biaosl.setCellSelectionEnabled(true);
biaosl.getTableHeader().addMouseListener(new zt());
getContentPane().add(new JScrollPane(biaosl));
}
public static void main(String args[]) {
biao1 biaomb=new biao1("测试程序") ;
biaomb.setLocation(200,200);
biaomb.setSize(750,450);
biaomb.setVisible(true);
}
class zt implements MouseListener {
public void mouseClicked(MouseEvent e) {
JTableHeader header= (JTableHeader)(e.getSource());
TableColumnModel tcm=header.getColumnModel();
TableColumn tc=tcm.getColumn(header.columnAtPoint(e.getPoint()));
TableCellRenderer tcr=tc.getHeaderRenderer();
JTable bb=header.getTable();
int d=header.columnAtPoint(e.getPoint());
tcr.setToolTipText(bb.getColumnName(d));
}
public void mousePressed(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
}
class biaomx extends AbstractTableModel {
public String[] mz={"ID号","名字","日期","婚否","学历"};
public Object[][] values={{new Integer(1),"zwh23", new
GregorianCalendar(1987, Calendar.JANUARY, 6).getTime(),new Boolean(true),"大
专"},{new Integer(2),"我会",new GregorianCalendar(2000,3,3),new
Boolean(true),"大专"},{new Integer(3),"该房",new
GregorianCalendar(2001,3,3),new Boolean(true),"大专"}};
public int getRowCount(){
return values.length;
}
public int getColumnCount() {
return mz.length;
}
public Object getValueAt(int row,int column) {
return values[row][column];
}
public String getColumnName(int column) {
return mz[column];
}
}
|
getHeaderRenderer
public TableCellRenderer getHeaderRenderer()Returns the TableCellRenderer used to draw the header of the TableColumn. When the headerRenderer is null, the JTableHeader uses its defaultRenderer. The default value for a headerRenderer is null.
Returns:
the headerRenderer property
java.swing.table.TableColumn类的getHeaderRenderer()方法返回的是一个TableCellRenderer,而不是一个JLable的实例。
public TableCellRenderer getHeaderRenderer()Returns the TableCellRenderer used to draw the header of the TableColumn. When the headerRenderer is null, the JTableHeader uses its defaultRenderer. The default value for a headerRenderer is null.
Returns:
the headerRenderer property
java.swing.table.TableColumn类的getHeaderRenderer()方法返回的是一个TableCellRenderer,而不是一个JLable的实例。