Example usage for javax.swing.table TableModel getColumnName

List of usage examples for javax.swing.table TableModel getColumnName

Introduction

In this page you can find the example usage for javax.swing.table TableModel getColumnName.

Prototype

public String getColumnName(int columnIndex);

Source Link

Document

Returns the name of the column at columnIndex.

Usage

From source file:au.com.jwatmuff.eventmanager.export.CSVExporter.java

public static void generateFromTable(TableModel tm, OutputStream out) {
    int rows = tm.getRowCount();
    int cols = tm.getColumnCount();
    Object[][] table = new Object[rows + 1][cols];
    for (int j = 0; j < cols; j++)
        table[0][j] = tm.getColumnName(j);
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            table[i + 1][j] = tm.getValueAt(i, j);

    generateFromArray(table, out);/*from  w ww  . ja v a2 s. c  o  m*/
}

From source file:edu.scripps.fl.pubchem.xml.extract.XMLExtractor.java

public Map<Integer, String> getColumnsMap(TableModel tableModel) {
    Map<Integer, String> map = new CaseInsensitiveMap();
    for (int ii = 0; ii < tableModel.getColumnCount(); ii++) {
        String name = tableModel.getColumnName(ii);
        if (null != name) { // excel sometimes adds null columns
            name = name.replaceAll("[-\\s+]", "");
            map.put(ii, name);/*  w  w  w . j  a  va  2s  . c  o m*/
        }
    }
    return map;
}

From source file:fll.web.FullTournamentTest.java

/**
 * Find a column index in a table model by name.
 * /*from  w ww.  j av  a 2s  . co  m*/
 * @return -1 if not found
 */
private static int findColumnByName(final TableModel tableModel, final String name) {
    for (int i = 0; i < tableModel.getColumnCount(); ++i) {
        if (name.equals(tableModel.getColumnName(i))) {
            return i;
        }
    }
    return -1;
}

From source file:edu.scripps.fl.pubchem.xml.PopulateArray.java

public Map<String, Integer> getColumnsMap(TableModel tableModel) {

    Map<String, Integer> map = new CaseInsensitiveMap();
    for (int ii = 0; ii < tableModel.getColumnCount(); ii++) {
        String name = tableModel.getColumnName(ii);
        if (null != name) { // excel sometimes adds null columns
            name = name.replaceAll("[-\\s+]", "");
            map.put(name, ii);// w w w  .  ja  v  a2s  .  c om
        }
    }
    return map;
}

From source file:edu.harvard.mcz.imagecapture.RunnableJobReportDialog.java

protected void serializeTableModel() {
    PrintWriter out = null;/*  w w  w .ja v a  2s  . c  o m*/
    CSVPrinter writer = null;
    try {
        int cols = jTable.getModel().getColumnCount();
        CSVFormat csvFormat = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL)
                .withHeaderComments(jTextArea.getText());
        TableModel model = jTable.getModel();
        switch (cols) {
        case 9:
            csvFormat = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL)
                    .withHeader(model.getColumnName(0), model.getColumnName(1), model.getColumnName(2),
                            model.getColumnName(3), model.getColumnName(4), model.getColumnName(5),
                            model.getColumnName(6), model.getColumnName(7), model.getColumnName(8))
                    .withCommentMarker('*').withHeaderComments(jTextArea.getText());
            break;
        case 6:
            csvFormat = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL)
                    .withHeader(model.getColumnName(0), model.getColumnName(1), model.getColumnName(2),
                            model.getColumnName(3), model.getColumnName(4), model.getColumnName(5))
                    .withCommentMarker('*').withHeaderComments(jTextArea.getText());
            break;
        case 5:
            csvFormat = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL)
                    .withHeader(model.getColumnName(0), model.getColumnName(1), model.getColumnName(2),
                            model.getColumnName(3), model.getColumnName(4))
                    .withCommentMarker('*').withHeaderComments(jTextArea.getText());
            break;
        case 4:
            csvFormat = CSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL)
                    .withHeader(model.getColumnName(0), model.getColumnName(1), model.getColumnName(2),
                            model.getColumnName(3))
                    .withCommentMarker('*').withHeaderComments(jTextArea.getText());
            break;
        }

        log.debug(jTextArea.getText());
        log.debug(csvFormat.getHeaderComments());

        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmdd_HHmmss");
        String time = dateFormat.format(now);
        String filename = "jobreport_" + time + ".csv";
        out = new PrintWriter(filename);

        writer = new CSVPrinter(out, csvFormat);
        writer.flush();

        int rows = jTable.getModel().getRowCount();
        for (int i = 0; i < rows; i++) {
            ArrayList<String> values = new ArrayList<String>();
            for (int col = 0; col < cols; col++) {
                values.add((String) jTable.getModel().getValueAt(i, col));
            }

            writer.printRecord(values);
        }
        writer.flush();
        writer.close();
        JOptionPane.showMessageDialog(Singleton.getSingletonInstance().getMainFrame(),
                "Saved report to file: " + filename, "Report to CSV file", JOptionPane.OK_OPTION);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            out.close();
        } catch (Exception e) {
        }
        try {
            writer.close();
        } catch (Exception e) {
        }

    }
}

From source file:com.hp.alm.ali.idea.ui.MultipleItemsDialog.java

private TableColumn createColumn(int index, TableModel model, int preferredWidth, Integer minWidth) {
    TableColumn column = new TableColumn(index);
    column.setPreferredWidth(preferredWidth);
    if (minWidth != null) {
        column.setMinWidth(minWidth);/* w w  w.  j a  v a  2  s  .  com*/
    }
    column.setHeaderValue(model.getColumnName(index));
    return column;
}

From source file:uk.ac.lkl.cram.ui.report.Report.java

private void addTutorPreparationCost() {
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
    mdp.addStyledParagraphOfText("Heading2", "Cost of Preparation");
    Tbl table = factory.createTbl();/*www .  j  av a 2  s.  c  o  m*/
    Tr tableHead = factory.createTr();
    TableModel tableModel = new TutorCostTableModel(module);
    addTableCell(tableHead, tableModel.getColumnName(0), JcEnumeration.CENTER, true);
    for (int col = 1; col < 4; col++) {
        addTableCell(tableHead, tableModel.getColumnName(col), JcEnumeration.CENTER, true);
    }
    table.getContent().add(tableHead);
    for (int row = 0; row < tableModel.getRowCount(); row++) {
        boolean lastRow = row == tableModel.getRowCount() - 1;
        Tr tableRow = factory.createTr();
        if (lastRow) {
            addTableCell(tableRow, tableModel.getValueAt(row, 0).toString(), JcEnumeration.LEFT, true);
        } else {
            addSimpleTableCell(tableRow, tableModel.getValueAt(row, 0).toString());
        }
        for (int col = 1; col < 4; col++) {
            addTableCell(tableRow, CURRENCY_FORMATTER.format(tableModel.getValueAt(row, col)),
                    JcEnumeration.RIGHT, lastRow);
        }
        table.getContent().add(tableRow);
    }
    addBorders(table);
    mdp.addObject(table);
}

From source file:uk.ac.lkl.cram.ui.report.Report.java

private void addTutorSupportCost() {
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
    mdp.addStyledParagraphOfText("Heading2", "Cost of Support");
    Tbl table = factory.createTbl();/*from   w ww.  ja  v  a  2  s  .co  m*/
    Tr tableHead = factory.createTr();
    TableModel tableModel = new TutorCostTableModel(module);
    addTableCell(tableHead, tableModel.getColumnName(0), JcEnumeration.CENTER, true);
    for (int col = 4; col < 7; col++) {
        addTableCell(tableHead, tableModel.getColumnName(col), JcEnumeration.CENTER, true);
    }
    table.getContent().add(tableHead);
    for (int row = 0; row < tableModel.getRowCount(); row++) {
        boolean lastRow = row == tableModel.getRowCount() - 1;
        Tr tableRow = factory.createTr();
        if (lastRow) {
            addTableCell(tableRow, tableModel.getValueAt(row, 0).toString(), JcEnumeration.LEFT, true);
        } else {
            addSimpleTableCell(tableRow, tableModel.getValueAt(row, 0).toString());
        }
        for (int col = 4; col < 7; col++) {
            addTableCell(tableRow, CURRENCY_FORMATTER.format(tableModel.getValueAt(row, col)),
                    JcEnumeration.RIGHT, lastRow);
        }
        table.getContent().add(tableRow);
    }
    addBorders(table);
    mdp.addObject(table);
}

From source file:uk.ac.lkl.cram.ui.report.Report.java

private void addTutorPreparationHours() {
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
    mdp.addStyledParagraphOfText("Heading2", "Tutor Preparation Hours");
    Tbl table = factory.createTbl();/*from   w w  w.  jav  a 2s . co  m*/
    Tr tableHead = factory.createTr();
    TableModel tableModel = new TutorHoursTableModel(module);
    addTableCell(tableHead, tableModel.getColumnName(0), JcEnumeration.CENTER, true);
    for (int col = 1; col < 4; col++) {
        addTableCell(tableHead, tableModel.getColumnName(col), JcEnumeration.CENTER, true);
    }
    table.getContent().add(tableHead);
    for (int row = 0; row < tableModel.getRowCount(); row++) {
        boolean lastRow = row == tableModel.getRowCount() - 1;
        Tr tableRow = factory.createTr();
        if (lastRow) {
            addTableCell(tableRow, tableModel.getValueAt(row, 0).toString(), JcEnumeration.LEFT, true);
        } else {
            addSimpleTableCell(tableRow, tableModel.getValueAt(row, 0).toString());
        }
        for (int col = 1; col < 4; col++) {
            addTableCell(tableRow, FLOAT_FORMATTER.format(tableModel.getValueAt(row, col)), JcEnumeration.RIGHT,
                    lastRow);
        }
        table.getContent().add(tableRow);
    }
    addBorders(table);
    mdp.addObject(table);
}

From source file:at.tuwien.ifs.feature.evaluation.SimilarityRetrievalGUI.java

private void initButtonSaveResults() {
    btnSaveResults = new JButton("Save as ...");
    btnSaveResults.setEnabled(false); // can't save right away, need a first search
    btnSaveResults.addActionListener(new ActionListener() {
        @Override//ww w  . j a  va 2s  .  c o  m
        public void actionPerformed(ActionEvent e) {
            fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            if (fileChooser.getSelectedFile() != null) { // reusing the dialog
                fileChooser.setSelectedFile(null);
            }

            int returnVal = fileChooser.showDialog(SimilarityRetrievalGUI.this, "Save results to file ...");
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                try {
                    PrintWriter w = FileUtils.openFileForWriting("Results file",
                            fileChooser.getSelectedFile().getAbsolutePath());
                    TableModel model = resultsTable.getModel();

                    // column headers
                    for (int col = 0; col < model.getColumnCount(); col++) {
                        w.print(model.getColumnName(col) + "\t");
                    }
                    w.println();

                    // write each data row
                    for (int row = 0; row < model.getRowCount(); row++) {
                        for (int col = 0; col < model.getColumnCount(); col++) {
                            w.print(model.getValueAt(row, col) + "\t");
                        }
                        w.println();
                    }
                    w.flush();
                    w.close();
                    JOptionPane.showMessageDialog(SimilarityRetrievalGUI.this,
                            "Successfully wrote to file " + fileChooser.getSelectedFile().getAbsolutePath(),
                            "Results stored", JOptionPane.INFORMATION_MESSAGE);

                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    });
}