如何在JTable中添加JLabel? [英] How do i add a JLabel in a JTable?
问题描述
所以我想在我的JTable中添加一个带有背景颜色的空JLabel. 这是一个饼图,我想添加它,以便图例匹配.
So i want to add a empty JLabel with a Background color to my JTable. It's for a piechart and i want to add this so the legend matches.
代码:
for (String result : queryResult) {
JLabel label = new JLabel("Hallo: "+rowCount);
label.setBackground(colors[rowCount]);
label.setOpaque(true);
String queryResultString = "";
queryResultString = result.toString();
String[] lineArray = queryResultString.split("////");
String[] pieData = new String[3];
pieData[0] = lineArray[0];
pieData[1] = lineArray[rangId - 1];
int value = Integer.parseInt(pieData[1]);
double percentage = value / total * 100;
pieData[2] = "" + percentage + "%";
pieModel.addRow(new Object[] {label, pieData[0], pieData[1], pieData[2]});
rowCount++;
}
pieTable.setDefaultRenderer(String.class, new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
if(value instanceof JLabel){
//This time return only the JLabel without icon
return (JLabel)value;
}else{
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
}
});
我在做什么错? :o
What am i doing wrong? :o
当我查看表格第一栏中的结果时,它会说:
When i look at the results in the first column of the table it says:
javax.swing.JLabel [,0,0,0 ...... 看不到其余部分,但它确实无法正常工作! :(
javax.swing.JLabel[,0,0,0...... can't see the rest but it definetly is not working as it should! :(
推荐答案
所以我想在我的JTable中添加一个带有背景颜色的空JLabel.这是一个饼图,我想添加它,以便图例匹配.
So i want to add a empty JLabel with a Background color to my JTable. It's for a piechart and i want to add this so the legend matches.
您不能通过渲染器来做到这一点默认值Component
,JComponent
或JLabel
you can't do that Renderer by default returs Component
, JComponent
or JLabel
这篇关于如何在JTable中添加JLabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!