在删除边框和标题后,我的JTable看起来像这样:
正如您所看到的,在表格的底部可以看到白色边框,我认为没有理由在我的代码中发生这种情况。我该如何删除?
JTable的代码是:
Object[][] datosMeasurements = {};
DefaultTableModel model = new DefaultTableModel(datosMeasurements, nombresColumnasTablas);
tablaMeasurements = new JTable(model){
public boolean isCellEditable(int rowIndex, int colIndex) {
if(colIndex==1){
return true;
}
return false; //Disallow the editing of any cell
}
};
model.addTableModelListener(new CambioTablaMeasurementListener());
TableCellRenderer rendererMeasurements = new paramRenderer();
tablaMeasurements.setDefaultRenderer(Object.class, rendererMeasurements);
((DefaultCellEditor) tablaMeasurements.getDefaultEditor(Object.class)).setClickCountToStart(1);
JScrollPane scrollMeasurements = new JScrollPane(tablaMeasurements);
tablaMeasurements.setTableHeader(null);
scrollMeasurements.setColumnHeaderView(null);
tablaMeasurements.setPreferredScrollableViewportSize(new Dimension(100,100));
对于它的渲染器:
class paramRenderer extends DefaultTableCellRenderer
{
private final DecimalFormat formatter = new DecimalFormat( "#.000" );
public Component getTableCellRendererComponent(
JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column)
{
//value = formatter.format((Number)value);
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setEnabled(table == null || table.isEnabled());
if (column == 0)
setHorizontalAlignment( RIGHT );
else if (column == 1)
setHorizontalAlignment( CENTER );
else
setHorizontalAlignment( LEFT );
for (int i=0; i<3; i++) {
TableColumn columna = table.getColumnModel().getColumn(i);
if (i==0) columna.setPreferredWidth(65);
if (i==1) columna.setPreferredWidth(75);
if (i==2) columna.setPreferredWidth(30);
}
if(isSelected)
{
setBackground(table.getBackground());
setForeground(table.getForeground());
if (row==0 || row==2) setBackground(new java.awt.Color(211, 217, 255));
else setBackground(java.awt.Color.white);
}
else
{
setBackground(table.getBackground());
setForeground(table.getForeground());
if (row==0 || row==2) setBackground(new java.awt.Color(211, 217, 255));
else setBackground(java.awt.Color.white);
}
return this;
}
public void setValue() {
formatter.setMinimumFractionDigits(1);
formatter.setMaximumFractionDigits(3);
}
}
}
答案 0 :(得分:2)
根据rowHeight计算prefScrollableViewportSize,类似于
int visibleRowCount = 10;
table.setPreferredScrollableViewportSize(
new Dimension(myWidth, visibleRowCount * table.getRowHeight()));
答案 1 :(得分:1)
这可能是表“viewPort”。您可以尝试:
yourTable.setFillsViewportHeight(true);