Java Code Examples for javax.swing.table.DefaultTableModel#setColumnCount()

The following examples show how to use javax.swing.table.DefaultTableModel#setColumnCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HidableTableColumnModelTest.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@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));
}
 
Example 2
Source File: CsvTableEditorSwing.java    From intellij-csv-validator with Apache License 2.0 6 votes vote down vote up
@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);
        }
    }
}
 
Example 3
Source File: PropertiesProvider.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
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);
}
 
Example 4
Source File: AOIDetailsProvider.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
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);
}
 
Example 5
Source File: AnnotationSettings.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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);
    }        
}
 
Example 6
Source File: MainView.java    From HiJson with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: FindDoubleEntriesTask.java    From Zettelkasten with GNU General Public License v3.0 4 votes vote down vote up
@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();
    }
}