Java Code Examples for javax.swing.JTable#getColumnName()

The following examples show how to use javax.swing.JTable#getColumnName() . 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: Options.java    From ramus with GNU General Public License v3.0 6 votes vote down vote up
public static void setJTableOptions(final String name, final JTable table,
                                    final Properties properties) {
    setInteger(name + "_col_count", table.getColumnCount(), properties);
    final String cNames[] = new String[table.getColumnCount()];
    for (int i = 0; i < cNames.length; i++)
        cNames[i] = table.getColumnName(i);

    for (final String element : cNames) {
        try {
            setInteger(name + "_col_" + element + "_width", table
                    .getColumn(element).getWidth(), properties);
            setInteger(name + "_col_" + element + "_index",
                    table.convertColumnIndexToView(table.getColumn(element)
                            .getModelIndex()), properties);
        } catch (final Exception e) {

        }
    }
}
 
Example 2
Source File: TablePopupFactory.java    From Llunatic with GNU General Public License v3.0 6 votes vote down vote up
@Override
public JPopupMenu createPopupMenu(int row, int column, Node[] selectedNodes, Component component) {
    JPopupMenu popup = super.createPopupMenu(row, column, selectedNodes, component);
    if (column > 0 && selectedNodes.length == 1) {
        JTable table = (JTable) component;
        if (table.getSelectedColumn() > 0) {
            TableTupleNode tuple = (TableTupleNode) selectedNodes[0];
            if (tuple.isMcResultNode()) {
                String columnName = table.getColumnName(column);
                Cell c = tuple.getCell(columnName);
                if (c.getValue().getType().equals(SpeedyConstants.LLUN)) {
                    popup.add(separator);
                    popup.add(cellGroup);
                }
            }
        }
    }
    return popup;
}
 
Example 3
Source File: JavaElementPropertyAccessor.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
public String getColumnName(int c) {
    JTable table = (JTable) component;
    JTableHeader tableHeader = table.getTableHeader();
    String columnName;
    if (tableHeader != null) {
        columnName = tableHeader.getColumnModel().getColumn(c).getHeaderValue().toString();
    } else {
        columnName = table.getColumnName(c);
    }
    return columnName;
}
 
Example 4
Source File: StringTableCellEditor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Component getTableCellEditorComponent(final JTable table, Object value, boolean isSelected, final int row, final int column) {
    final JComponent c = (JComponent) super.getTableCellEditorComponent(table, value, isSelected, row, column);      
    
    this.tableModel = table.getModel();
    this.columnName = table.getColumnName(column);
    this.modelRow = table.convertRowIndexToModel(row);
    this.modelColumn = table.convertColumnIndexToModel(column);  
    this.tc = c instanceof JTextComponent ? (JTextComponent) c : null;

    JPanel panel = new JPanel(new BorderLayout()) {
        @Override
        public void addNotify() {
            super.addNotify();
            c.requestFocus();
        }
    };
    panel.add(c);
    if (suppressEditorBorder) {
        c.setBorder(BorderFactory.createEmptyBorder());
    }
    panel.add(customEditorButton, BorderLayout.EAST);
    panel.revalidate();
    panel.repaint();

    return panel;
}
 
Example 5
Source File: Options.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
public static void getJTableOptions(final String name, final JTable table,
                                    final Properties properties) {
    final Integer colCount = getObjectInteger(name + "_col_count",
            properties);
    if (colCount == null || colCount.intValue() != table.getColumnCount())
        return;
    final String cNames[] = new String[table.getColumnCount()];
    final Object cols[] = new Object[table.getColumnCount()];

    for (int i = 0; i < cNames.length; i++) {
        cNames[i] = table.getColumnName(i);
        cols[i] = table.getColumnModel().getColumn(i);
    }

    for (final String element : cNames) {
        final int width = getInteger(name + "_col_" + element + "_width",
                table.getColumn(element).getWidth(), properties);
        table.getColumn(element).setPreferredWidth(width);
    }

    final TableColumnModel cm = table.getColumnModel();
    final int tci[] = new int[cNames.length];
    for (int i = 0; i < cNames.length; i++)
        cm.removeColumn((TableColumn) cols[i]);

    for (int i = 0; i < cNames.length; i++) {
        tci[i] = getInteger(name + "_col_" + cNames[i] + "_index", i,
                properties);
    }

    for (int i = 0; i < cNames.length; i++)
        for (int j = 0; j < cNames.length; j++)
            if (tci[j] == i)
                cm.addColumn((TableColumn) cols[j]);

}
 
Example 6
Source File: JtableUtils.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 5 votes vote down vote up
public static void exportModelToCSV(JTable table, File file, Boolean hflag) throws IOException {
        removeEmptyRows(table);
        cancelEditing(table);
        ArrayList<String[]> data = new ArrayList<>();
        ArrayList<Integer> expList = new ArrayList<>();
        ArrayList<String> row = new ArrayList<>();
        int colCount = table.getColumnModel().getColumnCount(),
                rowCount = table.getModel().getRowCount(), i;
        for (i = 0; i < colCount; i++) {
            Object hval = table.getColumnName(i);
            if (hval == null || "".equals(hval.toString())) {
                expList.add(i);
            } else {
                row.add(hval.toString());
            }
        }
        data.add(row.toArray(new String[row.size()]));
        for (i = 0; i < rowCount; i++) {
            row.clear();
            for (int j = 0; j < colCount; j++) {
                if (!expList.contains(j)) {
                    Object val = table.getModel().getValueAt(i, j);
                    row.add((val == null) ? "" : val.toString());
                }
            }
            data.add(row.toArray(new String[row.size()]));
        }
//        try (CSVWriter<String[]> csvWriter = CSVWriterBuilder.newDefaultWriter(new FileWriter(file))) {
//            csvWriter.writeAll(data);
//        }

    }
 
Example 7
Source File: StatPanel.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
protected void initLayout() {
  final boolean hasTech = !TechAdvance.getTechAdvances(gameData, null).isEmpty();
  // do no include a grid box for tech if there is no tech
  setLayout(new GridLayout((hasTech ? 2 : 1), 1));
  final JTable statsTable = new JTable(dataModel);
  statsTable.getTableHeader().setReorderingAllowed(false);
  statsTable.getColumnModel().getColumn(0).setPreferredWidth(175);
  add(new JScrollPane(statsTable));
  // if no technologies, do not show the tech table
  if (!hasTech) {
    return;
  }
  final JTable techTable = new JTable(techModel);
  techTable.getTableHeader().setReorderingAllowed(false);
  techTable.getColumnModel().getColumn(0).setPreferredWidth(500);
  // show icons for players:
  final TableCellRenderer componentRenderer = new JComponentTableCellRenderer();
  for (int i = 1; i < techTable.getColumnCount(); i++) {
    final TableColumn column = techTable.getColumnModel().getColumn(i);
    column.setHeaderRenderer(componentRenderer);
    final String player = techTable.getColumnName(i);
    final JLabel value = new JLabel("", getIcon(player), SwingConstants.CENTER);
    value.setToolTipText(player);
    column.setHeaderValue(value);
  }
  add(new JScrollPane(techTable));
}
 
Example 8
Source File: CSV.java    From cropplanning with GNU General Public License v3.0 4 votes vote down vote up
public void exportJTable( String filename, String title, JTable jtable) {

    CsvWriter csvOut = new CsvWriter( filename );
    // mark text with double quotes
    csvOut.setTextQualifier('"');
    // set default comment character to hash
    csvOut.setComment('#');

    int columnCount = jtable.getColumnCount();

    try {
      // write comment about date, time, etc
      csvOut.writeComment( " Created by CropPlanning Software" );
      csvOut.writeComment( " Available at https://github.com/claytonrcarter/cropplanning" );
      csvOut.writeComment( " Records exported: " + title );
      csvOut.writeComment( " Exported: " + new Date().toString() );


      String[] buffer = new String[columnCount];


      // create header row
      for ( int col = 0; col < jtable.getColumnCount(); col++ ) {
          String headName;
          if ( jtable instanceof CPSTable )
              headName = jtable.getColumnModel().getColumn(col).getHeaderValue().toString();
          else
              headName = jtable.getColumnName( col );
          buffer[col] = headName;
      }
      csvOut.writeRecord( buffer );

      // now do the rest of the data
      int row;
      for ( row = 0; row < jtable.getRowCount(); row++ ) {
        buffer = new String[columnCount];

         for ( int col = 0; col < jtable.getColumnCount(); col++ ) {
            Object o = jtable.getValueAt( row, col );
//               TODOLists.debug( "PDFExporter", "Row " + row + " column " + col );
//               TODOLists.debug( "PDFExporter", "Value is " + (( o==null) ? "NULL" : o.toString()) );
            if ( o == null ) {
              buffer[col] = "";
            }
            else if ( o instanceof Date )
              buffer[col] = CPSDateValidator.format( (Date) o );
            else if ( o instanceof Float )
              buffer[col] = CPSRecord.formatFloat( ((Float) o).floatValue(), 3);
            else if ( o instanceof Double )
              buffer[col] = CPSRecord.formatFloat( ((Double) o).floatValue(), 3);
            else
              buffer[col] = o.toString();

         }
         csvOut.writeRecord( buffer );
      }

      // write comment "EOF"
      csvOut.writeComment( " End of file" );

      // close
      csvOut.close();

      debug( "exported " + row + " records" );
    }
    catch ( Exception ignore ) { ignore.printStackTrace(); }


  }