[英]Get JTable default TableCellRenderer and Manipulate it?
I am going to be using a lot of custom TableCellRenderers in a JTable, but I don't want to have to recreate the default properties for every single one, because it seems I have to start with a plain JLabel. 我将在JTable中使用大量自定义TableCellRenderers,但我不想为每一个都重新创建默认属性,因为我似乎必须从一个普通的JLabel开始。 This creates a ton of annoying overhead, because I even have to populate the value myself as well as the matching background, the foreground, the font, etc... to match the rest of the JTable.
这会产生大量烦人的开销,因为我甚至必须自己填充值以及匹配的背景,前景,字体等......以匹配JTable的其余部分。
Is there a way I can retrieve the parent TableCellRenderer or the it's resulting JLabel and manipulate that? 有没有办法可以检索父TableCellRenderer或它的结果JLabel并操纵它? That way I have all the defaults set already, and I can just manipulate the properties I am actually changing?
那样我已经设置了所有默认值,我可以操纵我实际更改的属性? I have tried everything with super.getCellRenderer and it is not giving me anything to accomplish that.
我已经尝试了super.getCellRenderer的所有内容,并没有给我任何东西来完成它。
Also, my TableCellRenderer is not column-specific. 另外,我的TableCellRenderer不是特定于列的。 Each cell can vary.
每个细胞都可以变化。
public class ActionTable extends JTable {
...
public TableCellRenderer getCellRenderer(int row, int column) {
String colName = ((ActionTableModel)this.getModel()).getColumnName(column);
if ( colName.equals("CUSTOM COL") ) {
return new ActionCellRenderer(this);
}
return super.getCellRenderer(this.convertRowIndexToModel(row), this.convertColumnIndexToModel(column));
}
public class ActionCellRenderer extends JLabel implements TableCellRenderer {
private ActionTable actionTable;
public ActionCellRenderer(ActionTable actionTable) {
this.actionTable = actionTable;
setOpaque(true); //MUST do this for background to show up.
}
@Override
public Component getTableCellRendererComponent(JTable table, Object color,boolean isSelected, boolean hasFocus,int row, int column) {
int modelRow = actionTable.convertRowIndexToModel(row);
int modelCol = actionTable.convertColumnIndexToModel(column);
String colName = table.getModel().getColumnName(modelCol);
/*
annoying overhead to retrieve default cell renderer properties
to match the rest of the JTable
*/
if (isSelected)
{
setBackground(table.getSelectionBackground());
setForeground(table.getSelectionForeground());
}
else
{
setBackground(table.getBackground());
setForeground(table.getForeground());
}
//AND I HAVE TO RETRIEVE THE VALUE MYSELF TOO
String textVal = ((ActionTableModel)table.getModel()).getValueAt(modelRow, modelCol).toString();
this.setHorizontalAlignment(SwingConstants.CENTER);
this.setFont(new Font("Serif", Font.PLAIN, 15));
this.setText(textVal);
//NOW I CAN DO CUSTOMIZATIONS
//PUT CUSTOMIZATIONS HERE
return this;
} }
Just have your ActionCellRenderer extend DefaultTableCellRenderer and call the getTableCellRendererComponent() method to get the JLabel: 只需让你的ActionCellRenderer扩展DefaultTableCellRenderer并调用getTableCellRendererComponent()方法来获取JLabel:
public class ActionCellRenderer extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus,int row, int column){
//get the label
JLabel label = (JLabel)super.getTableCellRendererComponent(table, value,isSelected, hasFocus,row, column);
//do whatever you want with the label
return label;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.