我正在尝试增加插入到 tableView 中的标签中的字体,但以下代码不起作用,就像我们只能影响标签的文本或背景颜色一样。
这就是我填充 tableView 的方式,我为每个标签使用了 4 种不同的方式,但没有一个可以工作!
private void createTableView() {
// Initialize the model if it has not yet been loaded
_tableModel = new SortedTableModel(StringComparator.getInstance(true),
0);
// Populate the list
for (int i = 0; i < 2; i++) {
_tableModel.addRow(new Object[] { "" });
}
// Create and apply style
RegionStyles style = new RegionStyles(BorderFactory.createSimpleBorder(
new XYEdges(1, 1, 1, 1), Border.STYLE_SOLID), null, null, null,
RegionStyles.ALIGN_LEFT, RegionStyles.ALIGN_TOP);
// Create the view and controller
TableView tableView = new TableView(_tableModel);
TableController tableController = new TableController(_tableModel,
tableView);
// Set the behaviour of the controller when a table item is clicked
tableView.setController(tableController);
tableController.setFocusPolicy(0);
// Create a DataTemplate that suppresses the third column
DataTemplate dataTemplate = new DataTemplate(tableView, 1, 2) {
/**
* @see DataTemplate#getDataFields(int)
*/
public Field[] getDataFields(int modelRowIndex) {
Field[] fields = new Field[2];
if (modelRowIndex == 0) {
fields[0] = new LabelField(" Company name") {
public void paint(Graphics graphics) {
Font font = Font.getDefault().derive(Font.PLAIN, 6,
Ui.UNITS_pt);
graphics.setFont(font);
graphics.setColor(Color.BLUE);
super.paint(graphics);
}
};
LabelField somethingLabel = new LabelField("some Name",
USE_ALL_HEIGHT | USE_ALL_WIDTH) {
public void paint(Graphics graphics) {
graphics.setColor(Color.BROWN);
super.paint(graphics);
}
};
Font font = Font.getDefault().derive(Font.PLAIN, 6,
Ui.UNITS_pt);
somethingLabel.setFont(font);
fields[1] = somethingLabel;
} else {
fields[0] = new LabelField(" Shipping Date") {
public void paint(Graphics graphics) {
graphics.setColor(Color.BLUE);
super.paint(graphics);
}
protected void layout(int width, int height) {
// TODO Auto-generated method stub
int fontHeight = getFont().getHeight();
setExtent(width, fontHeight + 30);
}
};
LabelField dateLabel = new LabelField("some Date",
USE_ALL_HEIGHT | USE_ALL_WIDTH) {
public void paint(Graphics graphics) {
graphics.setColor(Color.BROWN);
super.paint(graphics);
}
};
Font myFont = Font.getDefault().derive(Font.BOLD, 9, Ui.UNITS_pt);
dateLabel.setFont(myFont);
fields[1] = dateLabel;
}
return fields;
}
};
// Set up regions
// dataTemplate.createRegion(new XYRect(0, 0, 1, 2), style);
dataTemplate.createRegion(new XYRect(0, 0, 1, 1), style);
dataTemplate.createRegion(new XYRect(1, 0, 1, 1), style);
// Specify the size of each column by percentage, and the height of a
// row
dataTemplate.setColumnProperties(0, new TemplateColumnProperties(40,
TemplateColumnProperties.PERCENTAGE_WIDTH));
dataTemplate.setColumnProperties(1, new TemplateColumnProperties(60,
TemplateColumnProperties.PERCENTAGE_WIDTH));
dataTemplate.setRowProperties(0, new TemplateRowProperties(ROW_HEIGHT));
// Apply the template to the view
tableView.setDataTemplate(dataTemplate);
dataTemplate.useFixedHeight(true);
content1.add(tableView);
}