Example usage for javax.swing JTable getValueAt

List of usage examples for javax.swing JTable getValueAt

Introduction

In this page you can find the example usage for javax.swing JTable getValueAt.

Prototype

public Object getValueAt(int row, int column) 

Source Link

Document

Returns the cell value at row and column.

Usage

From source file:funcoes.funcoes.java

public static String calculaTotal(JTable tabela, int coluna, String tipo) throws ParseException {
    double valorTotal = 0.0;
    double valorAdd = 0.0;
    String retorno = null;//w  w  w .j a v  a2s  .  c  o  m

    for (int a = 1; a <= tabela.getRowCount(); a++) {

        try {
            valorAdd = Double
                    .parseDouble(funcoes.formatoParaInserir(tabela.getValueAt(a - 1, coluna).toString()));
            if (valorAdd < 0.0 && tipo.equalsIgnoreCase("-")) {

                valorTotal = valorTotal + valorAdd;
                retorno = "R$ " + funcoes.paraFormatoDinheiro(valorTotal);

            } else {
                if (valorAdd >= 0.0 && tipo.equalsIgnoreCase("+")) {

                    valorTotal = valorTotal + valorAdd;
                    retorno = "R$ " + funcoes.paraFormatoDinheiro(valorTotal);

                } else {
                    if (tipo.equalsIgnoreCase("0")) {
                        valorTotal = valorTotal + valorAdd;
                        retorno = "R$ " + funcoes.paraFormatoDinheiro(valorTotal);

                    }
                }

            }

        } catch (NullPointerException ex) {
        }
    }
    return retorno;

}

From source file:Main.java

/**
 * Pack specified column. Sets the preferred width of the specified visible
 * column so it is will be just wide enough to show the column head and the
 * widest cell value in the column + {@code margin} each side.
 * //  w w  w  .java  2  s.  com
 * Taken from http://www.exampledepot.com/egs/javax.swing.table/PackCol.html
 * 
 * NB: If a table has more than 100 rows, then only the width of the column
 * header will be checked and not the widest cell value. Otherwise it could
 * take too long.
 * 
 * @param table
 *            table with column to pack
 * @param vColIndex
 *            column to pack
 * @param margin
 *            margin to leave at each side of the column (resulting in an
 *            additional width of 2*margin pixels).
 */
public static void packColumn(JTable table, int vColIndex, int margin) {

    DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
    TableColumn col = colModel.getColumn(vColIndex);
    int width = 0;

    // Get width of column header
    TableCellRenderer renderer = col.getHeaderRenderer();
    if (renderer == null) {
        renderer = table.getTableHeader().getDefaultRenderer();
    }
    Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
    width = comp.getPreferredSize().width;

    // because checking the width of each row can be time consuming,
    // only do so if less than 101 rows.
    boolean checkDataWidth = (table.getRowCount() < 101);

    if (checkDataWidth) {
        // Get maximum width from all column data
        for (int r = 0; r < table.getRowCount(); r++) {
            renderer = table.getCellRenderer(r, vColIndex);
            comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false,
                    r, vColIndex);
            width = Math.max(width, comp.getPreferredSize().width);
        }
    }

    // Add margin
    width += 2 * margin;

    // Set the width
    col.setPreferredWidth(width);
}

From source file:trabajoanalisis.grafico.java

public void graficar(JTable a) {
    XYSeries series = new XYSeries("t/n");

    // Introduccion de datos
    int i = 0;/*from  w  ww .jav a 2  s . com*/
    while (a.getValueAt(i, 1) != null) {
        System.out.println(a.getValueAt(i, 1).toString());
        System.out.println(i);
        series.add(Integer.parseInt(a.getValueAt(i, 0).toString()),
                Integer.parseInt(a.getValueAt(i, 1).toString()));
        System.out.println("Se ha agregado " + Integer.parseInt(a.getValueAt(i, 0).toString()) + " y "
                + a.getValueAt(i, 1).toString());
        i++;

    }
    //        series.add(1, 1);
    //        series.add(2, 6);
    //        series.add(3, 3);
    //        series.add(4, 10);

    XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(series);

    JFreeChart chart = ChartFactory.createXYLineChart("Grafica", // Ttulo
            "n", // Etiqueta Coordenada X
            "tiempo", // Etiqueta Coordenada Y
            dataset, // Datos
            PlotOrientation.VERTICAL, true, // Muestra la leyenda de los productos (Producto A)
            false, false);

    // Mostramos la grafica en pantalla
    ChartFrame frame = new ChartFrame("Grafica", chart);
    frame.pack();
    frame.setVisible(true);

}

From source file:Main.java

/**
 * Resizes the columns to match content while keeping the table the same
 * size. This means that the last column may be larger than the content.
 *//*from ww w  .j  a  v  a 2 s. co  m*/
public static void resizeTableColumns(JTable table) {
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    int totalWidth = 0;
    for (int i = 0; i < table.getColumnCount(); i++) {
        DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
        TableColumn col = colModel.getColumn(i);

        if (i == table.getColumnCount() - 1) {
            col.setPreferredWidth(table.getWidth() - totalWidth);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
            return;
        }

        int width = 0;
        TableCellRenderer renderer = col.getHeaderRenderer();
        if (renderer == null) {
            renderer = table.getTableHeader().getDefaultRenderer();
        }
        Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, -1,
                i);
        width = Math.max(width, comp.getPreferredSize().width);
        for (int r = 0; r < table.getRowCount(); r++) {
            renderer = table.getCellRenderer(r, i);
            comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, i), false, false, r, i);
            width = Math.max(width, comp.getPreferredSize().width);
        }
        totalWidth += width + 2;
        col.setPreferredWidth(width + 2);
    }
}

From source file:net.sf.webphotos.util.Util.java

/**
 * PackColumn sets the preferred width of the visible column specified by
 * vColIndex. The column will be just wide enough to show the column head
 * and the widest cell in the column. margin pixels are added to the left
 * and right (resulting in an additional width of 2*margin pixels).
 *
 * @param table The table you want to resize a column.
 * @param vColIndex The column number./*  w ww .  ja v a  2s .  c o m*/
 * @param margin Extra spaces for each side of column.
 */
public static void packColumn(JTable table, int vColIndex, int margin) {
    DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
    TableColumn col = colModel.getColumn(vColIndex);
    int width;

    // Get width of column header
    javax.swing.table.TableCellRenderer renderer = col.getHeaderRenderer();
    if (renderer == null) {
        renderer = table.getTableHeader().getDefaultRenderer();
    }
    java.awt.Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false,
            0, 0);
    width = comp.getPreferredSize().width;

    // Get maximum width of column data
    for (int r = 0; r < table.getRowCount(); r++) {
        renderer = table.getCellRenderer(r, vColIndex);
        comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false, r,
                vColIndex);
        width = Math.max(width, comp.getPreferredSize().width);
    }

    // Add margin
    width += 2 * margin;

    // Set the width
    col.setPreferredWidth(width);
}

From source file:LineChart.java

private static JFreeChart createDataset(JTable table) {
    TimeZone tz = TimeZone.getTimeZone("GMT");

    XYDataset ds = null;/*from   w  ww.j  a  v a2 s  . c o  m*/

    if (table.getColumnCount() > 0) {
        Class klass = table.getColumnClass(0);

        if ((klass == Date.class) || (klass == Time.class) || (klass == c.Month.class)
                || (klass == c.Minute.class) || (klass == c.Second.class) || (klass == Timestamp.class)) {
            TimeSeriesCollection tsc = new TimeSeriesCollection();

            for (int col = 1; col < table.getColumnCount(); col++) {
                TimeSeries series = null;

                try {
                    if (klass == Date.class) {
                        series = new TimeSeries(table.getColumnName(col), Day.class);

                        for (int row = 0; row < table.getRowCount(); row++) {
                            Date date = (Date) table.getValueAt(row, 0);
                            Day day = new Day(date, tz);

                            Object o = table.getValueAt(row, col);
                            if (o instanceof Number) {
                                ((TimeSeries) series).addOrUpdate(day, (Number) table.getValueAt(row, col));
                            }
                        }
                    } else if (klass == Time.class) {
                        series = new TimeSeries(table.getColumnName(col), Millisecond.class);

                        for (int row = 0; row < table.getRowCount(); row++) {
                            Time time = (Time) table.getValueAt(row, 0);
                            Millisecond ms = new Millisecond(time, tz);

                            //    Millisecond ms = new Millisecond(time);
                            Object o = table.getValueAt(row, col);
                            if (o instanceof Number) {
                                series.addOrUpdate(ms, (Number) table.getValueAt(row, col));
                            }
                        }
                    } else if (klass == Timestamp.class) {
                        series = new TimeSeries(table.getColumnName(col), Millisecond.class);

                        for (int row = 0; row < table.getRowCount(); row++) {
                            Timestamp time = (Timestamp) table.getValueAt(row, 0);
                            Millisecond ms = new Millisecond(time, tz);

                            Object o = table.getValueAt(row, col);
                            if (o instanceof Number) {
                                series.addOrUpdate(ms, (Number) table.getValueAt(row, col));
                            }

                        }
                    } else if (klass == c.Month.class) {
                        series = new TimeSeries(table.getColumnName(col), Month.class);

                        for (int row = 0; row < table.getRowCount(); row++) {
                            c.Month time = (c.Month) table.getValueAt(row, 0);
                            int m = time.i + 24000;
                            int y = m / 12;
                            m = 1 + m % 12;

                            Month month = new Month(m, y);

                            Object o = table.getValueAt(row, col);
                            if (o instanceof Number) {
                                series.addOrUpdate(month, (Number) table.getValueAt(row, col));
                            }

                        }
                    } else if (klass == c.Second.class) {
                        series = new TimeSeries(table.getColumnName(col), Second.class);

                        for (int row = 0; row < table.getRowCount(); row++) {
                            c.Second time = (c.Second) table.getValueAt(row, 0);

                            Second second = new Second(time.i % 60, time.i / 60, 0, 1, 1, 2001);

                            Object o = table.getValueAt(row, col);
                            if (o instanceof Number) {
                                series.addOrUpdate(second, (Number) table.getValueAt(row, col));
                            }

                        }
                    } else if (klass == c.Minute.class) {
                        series = new TimeSeries(table.getColumnName(col), Minute.class);

                        for (int row = 0; row < table.getRowCount(); row++) {
                            c.Minute time = (c.Minute) table.getValueAt(row, 0);

                            Minute minute = new Minute(time.i % 60, time.i / 60, 1, 1, 2001);

                            Object o = table.getValueAt(row, col);
                            if (o instanceof Number) {
                                series.addOrUpdate(minute, (Number) table.getValueAt(row, col));
                            }
                        }
                    }
                } catch (SeriesException e) {
                    System.err.println("Error adding to series");
                }

                if (series.getItemCount() > 0)
                    tsc.addSeries(series);
            }

            ds = tsc;
        } else if ((klass == double.class) || (klass == int.class) || (klass == short.class)
                || (klass == long.class) || (klass == Time.class)) {
            XYSeriesCollection xysc = new XYSeriesCollection();

            for (int col = 1; col < table.getColumnCount(); col++) {
                XYSeries series = null;

                try {
                    series = new XYSeries(table.getColumnName(col));

                    for (int row = 0; row < table.getRowCount(); row++) {
                        Number x = (Number) table.getValueAt(row, 0);

                        Object y = table.getValueAt(row, col);
                        if (y instanceof Number) {
                            series.add(x, (Number) y);
                        }
                    }
                } catch (SeriesException e) {
                    System.err.println("Error adding to series");
                }

                if (series.getItemCount() > 0)
                    xysc.addSeries(series);
            }

            ds = xysc;
        }
    }

    if (ds != null) {
        boolean legend = false;

        if (ds.getSeriesCount() > 1) {
            legend = true;
        }

        if (ds instanceof XYSeriesCollection) {
            return ChartFactory.createXYLineChart("", "", "", ds, PlotOrientation.VERTICAL, legend, true, true);
        } else if (ds instanceof TimeSeriesCollection)
            return ChartFactory.createTimeSeriesChart("", "", "", ds, legend, true, true);
    }

    return null;
}

From source file:charts.ErroresPorMilChart.java

private DefaultCategoryDataset createDataset(JTable table) {
    DefaultCategoryDataset result = new DefaultCategoryDataset();

    for (int row = 0; row < table.getRowCount(); row++) {
        String name = (String) table.getValueAt(row, 0);
        double quantity = (double) table.getValueAt(row, 1);
        result.setValue(quantity, name, "");
    }/*from  w ww.j  av  a  2  s.  c  om*/
    return result;
}

From source file:charts.ErrorPorcentajePie.java

private PieDataset creaDataset(JTable table, int total) {
    DefaultPieDataset dataset = new DefaultPieDataset();
    for (int row = 0; row < table.getRowCount(); row++) {
        String name = (String) table.getValueAt(row, 0);
        int quantity = (int) table.getValueAt(row, 1);
        int porcentaje = (quantity * 100) / total;
        dataset.setValue(name, porcentaje);

    }/*from  w  ww .ja v a2  s  .c  om*/
    return dataset;
}

From source file:charts.AceptadosRechazadosPie.java

private PieDataset creaDataset(JTable table, int total) {

    DefaultPieDataset dataset = new DefaultPieDataset();
    for (int row = 0; row < table.getRowCount(); row++) {
        String nombre = (String) table.getValueAt(row, 0);
        int cantidad = (int) table.getValueAt(row, 1);
        int porcentaje = (cantidad * 100 / total);
        dataset.setValue(nombre, porcentaje);
    }//  w  w  w .  j  a  v  a2 s. co m
    return dataset;
}

From source file:charts.TiposErrorChart.java

private DefaultCategoryDataset createDataset(JTable table) {
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    for (int row = 0; row < table.getRowCount(); row++) {
        String name = (String) table.getValueAt(row, 0);
        int quantity = (int) table.getValueAt(row, 1);
        resultadosExcell += "\n" + name + ":" + quantity;
        sumatoriaCantidad += quantity;//from w w w . j a v a  2  s .c o m
        dataset.setValue(quantity, name, "");
    }
    return dataset;
}