4
我有3個表格在相同的形式,但他們有不同的號碼。的列,我想爲每個表設置不同的列寬。我可以在下面的代碼中獲得,或者我必須創建3個不同的類擴展表,然後設置不同的約束寬度?通過這樣做,我正在寫相同的代碼三次,我不想這樣做。設置3個表格的不同單元格寬度
protected void beforeReport(Form f){
Table table=new MyTable(new DefaultTableModel(columnNamesInOut,dataInOut));
Table tablePunishment=new MyTable(new DefaultTableModel(columnNamesPunishment,dataPunishment));
Table tableAccident=new MyTable(new DefaultTableModel(columnNamesAccident,dataAccident));
}
public class MyTable extends Table {
public MyTable(TableModel tm) {
super(tm, true);
}
public Component createCell(Object value, int row, int col, boolean editable) {
Component component = null;
if (value instanceof Vector) {
Container c = new Container(new BoxLayout(BoxLayout.Y_AXIS));
Iterator i = ((Vector) value).iterator();
while (i.hasNext()) {
c.addComponent(new Label(i.next().toString()));
}
component = c;
} else {
component = super.createCell(value, row, col, editable);
}
return component;
}
@Override
protected TableLayout.Constraint createCellConstraint(Object value, int row, int column) {
TableLayout.Constraint constraint = ((TableLayout) this.getLayout()).createConstraint();
constraint.setVerticalAlign(CENTER);
addComponent(constraint);
switch (column) {
case 0: {
constraint.setWidthPercentage(25);
break;
}
case 1: {
constraint.setWidthPercentage(25);
break;
}
case 2: {
constraint.setWidthPercentage(25);
break;
}
case 3: {
constraint.setWidthPercentage(25);
break;
}
}
return constraint;
}
}
不應該使用column.setPreferredWidth()來指定表格列寬度嗎? – SomeDude
不,因爲您必須精確地使用setPreferredWidth,但setWidthPercentage會在所有不同的屏幕中調整coln大小。 – beck
而最重要的是,我認爲TableLayout.Constraint不採用setPreferredWidth方法。 – beck