下面列出了javax.swing.table.DefaultTableModel#setColumnCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testTableModelResize() {
DefaultTableModel model = new DefaultTableModel(6, 6);
HidableTableColumnModel tcm = new HidableTableColumnModel(model);
// At start numAllColumns == numVisibleColumns.
Assert.assertEquals(tcm.getColumnCount(false), tcm.getColumnCount(true));
tcm.setColumnVisible(tcm.getColumn(1, false), false); // Remove column at modelIndex 1.
tcm.setColumnVisible(tcm.getColumn(4, false), false); // Remove column at modelIndex 4.
// We've removed 2 columns.
Assert.assertEquals(tcm.getColumnCount(false) - 2, tcm.getColumnCount(true));
model.setColumnCount(10);
Assert.assertEquals(10, tcm.getColumnCount(true));
/*
* This assertion failed in the original source code of XTableColumnModel.
* From http://www.stephenkelvin.de/XTableColumnModel/:
* "There is one gotcha with this design: If you currently have invisible columns and change your table
* model the JTable will recreate columns, but will fail to remove any invisible columns."
*/
Assert.assertEquals(10, tcm.getColumnCount(false));
}
@Override
protected void setTableComponentData(Object[][] values) {
DefaultTableModel tableModel = getTableModel();
boolean fixedHeader = getFileEditorState().getFixedHeaders();
int firstRow = fixedHeader ? 1 : 0;
int rowCount = values.length - firstRow;
int columnCount = values.length == 0 ? 0 : values[0].length;
tableModel.setRowCount(rowCount);
tableModel.setColumnCount(columnCount);
for (int row = 0; row < rowCount; ++row) {
for (int column = 0; column < columnCount; ++column) {
tableModel.setValueAt(values[row + firstRow][column], row, column);
}
}
}
public ProductPropertiesRenderer() {
final DefaultTableModel dataModel = new DefaultTableModel();
dataModel.setColumnCount(1);
setRowHeight(14);
dataModel.setRowCount(propertyLabels.length);
setModel(dataModel);
valueFont = getFont().deriveFont(Font.PLAIN, 12);
boldFont = valueFont.deriveFont(Font.BOLD);
getColumnModel().getColumn(0).setCellRenderer(new PropertyValueCellRenderer(valueFont, boldFont));
//this.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
getTableHeader().setVisible(false);
setShowHorizontalLines(false);
setShowVerticalLines(false);
}
public AOIDetailsRenderer() {
final DefaultTableModel dataModel = new DefaultTableModel();
dataModel.setColumnCount(2);
dataModel.setRowCount(detailsLables.length);
for (int i = 0; i < detailsLables.length; i++) {
dataModel.setValueAt(detailsLables[i], i, 0);
dataModel.setValueAt("", i, 1);
}
setModel(dataModel);
valueFont = getFont().deriveFont(Font.BOLD);
getColumnModel().getColumn(1).setCellRenderer(new PropertyValueCellRenderer(valueFont));
getColumnModel().getColumn(0).setMaxWidth(120);
//this.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
getTableHeader().setVisible(false);
setShowHorizontalLines(false);
setShowVerticalLines(false);
}
private void reset(List<AnnotationExpression> exps) {
getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
DefaultTableModel model = getModel();
model.setColumnCount(2);
model.setRowCount(exps.size());
int r = -1;
for (Iterator<AnnotationExpression> it = exps.iterator(); it.hasNext();) {
AnnotationExpression annotationExpression = it.next();
r++;
model.setValueAt(annotationExpression.getUrlExp(), r, 0);
model.setValueAt(annotationExpression.getAnnotationExp(), r, 1);
}
}
private JTable newTable(){
String col[] ={"key","value"};
DefaultTableModel tm = new DefaultTableModel();
tm.setColumnCount(2);
tm.setColumnIdentifiers(col);
JTable table = new JTable(tm);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setAutoscrolls(true);
table.setMinimumSize(new Dimension(160, 100));
return table;
}
@Override
protected void finished()
{
super.finished();
// when the task is finished, clear it
doubleEntryTask = null;
// hide task-progress bar
jPanel1.setVisible(false);
// create tablemodel for the table data, which is not editable
DefaultTableModel tm = new DefaultTableModel(new Object[] {}, 0) {
@Override public boolean isCellEditable(int row, int column) { return false; }
};
// apply model to table
jTable1.setModel(tm);
// and delete all rows and columns
tm.setRowCount(0);
tm.setColumnCount(0);
// now fill the table
// check whether we have any multiple entries at all.
if (!doubleentries.isEmpty()) {
// create iterator for the list
Iterator<Integer[]> it = doubleentries.iterator();
// iterate all multiple entries
while (it.hasNext()) {
// retrieve the row data
Integer[] rowdata = it.next();
// if the rowdate has more columns than the table model, adjust
// column count
while (tm.getColumnCount()<rowdata.length) {
tm.addColumn(String.valueOf(tm.getColumnCount()+1));
}
// add it to the table model
tm.addRow((Object[])rowdata);
}
// finally, enable table
jTable1.setEnabled(true);
}
else {
JOptionPane.showMessageDialog(null,resourceMap.getString("noMultipleEntriesFoundMsg"),resourceMap.getString("noMultipleEntriesFoundTitle"),JOptionPane.PLAIN_MESSAGE);
setVisible(false);
dispose();
}
}