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:uk.ac.lkl.cram.ui.report.Report.java

private void addTutorSupportHours() {
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
    mdp.addStyledParagraphOfText("Heading2", "Tutor Support Hours");
    Tbl table = factory.createTbl();/*from w  w  w. j ava2s  . com*/
    Tr tableHead = factory.createTr();
    TableModel tableModel = new TutorHoursTableModel(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, FLOAT_FORMATTER.format(tableModel.getValueAt(row, col)), JcEnumeration.RIGHT,
                    lastRow);
        }
        table.getContent().add(tableRow);
    }
    addBorders(table);
    mdp.addObject(table);

    float[] tutor_hours_online = new float[3];
    float[] tutor_hours_f2f = new float[3];
    List<ModulePresentation> modulePresentations = module.getModulePresentations();
    for (int i = 0; i < modulePresentations.size(); i++) {
        ModulePresentation mp = modulePresentations.get(i);
        for (TLALineItem tLALineItem : module.getTLALineItems()) {
            SupportTime st = tLALineItem.getSupportTime(mp);
            float tutor_hours = st.getTotalHours(module, mp, tLALineItem);
            StudentTeacherInteraction sti = tLALineItem.getActivity().getStudentTeacherInteraction();
            if (sti.isOnline()) {
                tutor_hours_online[i] += tutor_hours;
            }
            if (sti.isTutorSupported() && sti.isLocationSpecific()) {
                tutor_hours_f2f[i] += tutor_hours;
            }
        }
    }
    mdp.addParagraphOfText("");
    table = factory.createTbl();
    tableHead = factory.createTr();
    addSimpleTableCell(tableHead, "");
    table.getContent().add(tableHead);
    for (int col = 4; col < 7; col++) {
        addTableCell(tableHead, tableModel.getColumnName(col), JcEnumeration.CENTER, true);
    }
    Tr tableRow = factory.createTr();
    addSimpleTableCell(tableRow, "Tutor hours online");
    for (int i = 0; i < tutor_hours_online.length; i++) {
        addTableCell(tableRow, INTEGER_FORMATTER.format(tutor_hours_online[i]), JcEnumeration.RIGHT, false);
    }
    table.getContent().add(tableRow);
    tableRow = factory.createTr();
    addSimpleTableCell(tableRow, "Tutor hours face-to-face");
    for (int i = 0; i < tutor_hours_f2f.length; i++) {
        addTableCell(tableRow, INTEGER_FORMATTER.format(tutor_hours_f2f[i]), JcEnumeration.RIGHT, false);
    }
    table.getContent().add(tableRow);
    addBorders(table);
    mdp.addObject(table);
}

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

private void addLearningActivities() {
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
    mdp.addStyledParagraphOfText("Heading1", "Learning Activities");
    float online_hours = 0f;
    float f2f_hours = 0f;
    Tbl table = factory.createTbl();//from   ww  w  .j  a v a 2s .c o m
    Tr tableHead = factory.createTr();
    TableModel tableModel = new ModuleReportModel(module);
    for (int col = 0; col < tableModel.getColumnCount(); col++) {
        addTableCell(tableHead, tableModel.getColumnName(col), JcEnumeration.CENTER, true);
    }
    table.getContent().add(tableHead);
    int columnCount = tableModel.getColumnCount();
    for (int row = 0; row < tableModel.getRowCount(); row++) {
        boolean lastRow = row == tableModel.getRowCount() - 1;
        Tr tableRow = factory.createTr();
        addTableCell(tableRow, tableModel.getValueAt(row, 0).toString(), JcEnumeration.LEFT, lastRow);
        for (int col = 1; col < columnCount; col++) {
            addTableCell(tableRow, FLOAT_FORMATTER.format(tableModel.getValueAt(row, col)), JcEnumeration.RIGHT,
                    lastRow);
        }
        table.getContent().add(tableRow);
    }
    addBorders(table);
    List<TLALineItem> lineItems = module.getTLALineItems();
    for (TLALineItem lineItem : lineItems) {
        TLActivity activity = lineItem.getActivity();
        float totalLearnerHourCount = lineItem.getTotalLearnerHourCount(module);
        StudentTeacherInteraction sti = activity.getStudentTeacherInteraction();
        if (sti.isOnline()) {
            online_hours += totalLearnerHourCount;
        }
        if (sti.isTutorSupported() && sti.isLocationSpecific()) {
            f2f_hours += totalLearnerHourCount;
        }
    }
    mdp.addStyledParagraphOfText("BodyText", "Number of learner hours online: " + (int) online_hours);
    mdp.addStyledParagraphOfText("BodyText", "Number of learner hours face-to-face: " + (int) f2f_hours);
    addBorders(table);
    mdp.addObject(table);
}

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

private void addSummary() {
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
    mdp.addStyledParagraphOfText("Heading1", "Summary");
    Tbl table = factory.createTbl();//  w  w w  .  ja va2  s. co  m
    Tr tableHead = factory.createTr();
    TableModel tableModel = new SummaryReportModel(module);
    int columnCount = tableModel.getColumnCount();
    for (int col = 0; col < columnCount; 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 < columnCount; col++) {
            if (row > 4) {
                addTableCell(tableRow, CURRENCY_FORMATTER.format(tableModel.getValueAt(row, col)),
                        JcEnumeration.RIGHT, lastRow);
            } else {
                addTableCell(tableRow, INTEGER_FORMATTER.format(tableModel.getValueAt(row, col)),
                        JcEnumeration.RIGHT, lastRow);
            }
        }
        table.getContent().add(tableRow);
    }
    addBorders(table);
    mdp.addObject(table);
}

From source file:org.datacleaner.widgets.result.AbstractCrosstabResultSwingRenderer.java

protected void valueCell(Object value, final ResultProducer drillToDetailResultProducer, TableModel tableModel,
        int row, int col, boolean headersIncluded, Alignment alignment) {
    final Object resultValue;

    ActionListener action = null;
    if (drillToDetailResultProducer != null) {
        final StringBuilder sb = new StringBuilder("Detailed result for [");

        sb.append(getLabelText(value));//from  w w w.java 2 s.  co  m
        sb.append(" (");

        final String cat1;
        if (headersIncluded) {
            cat1 = tableModel.getColumnName(col);
        } else {
            cat1 = tableModel.getValueAt(0, col).toString();
        }
        sb.append(cat1).append(", ");

        final String cat2 = tableModel.getValueAt(row, 0).toString();
        sb.append(cat2);

        sb.append(")]");

        action = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                _drillToDetailsCallback.drillToDetails(sb.toString(), drillToDetailResultProducer);
            }
        };
        resultValue = createActionableValuePanel(value, alignment, action, IMAGE_PATH_DRILL_TO_DETAIL);
    } else {
        resultValue = getLabelText(value);
    }

    tableModel.setValueAt(resultValue, row, col);
}

From source file:mekhq.Utilities.java

/**
 * Export a JTable to a CSV file// w  w w.ja  v  a  2 s. c  om
 * @param table
 * @param file
 * @return report
 */
public static String exportTabletoCSV(JTable table, File file) {
    String report;
    try {
        TableModel model = table.getModel();
        BufferedWriter writer = Files.newBufferedWriter(Paths.get(file.getPath()));
        String[] columns = new String[model.getColumnCount()];
        for (int i = 0; i < model.getColumnCount(); i++) {
            columns[i] = model.getColumnName(i);
        }
        CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader(columns));

        for (int i = 0; i < model.getRowCount(); i++) {
            Object[] towrite = new String[model.getColumnCount()];
            for (int j = 0; j < model.getColumnCount(); j++) {
                // use regex to remove any HTML tags
                towrite[j] = model.getValueAt(i, j).toString().replaceAll("\\<[^>]*>", "");
            }
            csvPrinter.printRecord(towrite);
        }

        csvPrinter.flush();
        csvPrinter.close();

        report = model.getRowCount() + " " + resourceMap.getString("RowsWritten.text");
    } catch (Exception ioe) {
        MekHQ.getLogger().log(Utilities.class, "exportTabletoCSV", LogLevel.INFO, "Error exporting JTable");
        report = "Error exporting JTable. See log for details.";
    }
    return report;
}

From source file:net.sourceforge.processdash.ev.ui.EVReport.java

private static void customizeTableWriter(HTMLTableWriter writer, TableModel t, String[] toolTips) {
    writer.setTableAttributes("border='1'");
    writer.setHeaderRenderer(new HTMLTableWriter.DefaultHTMLHeaderCellRenderer(toolTips));

    for (int i = t.getColumnCount(); i-- > 0;)
        if (t.getColumnName(i).endsWith(" "))
            writer.setSkipColumn(i, true);
}

From source file:edu.ku.brc.specify.config.init.PrintTableHelper.java

/**
 * @param model/*from   w w w .  j  av a  2 s.  c  o m*/
 * @return
 * @throws Exception
 */
public DynamicReport buildReport(final TableModel model, final PageSetupDlg pageSetupDlg) throws Exception {
    // Find a Sans Serif Font on the System
    String fontName = null;

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    for (java.awt.Font font : ge.getAllFonts()) {

        String fName = font.getFamily().toLowerCase();
        if (StringUtils.contains(fName, "sansserif") || StringUtils.contains(fName, "arial")
                || StringUtils.contains(fName, "verdana")) {
            fontName = font.getFamily();
            break;
        }
    }

    if (fontName == null) {
        fontName = Font._FONT_TIMES_NEW_ROMAN;
    }

    /**
     * Creates the DynamicReportBuilder and sets the basic options for the report
     */
    FastReportBuilder drb = new FastReportBuilder();

    Style columDetail = new Style();
    //columDetail.setBorder(Border.THIN);

    Style columDetailWhite = new Style();
    //columDetailWhite.setBorder(Border.THIN);
    columDetailWhite.setBackgroundColor(Color.WHITE);
    columDetailWhite.setFont(new Font(10, fontName, false));
    columDetailWhite.setHorizontalAlign(HorizontalAlign.CENTER);
    columDetailWhite.setBlankWhenNull(true);

    Style columDetailWhiteBold = new Style();
    //columDetailWhiteBold.setBorder(Border.THIN);
    columDetailWhiteBold.setBackgroundColor(Color.WHITE);

    Style titleStyle = new Style();
    titleStyle.setFont(new Font(12, fontName, true));

    // Odd Row Style
    Style oddRowStyle = new Style();
    //oddRowStyle.setBorder(Border.NO_BORDER);
    oddRowStyle.setHorizontalAlign(HorizontalAlign.CENTER);

    Color veryLightGrey = new Color(240, 240, 240);
    oddRowStyle.setBackgroundColor(veryLightGrey);
    oddRowStyle.setTransparency(Transparency.OPAQUE);

    // Create Column Headers for the Report
    for (int i = 0; i < model.getColumnCount(); i++) {
        String colName = model.getColumnName(i);

        Class<?> dataClass = model.getColumnClass(i);
        if (dataClass == Object.class) {
            if (model.getRowCount() > 0) {
                Object data = model.getValueAt(0, i);
                if (data != null) {
                    dataClass = data.getClass();
                } else {
                    // Column in first row was null so search down the rows
                    // for a non-empty cell
                    for (int j = 1; j < model.getRowCount(); j++) {
                        data = model.getValueAt(j, i);
                        if (dataClass != null) {
                            dataClass = data.getClass();
                            break;
                        }
                    }

                    if (dataClass == null) {
                        dataClass = String.class;
                    }
                }
            }
        }

        ColumnBuilder colBldr = ColumnBuilder.getInstance().setColumnProperty(colName, dataClass.getName());
        int bracketInx = colName.indexOf('[');
        if (bracketInx > -1) {
            colName = colName.substring(0, bracketInx - 1);
        }
        colBldr.setTitle(colName);

        colBldr.setStyle(columDetailWhite);

        AbstractColumn column = colBldr.build();
        drb.addColumn(column);

        Style headerStyle = new Style();
        headerStyle.setFont(new Font(11, fontName, true));
        //headerStyle.setBorder(Border.THIN);
        headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
        headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
        headerStyle.setBackgroundColor(new Color(80, 80, 80));
        headerStyle.setTransparency(Transparency.OPAQUE);
        headerStyle.setTextColor(new Color(255, 255, 255));
        column.setHeaderStyle(headerStyle);
    }

    drb.setTitle(pageSetupDlg.getPageTitle());
    drb.setTitleStyle(titleStyle);

    drb.setLeftMargin(20);
    drb.setRightMargin(20);
    drb.setTopMargin(10);
    drb.setBottomMargin(10);

    drb.setPrintBackgroundOnOddRows(true);
    drb.setOddRowBackgroundStyle(oddRowStyle);
    drb.setColumnsPerPage(new Integer(1));
    drb.setUseFullPageWidth(true);
    drb.setColumnSpace(new Integer(5));

    // This next line causes an exception
    // Event with DynamicReport 3.0.12 and JasperReposrts 3.7.3
    //drb.addAutoText(AutoText.AUTOTEXT_PAGE_X_OF_Y, AutoText.POSITION_FOOTER, AutoText.ALIGMENT_CENTER);

    Page[] pageSizes = new Page[] { Page.Page_Letter_Portrait(), Page.Page_Legal_Portrait(),
            Page.Page_A4_Portrait(), Page.Page_Letter_Landscape(), Page.Page_Legal_Landscape(),
            Page.Page_A4_Landscape() };
    int pageSizeInx = pageSetupDlg.getPageSize() + (pageSetupDlg.isPortrait() ? 0 : 3);
    drb.setPageSizeAndOrientation(pageSizes[pageSizeInx]);

    DynamicReport dr = drb.build();

    return dr;
}

From source file:edu.ku.brc.specify.tasks.ReportsBaseTask.java

/**
 * @param model//from   ww w  .j  a  va 2 s. c  om
 * @return
 * @throws Exception
 */
public DynamicReport buildReport(final TableModel model, final PageSetupDlg pageSetupDlg) throws Exception {
    // Find a Sans Serif Font on the System
    String fontName = null;

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    for (java.awt.Font font : ge.getAllFonts()) {
        String fName = font.getFamily().toLowerCase();
        if (StringUtils.contains(fName, "sansserif") || StringUtils.contains(fName, "arial")
                || StringUtils.contains(fName, "verdana")) {
            fontName = font.getFamily();
        }
    }

    if (fontName == null) {
        fontName = Font._FONT_TIMES_NEW_ROMAN;
    }

    /**
     * Creates the DynamicReportBuilder and sets the basic options for the report
     */
    FastReportBuilder drb = new FastReportBuilder();

    Style columDetail = new Style();
    //columDetail.setBorder(Border.THIN);

    Style columDetailWhite = new Style();
    //columDetailWhite.setBorder(Border.THIN);
    columDetailWhite.setBackgroundColor(Color.WHITE);
    columDetailWhite.setFont(new Font(10, fontName, true));
    columDetailWhite.setHorizontalAlign(HorizontalAlign.CENTER);
    columDetailWhite.setBlankWhenNull(true);

    Style columDetailWhiteBold = new Style();
    //columDetailWhiteBold.setBorder(Border.THIN);
    columDetailWhiteBold.setBackgroundColor(Color.WHITE);

    Style titleStyle = new Style();
    titleStyle.setFont(new Font(14, fontName, true));

    /*Style numberStyle = new Style();
    numberStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
            
    Style amountStyle = new Style();
    amountStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
    amountStyle.setBackgroundColor(Color.cyan);
    amountStyle.setTransparency(Transparency.OPAQUE);*/

    //Font dataRowFont = new Font(10, Font._FONT_VERDANA, true);

    // Odd Row Style
    Style oddRowStyle = new Style();
    //oddRowStyle.setBorder(Border.NO_BORDER);
    //oddRowStyle.setFont(dataRowFont);
    oddRowStyle.setHorizontalAlign(HorizontalAlign.CENTER);

    Color veryLightGrey = new Color(240, 240, 240);
    oddRowStyle.setBackgroundColor(veryLightGrey);
    oddRowStyle.setTransparency(Transparency.OPAQUE);

    // Event Row Style
    //Style evenRowStyle = new Style();
    //evenRowStyle.setBorder(Border.NO_BORDER);
    //evenRowStyle.setFont(dataRowFont);

    // Create Column Headers for the Report
    for (int i = 0; i < model.getColumnCount(); i++) {
        String colName = model.getColumnName(i);

        Class<?> dataClass = model.getColumnClass(i);
        if (dataClass == Object.class) {
            if (model.getRowCount() > 0) {
                Object data = model.getValueAt(0, i);
                if (data != null) {
                    dataClass = data.getClass();
                } else {
                    // Column in first row was null so search down the rows
                    // for a non-empty cell
                    for (int j = 1; j < model.getRowCount(); j++) {
                        data = model.getValueAt(j, i);
                        if (dataClass != null) {
                            dataClass = data.getClass();
                            break;
                        }
                    }

                    if (dataClass == null) {
                        dataClass = String.class;
                    }
                }
            }
        }

        ColumnBuilder colBldr = ColumnBuilder.getInstance().setColumnProperty(colName, dataClass.getName());
        int bracketInx = colName.indexOf('[');
        if (bracketInx > -1) {
            colName = colName.substring(0, bracketInx - 1);
        }
        colBldr.setTitle(colName);
        //colBldr.setWidth(new Integer(100));

        colBldr.setStyle(columDetailWhite);
        //colBldr.setHeaderStyle(columDetailWhite);

        AbstractColumn column = colBldr.build();
        drb.addColumn(column);

        Style headerStyle = new Style();
        headerStyle.setFont(new Font(12, fontName, true));
        //headerStyle.setBorder(Border.THIN);
        headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
        headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
        headerStyle.setBackgroundColor(new Color(80, 80, 80));
        headerStyle.setTransparency(Transparency.OPAQUE);
        headerStyle.setTextColor(new Color(255, 255, 255));
        column.setHeaderStyle(headerStyle);
    }

    drb.setTitle(pageSetupDlg.getPageTitle());
    drb.setTitleStyle(titleStyle);
    //drb.setTitleHeight(new Integer(30));
    //drb.setSubtitleHeight(new Integer(20));
    //drb.setDetailHeight(new Integer(15));
    //drb.setDefaultStyles(null, null, null, evenRowStyle);

    drb.setLeftMargin(20);
    drb.setRightMargin(20);
    drb.setTopMargin(10);
    drb.setBottomMargin(10);

    drb.setPrintBackgroundOnOddRows(true);
    drb.setOddRowBackgroundStyle(oddRowStyle);
    drb.setColumnsPerPage(new Integer(1));
    drb.setUseFullPageWidth(true);
    drb.setColumnSpace(new Integer(5));

    // This next line causes an exception
    // Event with DynamicReport 3.0.12 and JasperReposrts 3.7.3
    //drb.addAutoText(AutoText.AUTOTEXT_PAGE_X_OF_Y, AutoText.POSITION_FOOTER, AutoText.ALIGMENT_CENTER);

    Page[] pageSizes = new Page[] { Page.Page_Letter_Portrait(), Page.Page_Legal_Portrait(),
            Page.Page_A4_Portrait(), Page.Page_Letter_Landscape(), Page.Page_Legal_Landscape(),
            Page.Page_A4_Landscape() };
    int pageSizeInx = pageSetupDlg.getPageSize() + (pageSetupDlg.isPortrait() ? 0 : 3);
    drb.setPageSizeAndOrientation(pageSizes[pageSizeInx]);

    DynamicReport dr = drb.build();

    return dr;
}

From source file:org.broad.igv.cbio.FilterGeneNetworkUI.java

/**
 * Export the current table to a tab-delimited file.
 * String exported should be the same as what user sees
 *
 * @param outFile/*from w  w  w .  j a v a2s  .  c o m*/
 * @throws IOException
 */
private void saveTable(File outFile) throws FileNotFoundException {
    PrintWriter writer = new PrintWriter(outFile);
    TableModel model = geneTable.getModel();
    String delimiter = "\t";

    //Write header
    String header = model.getColumnName(0);
    for (int col = 1; col < model.getColumnCount(); col++) {
        header += delimiter + model.getColumnName(col);
    }

    writer.println(header);

    for (int row = 0; row < model.getRowCount(); row++) {
        String rowStr = "" + model.getValueAt(row, 0);
        for (int col = 1; col < model.getColumnCount(); col++) {
            rowStr += delimiter + model.getValueAt(row, col);
        }

        writer.println(rowStr);
    }

    writer.flush();
    writer.close();
}

From source file:org.datacleaner.result.AnnotatedRowResultTest.java

private void performAssertions(AnnotatedRowsResult result) {
    assertEquals(2, result.getAnnotatedRowCount());
    assertEquals("[MockInputColumn[name=foo], MockInputColumn[name=bar]]", result.getInputColumns().toString());
    assertEquals("[MockInputColumn[name=foo]]", Arrays.toString(result.getHighlightedColumns()));
    assertNotNull(result.getSampleRows());
    TableModel tableModel = result.toTableModel();
    assertNotNull(tableModel);//ww w  . j  av a 2 s.co  m
    assertEquals(2, tableModel.getColumnCount());
    assertEquals("foo", tableModel.getColumnName(0));
    assertEquals("bar", tableModel.getColumnName(1));

    tableModel = result.toDistinctValuesTableModel(result.getHighlightedColumns()[0]);
    assertNotNull(tableModel);
    assertEquals(2, tableModel.getColumnCount());
    assertEquals("foo", tableModel.getColumnName(0));
    assertEquals("Count in dataset", tableModel.getColumnName(1));
}