自定义 JTableHeader 导致 NullPointerException
Custom JTableHeader causes NullPointerException
我一直在尝试使用 Oracle's How To Use Tables to create a JTable with tool tips for each column header. The demo 中的代码似乎可以工作,但是无论我是直接粘贴代码还是抽象我自己的代码 class,我都会收到 NullPointerException 在 SynthTableHeaderUI.java 行 233 中调用 getTableCellRendererComponent()。这是由于调用 header.getTable() which returns null on any table 我尝试 setTableHeader() on,即使我setTableHeader(new JTableHeader(tblWhatever.getColumnModel()));
我从演示中粘贴的函数位于自定义 TableModel 中,否则效果很好,如下所示:
public class TestTableModel extends AbstractTableModel {
private final String[] columnNames = {"Name", "Height", "Weight", "Age"};
private final String[] columnToolTips = {"Person's Name",
"Height in centimetres.",
"Weight in kilograms.",
"Age in years as of 2015-Jan-01."};
private ToolTipTableHeader ClientTableHeader; // = new ToolTipTableHeader((new JTable()).getColumnModel(), columnToolTips);
private final Client[] List = {
new Client("Abigale", 150, 108, 22),
new Client("Bob", 180, 175, 36),
new Client("Charles", 150, 210, 52)
};
/*
* Constructors
*/
public TestTableModel() {
super();
}
public void setTableHeader(JTable tblClients) {
tblClients.setTableHeader(createDefaultTableHeader(tblClients.getColumnModel()));
}
/*
* AbstractCellEditor Implementations
*/
@Override
public Class getColumnClass(int col) throws java.lang.IndexOutOfBoundsException {
switch(col) {
case 0: return String.class; //.ClientName;
case 1: return Integer.class; //.Height;
case 2: return Integer.class; //.Weight;
case 3: return Integer.class; //.Age;
default: throw new IndexOutOfBoundsException("Column " + col + ": class not accounted for in " + this.getClass().getName() + ".getColumnClass");
}
}
@Override
public int getColumnCount() { return columnNames.length; }
@Override
public String getColumnName(int col) { return columnNames[col]; }
@Override
public int getRowCount() { return List.length; }
@Override
public Object getValueAt(int row, int col) throws java.lang.IndexOutOfBoundsException {
switch(col) {
case 0: return List[row].ClientName;
case 1: return List[row].Height;
case 2: return List[row].Weight;
case 3: return List[row].Age;
default: throw new IndexOutOfBoundsException("Column " + col + ": value not accounted for in " + this.getClass().getName() + ".getValueAt");
}
}
@Override
public boolean isCellEditable(int row, int col) { return true; }
@Override
public void setValueAt(Object value, int row, int col) {
switch(col) {
case 0: List[row].ClientName = (String) value; break;
case 1: List[row].Height = (Integer) value; break;
case 2: List[row].Weight = (Integer) value; break;
case 3: List[row].Age = (Integer) value; break;
default: throw new IndexOutOfBoundsException("Column " + col + ": value not accounted for in " + this.getClass().getName() + ".setValueAt");
}
fireTableCellUpdated(row, col);
}
/*
* Extensions
*/
//Implement table header tool tips.
protected JTableHeader createDefaultTableHeader(TableColumnModel tcmThis) {
return new JTableHeader(tcmThis) {
@Override
public String getToolTipText(MouseEvent e) {
String tip = null;
java.awt.Point p = e.getPoint();
int index = columnModel.getColumnIndexAtX(p.x);
int realIndex =
columnModel.getColumn(index).getModelIndex();
return columnToolTips[realIndex];
}
};
}
}
自定义 class 看起来像这样:
public class ToolTipTableHeader extends JTableHeader {
private final String ColumnToolTips[];
ToolTipTableHeader(TableColumnModel cm, String iniToolTips[]) {
super(cm);
if(iniToolTips.length != cm.getColumnCount())
throw new InvalidParameterException("The size of iniToolTips must be precisely equal to the columnModel column count.");
ColumnToolTips = iniToolTips;
}
@Override
public String getToolTipText(MouseEvent meToolTipEvent) {
String tip = null;
if(columnModel == null) return "columnModel == null";
if(meToolTipEvent == null) return "meMouseEvent == null";
Point p = meToolTipEvent.getPoint();
int index = columnModel.getColumnIndexAtX(p.x);
int realIndex = columnModel.getColumn(index).getModelIndex();
return ColumnToolTips[realIndex];
}
}
初始化在 JDialog 构造函数中完成(JTable tblTest 在设计器中创建):
public TestForm(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
TestTableModel htmTest = new TestTableModel();
tblTest.setModel(new TestTableModel());
htmTest.setTableHeader(tblTest);
}
我注意到默认 JTableHeader 的构造函数不需要将 JTable 传递给它,我已经实现了构造函数和覆盖的 getTable() t 似乎被称为。当我写这篇文章时,我意识到有效的演示将函数放在自定义 JTable 中,我不想这样做,因为我使用的是 NetBeans IDE 并且我不知道向设计器添加自定义 table 的简单方法。
我错过了什么?如何在不创建自定义 JTable 的情况下实现它?感谢您的指点。
JTableHeader 扩展 必须 添加为 JTable 构造的一部分。 table header 的任何后续设置都会导致上述错误。如果使用 NetBeans,请转到 Design Mode 以获取 dialog/form/panel、select 和 table,然后单击 Code 选项卡 属性 window。添加(例如)
new javax.swing.JTable() {
//Implement table header tool tips.
protected JTableHeader createDefaultTableHeader() {
return new JTableHeader(columnModel) {
public String getToolTipText(MouseEvent e) {
String tip = null;
java.awt.Point p = e.getPoint();
int index = columnModel.getColumnIndexAtX(p.x);
int realIndex = columnModel.getColumn(index).getModelIndex();
return myToolTips[realIndex];
}
};
}
};
到自定义创建代码字段并确保定义
private final String[] myToolTips = {
"Column 0 tool tip",
"Column 1 tool tip",
// ...
"Final column tool tip"
};
在 dialog/form/panel 的 class 的某处。这可行,但仍然有点不雅,因为它需要为使用该 table 模型的每种形式完成,而不是能够将其融合并拥有一个 class 来设置 table.
我一直在尝试使用 Oracle's How To Use Tables to create a JTable with tool tips for each column header. The demo 中的代码似乎可以工作,但是无论我是直接粘贴代码还是抽象我自己的代码 class,我都会收到 NullPointerException 在 SynthTableHeaderUI.java 行 233 中调用 getTableCellRendererComponent()。这是由于调用 header.getTable() which returns null on any table 我尝试 setTableHeader() on,即使我setTableHeader(new JTableHeader(tblWhatever.getColumnModel()));
我从演示中粘贴的函数位于自定义 TableModel 中,否则效果很好,如下所示:
public class TestTableModel extends AbstractTableModel {
private final String[] columnNames = {"Name", "Height", "Weight", "Age"};
private final String[] columnToolTips = {"Person's Name",
"Height in centimetres.",
"Weight in kilograms.",
"Age in years as of 2015-Jan-01."};
private ToolTipTableHeader ClientTableHeader; // = new ToolTipTableHeader((new JTable()).getColumnModel(), columnToolTips);
private final Client[] List = {
new Client("Abigale", 150, 108, 22),
new Client("Bob", 180, 175, 36),
new Client("Charles", 150, 210, 52)
};
/*
* Constructors
*/
public TestTableModel() {
super();
}
public void setTableHeader(JTable tblClients) {
tblClients.setTableHeader(createDefaultTableHeader(tblClients.getColumnModel()));
}
/*
* AbstractCellEditor Implementations
*/
@Override
public Class getColumnClass(int col) throws java.lang.IndexOutOfBoundsException {
switch(col) {
case 0: return String.class; //.ClientName;
case 1: return Integer.class; //.Height;
case 2: return Integer.class; //.Weight;
case 3: return Integer.class; //.Age;
default: throw new IndexOutOfBoundsException("Column " + col + ": class not accounted for in " + this.getClass().getName() + ".getColumnClass");
}
}
@Override
public int getColumnCount() { return columnNames.length; }
@Override
public String getColumnName(int col) { return columnNames[col]; }
@Override
public int getRowCount() { return List.length; }
@Override
public Object getValueAt(int row, int col) throws java.lang.IndexOutOfBoundsException {
switch(col) {
case 0: return List[row].ClientName;
case 1: return List[row].Height;
case 2: return List[row].Weight;
case 3: return List[row].Age;
default: throw new IndexOutOfBoundsException("Column " + col + ": value not accounted for in " + this.getClass().getName() + ".getValueAt");
}
}
@Override
public boolean isCellEditable(int row, int col) { return true; }
@Override
public void setValueAt(Object value, int row, int col) {
switch(col) {
case 0: List[row].ClientName = (String) value; break;
case 1: List[row].Height = (Integer) value; break;
case 2: List[row].Weight = (Integer) value; break;
case 3: List[row].Age = (Integer) value; break;
default: throw new IndexOutOfBoundsException("Column " + col + ": value not accounted for in " + this.getClass().getName() + ".setValueAt");
}
fireTableCellUpdated(row, col);
}
/*
* Extensions
*/
//Implement table header tool tips.
protected JTableHeader createDefaultTableHeader(TableColumnModel tcmThis) {
return new JTableHeader(tcmThis) {
@Override
public String getToolTipText(MouseEvent e) {
String tip = null;
java.awt.Point p = e.getPoint();
int index = columnModel.getColumnIndexAtX(p.x);
int realIndex =
columnModel.getColumn(index).getModelIndex();
return columnToolTips[realIndex];
}
};
}
}
自定义 class 看起来像这样:
public class ToolTipTableHeader extends JTableHeader {
private final String ColumnToolTips[];
ToolTipTableHeader(TableColumnModel cm, String iniToolTips[]) {
super(cm);
if(iniToolTips.length != cm.getColumnCount())
throw new InvalidParameterException("The size of iniToolTips must be precisely equal to the columnModel column count.");
ColumnToolTips = iniToolTips;
}
@Override
public String getToolTipText(MouseEvent meToolTipEvent) {
String tip = null;
if(columnModel == null) return "columnModel == null";
if(meToolTipEvent == null) return "meMouseEvent == null";
Point p = meToolTipEvent.getPoint();
int index = columnModel.getColumnIndexAtX(p.x);
int realIndex = columnModel.getColumn(index).getModelIndex();
return ColumnToolTips[realIndex];
}
}
初始化在 JDialog 构造函数中完成(JTable tblTest 在设计器中创建):
public TestForm(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
TestTableModel htmTest = new TestTableModel();
tblTest.setModel(new TestTableModel());
htmTest.setTableHeader(tblTest);
}
我注意到默认 JTableHeader 的构造函数不需要将 JTable 传递给它,我已经实现了构造函数和覆盖的 getTable() t 似乎被称为。当我写这篇文章时,我意识到有效的演示将函数放在自定义 JTable 中,我不想这样做,因为我使用的是 NetBeans IDE 并且我不知道向设计器添加自定义 table 的简单方法。
我错过了什么?如何在不创建自定义 JTable 的情况下实现它?感谢您的指点。
JTableHeader 扩展 必须 添加为 JTable 构造的一部分。 table header 的任何后续设置都会导致上述错误。如果使用 NetBeans,请转到 Design Mode 以获取 dialog/form/panel、select 和 table,然后单击 Code 选项卡 属性 window。添加(例如)
new javax.swing.JTable() {
//Implement table header tool tips.
protected JTableHeader createDefaultTableHeader() {
return new JTableHeader(columnModel) {
public String getToolTipText(MouseEvent e) {
String tip = null;
java.awt.Point p = e.getPoint();
int index = columnModel.getColumnIndexAtX(p.x);
int realIndex = columnModel.getColumn(index).getModelIndex();
return myToolTips[realIndex];
}
};
}
};
到自定义创建代码字段并确保定义
private final String[] myToolTips = {
"Column 0 tool tip",
"Column 1 tool tip",
// ...
"Final column tool tip"
};
在 dialog/form/panel 的 class 的某处。这可行,但仍然有点不雅,因为它需要为使用该 table 模型的每种形式完成,而不是能够将其融合并拥有一个 class 来设置 table.