Example usage for javax.swing.table DefaultTableModel getColumnName

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

Introduction

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

Prototype

public String getColumnName(int column) 

Source Link

Document

Returns the column name.

Usage

From source file:com.qualixium.executor.command.CommandHelper.java

public static JsonNode getJsonNodeFromCommandsModel(DefaultTableModel model) {
    ArrayNode jsonArray = MAPPER.createArrayNode();

    for (int i = 0; i < model.getRowCount(); i++) {
        ObjectNode jsonNode = MAPPER.createObjectNode();

        String name = (String) model.getValueAt(i, 0);
        String command = ((String) model.getValueAt(i, 1)).replace("\\", "\\\\");

        jsonNode.put(model.getColumnName(0), name);
        jsonNode.put(model.getColumnName(1), command);

        jsonArray.add(jsonNode);/*from  w  w w.  j ava 2  s.  co  m*/
    }

    return jsonArray;
}

From source file:edu.ku.brc.af.ui.forms.formatters.DataObjFieldFormatMultiplePanel.java

/**
 * @param model//from   w w  w .jav a2s.c o  m
 */
protected void addTableModelListener(DefaultTableModel model) {
    TableModelListener tml = new TableModelListener() {
        public void tableChanged(TableModelEvent e) {
            int row = e.getFirstRow();
            int column = e.getColumn();
            DefaultTableModel localModel = (DefaultTableModel) e.getSource();
            String columnName = localModel.getColumnName(column);

            if (columnName.equals(FIELD_VALUE_COL)) {
                int formatColumn = formatSwitchTbl.getColumnModel().getColumnIndex(DISPLAY_FORMAT_COL);
                DataObjDataFieldFormat format = (DataObjDataFieldFormat) localModel.getValueAt(row,
                        formatColumn);
                String value = (String) localModel.getValueAt(row, column);
                format.setValue(value);
                enableUIControls();
            }
        }
    };
    model.addTableModelListener(tml);
}

From source file:de.stefanwndelmann.zy1270logger.ZY1270LoggerMain.java

/**
 * Reads the Data from the JTable and returns it as a CSV String.
 * // w w  w  . j ava2  s.  c  o  m
 * @return 
 */
private String getCSVData() {
    DefaultTableModel dtm = (DefaultTableModel) resultTable.getModel();
    int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();
    String data = "";
    for (int i = 0; i < nCol; i++) {
        data += dtm.getColumnName(i) + ";";
    }
    data = data.substring(0, data.length() - 1) + "\n";
    for (int i = 0; i < nRow; i++) {
        for (int j = 0; j < nCol; j++) {
            data += dtm.getValueAt(i, j) + ";";
        }
        data = data.substring(0, data.length() - 1) + "\n";
    }
    return data;
}

From source file:com.mirth.connect.client.ui.components.MirthTable.java

private JPopupMenu getColumnMenu() {
    JPopupMenu columnMenu = new JPopupMenu();
    DefaultTableModel model = (DefaultTableModel) getModel();

    for (int i = 0; i < model.getColumnCount(); i++) {
        final String columnName = model.getColumnName(i);
        // Get the column object by name. Using an index may not return the column object if the column is hidden
        TableColumnExt column = getColumnExt(columnName);

        // Create the menu item
        final JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(columnName);
        // Show or hide the checkbox
        menuItem.setSelected(column.isVisible());

        menuItem.addActionListener(new ActionListener() {
            @Override/*from w w  w.  j  a  v  a2s . c om*/
            public void actionPerformed(ActionEvent evt) {
                TableColumnExt column = getColumnExt(menuItem.getText());
                // Determine whether to show or hide the selected column
                boolean enable = !column.isVisible();
                // Do not hide a column if it is the last remaining visible column              
                if (enable || getColumnCount() > 1) {
                    column.setVisible(enable);
                    saveColumnOrder();
                }
            }
        });

        columnMenu.add(menuItem);
    }

    columnMenu.addSeparator();

    JMenuItem menuItem = new JMenuItem("Restore Default");
    menuItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            restoreDefaultColumnPreferences();

        }

    });
    columnMenu.add(menuItem);

    return columnMenu;
}

From source file:maltcms.ui.fileHandles.csv.CSV2JFCLoader.java

private DefaultTableModel removeColumn(DefaultTableModel dtm, int column) {
    Object[][] data = new Object[dtm.getRowCount()][dtm.getColumnCount() - 1];
    Object[] names = new Object[dtm.getColumnCount() - 1];
    int cnt = 0;//  w w w  .ja va  2s  .  c om
    for (int j = 0; j < dtm.getColumnCount(); j++) {
        if (j != column) {
            names[cnt++] = dtm.getColumnName(j);
        }
    }
    for (int i = 0; i < dtm.getRowCount(); i++) {
        cnt = 0;
        for (int j = 0; j < dtm.getColumnCount(); j++) {
            if (j != column) {
                data[i][cnt++] = dtm.getValueAt(i, j);
            }
        }
    }
    Logger.getLogger(getClass().getName()).log(Level.INFO,
            "Table model has {0} rows and {1} columns with {2} labels",
            new Object[] { data.length, data[0].length, names.length });
    return new DefaultTableModel(data, names);
}

From source file:maltcms.ui.fileHandles.csv.CSV2JFCLoader.java

@Override
public void run() {
    CSV2TableLoader tl = new CSV2TableLoader(this.ph, this.is);

    DefaultTableModel dtm;
    try {//  ww  w. ja va 2s.com
        dtm = tl.call();
        if (this.mode == CHART.XY) {
            XYSeriesCollection cd = new XYSeriesCollection();
            for (int j = 0; j < dtm.getColumnCount(); j++) {
                XYSeries xys = new XYSeries(dtm.getColumnName(j));
                for (int i = 0; i < dtm.getRowCount(); i++) {
                    Object o = dtm.getValueAt(i, j);
                    try {
                        double d = Double.parseDouble(o.toString());
                        xys.add(i, d);
                        Logger.getLogger(getClass().getName()).log(Level.INFO, "Adding {0} {1} {2}",
                                new Object[] { i, d, dtm.getColumnName(j) });
                    } catch (Exception e) {
                    }
                }
                cd.addSeries(xys);
            }
            XYLineAndShapeRenderer d = new XYLineAndShapeRenderer(true, false);
            XYPlot xyp = new XYPlot(cd, new NumberAxis("category"), new NumberAxis("value"), d);

            JFreeChart jfc = new JFreeChart(this.title, xyp);
            jtc.setChart(jfc);
            Logger.getLogger(getClass().getName()).info("creating chart done");
        } else if (this.mode == CHART.MATRIX) {
            DefaultXYZDataset cd = new DefaultXYZDataset();
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Name of column 0: {0}",
                    dtm.getColumnName(0));
            if (dtm.getColumnName(0).isEmpty()) {
                Logger.getLogger(getClass().getName()).info("Removing column 0");
                dtm = removeColumn(dtm, 0);
            }
            if (dtm.getColumnName(dtm.getColumnCount() - 1).equalsIgnoreCase("filename")) {
                dtm = removeColumn(dtm, dtm.getColumnCount() - 1);
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < dtm.getRowCount(); i++) {
                for (int j = 0; j < dtm.getColumnCount(); j++) {
                    sb.append(dtm.getValueAt(i, j) + " ");
                }
                sb.append("\n");
            }
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Table before sorting: {0}", sb.toString());
            //                dtm = sort(dtm);
            StringBuilder sb2 = new StringBuilder();
            for (int i = 0; i < dtm.getRowCount(); i++) {
                for (int j = 0; j < dtm.getColumnCount(); j++) {
                    sb2.append(dtm.getValueAt(i, j) + " ");
                }
                sb2.append("\n");
            }
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Table after sorting: {0}", sb2.toString());
            int rows = dtm.getRowCount();
            int columns = dtm.getColumnCount();
            Logger.getLogger(getClass().getName()).log(Level.INFO, "Storing {0} * {1} elements, {2} total!",
                    new Object[] { columns, rows, rows * columns });
            double[][] data = new double[3][(columns * rows)];
            ArrayDouble.D1 dt = new ArrayDouble.D1((columns) * rows);
            double min = Double.POSITIVE_INFINITY;
            double max = Double.NEGATIVE_INFINITY;
            EvalTools.eqI(rows, columns, this);
            int k = 0;
            for (int i = 0; i < dtm.getRowCount(); i++) {
                for (int j = 0; j < dtm.getColumnCount(); j++) {

                    Object o = dtm.getValueAt(i, j);
                    try {
                        double d = Double.parseDouble(o.toString());
                        if (d < min) {
                            min = d;
                        }
                        if (d > max) {
                            max = d;
                        }
                        data[0][k] = (double) i;
                        data[1][k] = (double) j;
                        data[2][k] = d;
                        dt.set(k, d);
                        k++;
                        //System.out.println("Adding "+i+" "+d+" "+dtm.getColumnName(j));
                    } catch (Exception e) {
                    }
                }
                //cd.addSeries(xys);
            }
            cd.addSeries(this.title, data);
            XYBlockRenderer xyb = new XYBlockRenderer();
            GradientPaintScale ps = new GradientPaintScale(ImageTools.createSampleTable(256), min, max,
                    ImageTools
                            .rampToColorArray(new ColorRampReader().readColorRamp("res/colorRamps/bcgyr.csv")));

            xyb.setPaintScale(ps);
            final String[] colnames = new String[dtm.getColumnCount()];
            for (int i = 0; i < colnames.length; i++) {
                colnames[i] = dtm.getColumnName(i);
            }
            NumberAxis na1 = new SymbolAxis("category", colnames);
            na1.setVerticalTickLabels(false);
            NumberAxis na2 = new SymbolAxis("category", colnames);
            na1.setVerticalTickLabels(true);
            XYPlot xyp = new XYPlot(cd, na1, na2, xyb);
            xyb.setSeriesToolTipGenerator(0, new XYToolTipGenerator() {

                @Override
                public String generateToolTip(XYDataset xyd, int i, int i1) {
                    return "[" + colnames[xyd.getX(i, i1).intValue()] + ":"
                            + colnames[xyd.getY(i, i1).intValue()] + "] = "
                            + ((XYZDataset) xyd).getZValue(i, i1) + "";
                }
            });

            JFreeChart jfc = new JFreeChart(this.title, xyp);
            NumberAxis values = new NumberAxis("value");
            values.setAutoRange(false);
            values.setRangeWithMargins(min, max);
            PaintScaleLegend psl = new PaintScaleLegend(ps, values);
            psl.setBackgroundPaint(jfc.getBackgroundPaint());
            jfc.addSubtitle(psl);
            psl.setStripWidth(50);
            psl.setPadding(20, 20, 20, 20);
            psl.setHeight(200);
            psl.setPosition(RectangleEdge.RIGHT);
            jtc.setChart(jfc);
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
    }

    ph.finish();
}

From source file:maltcms.ui.fileHandles.csv.CSV2JFCLoader.java

private DefaultTableModel sortByRows(DefaultTableModel dtm, int[] permutation) {
    Object[][] modelByRows = new Object[dtm.getRowCount()][dtm.getColumnCount()];
    for (int i = 0; i < dtm.getRowCount(); i++) {
        modelByRows[i] = new Object[dtm.getColumnCount()];
        for (int j = 0; j < dtm.getColumnCount(); j++) {
            Object o = dtm.getValueAt(permutation[i], j);
            //if(o instanceof Double) {
            modelByRows[i][j] = o;//  ww  w. j a  v a2  s .  co m
            //}
        }
    }
    Object[] names = new Object[dtm.getRowCount()];
    for (int i = 0; i < dtm.getRowCount(); i++) {
        names[i] = dtm.getColumnName(permutation[i]);
    }
    Logger.getLogger(getClass().getName()).log(Level.INFO,
            "Table model has {0} rows and {1} columns with {2} labels",
            new Object[] { modelByRows.length, modelByRows[0].length, names.length });
    return new DefaultTableModel(modelByRows, names);
}

From source file:maltcms.ui.fileHandles.csv.CSV2JFCLoader.java

private DefaultTableModel sortByColumns(DefaultTableModel dtm, int[] permutation) {
    Object[][] modelByRows = new Object[dtm.getRowCount()][dtm.getColumnCount()];
    for (int i = 0; i < dtm.getRowCount(); i++) {
        modelByRows[i] = new Object[dtm.getColumnCount()];
        for (int j = 0; j < dtm.getColumnCount(); j++) {
            Object o = dtm.getValueAt(i, permutation[j]);
            //if(o instanceof Double) {
            modelByRows[i][j] = o;//from w  w  w . j a  v a 2 s  .  c  o  m
            //}
        }
    }
    Object[] names = new Object[dtm.getRowCount()];
    for (int i = 0; i < dtm.getRowCount(); i++) {
        names[i] = dtm.getColumnName(permutation[i]);
    }
    Logger.getLogger(getClass().getName()).log(Level.INFO,
            "Table model has {0} rows and {1} columns with {2} labels",
            new Object[] { modelByRows.length, modelByRows[0].length, names.length });
    return new DefaultTableModel(modelByRows, names);
}

From source file:frames.MainGUI.java

public void writeReport() {
    for (int i = 0; i < REPORT_FILES.length; i++) {
        try {//w  ww .  j a  va 2s  . c  o  m
            //                FileWriter fw = new FileWriter(REPORT_FILES[i]);
            //                File f = new File(REPORT_FILES[i]);
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(new FileOutputStream(REPORT_FILES[i]), "UTF-8"));
            //                BufferedWriter writer = new BufferedWriter(fw);
            DefaultTableModel model = (DefaultTableModel) REPORT_TABLES[i].getModel();
            String s = "";
            for (int k = 0; k < model.getColumnCount(); k++) {
                s += model.getColumnName(k) + ",";
            }
            s += "\n";
            int rows = model.getRowCount();

            for (int j = 0; j < rows; j++) {
                s += this.getTableRow(REPORT_TABLES[i], j);
            }
            writer.write(s);
            writer.close();

        } catch (Exception ex) {
        }

    }
}

From source file:dataviewer.DataViewer.java

private void transpose(DefaultTableModel model) {
    try {/*from   www. ja  v  a2s  .  com*/
        if (model.getColumnCount() == 0) {
            return;
        }
        DefaultTableModel new_model = new DefaultTableModel();

        // if not transposed
        if (!model.getColumnName(0).equals("_field_") && transpose == true) {
            new_model.addColumn("_field_");
            for (int r = 1; r <= model.getRowCount(); ++r) {
                new_model.addColumn("_col_" + r);
            }

            long n = 1;
            for (int c = 0; c < model.getColumnCount(); ++c) {
                Object[] s = new Object[new_model.getColumnCount()];
                String colname = model.getColumnName(c);
                int l = 0;
                s[l++] = colname;
                for (int r = 0; r < model.getRowCount(); ++r) {
                    s[r + l] = model.getValueAt(r, c);
                }
                new_model.addRow(s);
            }
        }
        if (model.getColumnName(0).equals("_field_") && transpose == false) {
            // if transposed
            for (int r = 0; r < model.getRowCount(); ++r) {
                new_model.addColumn(model.getValueAt(r, 0));
            }

            List<Integer> colIdx = new ArrayList();
            for (int c = 0; c < model.getColumnCount(); ++c) {
                if (!model.getColumnName(c).equals("_field_")) {
                    colIdx.add(c);
                }
            }

            for (int c = 0; c < colIdx.size(); ++c) {
                Object[] s = new Object[new_model.getColumnCount()];
                for (int r = 0; r < model.getRowCount(); ++r) {
                    s[r] = model.getValueAt(r, colIdx.get(c));
                }
                new_model.addRow(s);
            }
        }

        renderData(new_model);
    } catch (Exception e) {
        txt_count.setText(e.getMessage());
    }
}