我在这里遇到了一件奇怪的事。我正在尝试在JTable单元格中添加JLabel。标签图标可能会根据某些条件发生变化。我根据此处的规范创建了一个虚拟项目:http://javanepal.wordpress.com/2010/06/30/adding-jlabel-in-jtable/
它工作正常。我将TableModel更改为从AbstractTableModel而不是DefaultTableModel扩展,现在添加一行时,如果我检查Object []:
[javax.swing.JLabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,
flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,
disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,
iconTextGap=4,labelFor=,text=Row 1,Col1,verticalAlignment=CENTER,
verticalTextPosition=CENTER], whatever....]
代码就是这样,对于TableModel:
public class MyModel extends javax.swing.table.AbstractTableModel {
Object[][] row = {{new JLabel("Row 1 Col 1"), "Row 1 Col 2", "Row 1 Col3"},
{new JLabel("Row 2 Col 1"), "Row 2 Col 2", "Row 2 Col3"},
{new JLabel("Row 3 Col 1"), "Row 3 Col 2", "Row 3 Col3"},
{new JLabel("Row 4 Col 1"), "Row 4 Col 2", "Row 4 Col3"}};
Object[] col = {"Column 1", "Column 2", "Column 3"};
protected Vector<Object> data;
public void addRow(Object o[]) {
for (int i = 0; i < o.length; i++) {
if (o[i] == null) {
o[i] = new String();
}
}
data.addElement(o);
fireTableRowsInserted(data.size() - 1, data.size() - 1);
}
public MyModel() {
super();
data = new Vector<Object>();
//Adding rows
for (Object[] r : row) {
addRow(r);
}
}
@Override
public Class getColumnClass(int columnIndex) {
if (columnIndex == 0) {
return getValueAt(0, columnIndex).getClass();
} else {
return super.getColumnClass(columnIndex);
}
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getValueAt(int arg0, int arg1) {
// TODO Auto-generated method stub
return null;
}
}
以下是测试类的代码:
public class MyTableTest extends JFrame {
public MyTableTest(String title) {
super(title);
showGUI();
}
public void showGUI() {
JTable table = new JTable();
table.setModel(new MyModel());//invoking our custom model
//loadMessages(table);
table.setDefaultRenderer(JLabel.class, new Renderer());// for the rendering of cell
JScrollPane jp = new JScrollPane(table);
getContentPane().add(jp);
setVisible(true);
setSize(500, 300);
}
public static void main(String[] args) {
MyTableTest t = new MyTableTest("Table Custom");
}
}
我真的很困惑,为什么会发生这种情况,关于这个问题的消息并不是很清楚。
谢谢!
答案 0 :(得分:4)
您没有发布消息,也没有发布问题。但只是看看你的代码,我已经可以说出很多问题了
JLabel
中存储TableModel
个实例。如果您想使用TableModel
个实例呈现JLabel
的内容,可以在渲染器中执行此操作TableModel
实现针对模型和行数返回0(请参阅getColumnCount()
和getRowCount()
方法)getValueAt
始终返回null
,因为您使用NullPointerException
getColumnClass
方法中的getValueAt(0, columnIndex).getClass()
先解决这些问题,看看会发生什么
答案 1 :(得分:4)
1)您无法将JLabel
放入Cell,Cell默认返回JComponent/JLabel
2)您无法将Icon
添加到JLabel
,直接将Icon
添加到JTable
3)您的AbstractTableModel
错误地声明了Object[][]/Object[]
而Model
基于Vector
,将Object[][]
更改为Vector<Vector<Object>>
/ Object[]
至Vector<String>
您可以删除此问题
4)如果您不能限制或覆盖AbstractTableModel
DefaultTableModel
,没有理由
答案 2 :(得分:0)
如果使用JXTreeTable,我将设置第0列是标签类型
ArrayNode dataRow = new ArrayNode(new Object[]{new JLabel("text",icon), ...});
// add Root --> Set TreeTableModel
.......
jxTreeTable.setTreeCellRenderer(new TreeCellRenderer() {
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
AbstractMutableTreeTableNode data = (AbstractMutableTreeTableNode) value;
if (data.isLeaf()) {// Is Leaf as Label
return (JLabel) data.getValueAt(0); // Value 0 is a label
} else if(data.getValueAt(0)!=null){ // Is Root
return new JLabel(data.getValueAt(0).toString());
}else{ // IsRoot, but is Cell Null Value
return new JLabel("");
}
}
});