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:Main.java

public static void main(String[] argv) throws Exception {
    int rows = 3;
    int cols = 3;
    JTable table = new JTable(rows, cols);

    int rowIndex = 1;
    int vColIndex = 2;
    Object o = table.getValueAt(rowIndex, vColIndex);

}

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JTable table;

    String[] columnTitles = { "A", "B", "C", "D" };
    Object[][] rowData = { { "11", "12", "13", "14" }, { "21", "22", "23", "24" }, { "31", "32", "33", "34" },
            { "41", "42", "44", "44" } };

    table = new JTable(rowData, columnTitles);

    table.setCellSelectionEnabled(true);
    ListSelectionModel cellSelectionModel = table.getSelectionModel();
    cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            String selectedData = null;

            int[] selectedRow = table.getSelectedRows();
            int[] selectedColumns = table.getSelectedColumns();

            for (int i = 0; i < selectedRow.length; i++) {
                for (int j = 0; j < selectedColumns.length; j++) {
                    selectedData = (String) table.getValueAt(selectedRow[i], selectedColumns[j]);
                }/* w ww  .  j av  a 2 s. co m*/
            }
            System.out.println("Selected: " + selectedData);
        }

    });

    frame.add(new JScrollPane(table));

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static boolean containStringInColumn(JTable jTable, int col, String valueBuscado) {

    int num_rows = jTable.getRowCount();
    boolean resp = false;
    //iteramos la tabla
    for (int r = 0; r < num_rows; r++) {
        String valueTabla = "" + jTable.getValueAt(r, col);

        if (valueTabla.equalsIgnoreCase(valueBuscado)) {
            resp = true;/* w ww.ja  v a  2s  .co  m*/
            break;
        }
    }

    return resp;
}

From source file:Main.java

/**
 *
 * @param table/*from w w  w  .  j a  v  a2 s. co m*/
 * @param row
 * @param column
 * @param p
 * @return
 */
public static boolean pointOutsidePrefSize(JTable table, int row, int column, Point p) {
    if ((table.convertColumnIndexToModel(column) != 0) || (row == -1)) {
        return true;
    }

    TableCellRenderer tcr = table.getCellRenderer(row, column);
    Object value = table.getValueAt(row, column);
    Component cell = tcr.getTableCellRendererComponent(table, value, false, false, row, column);
    Dimension itemSize = cell.getPreferredSize();
    Rectangle cellBounds = table.getCellRect(row, column, false);
    cellBounds.width = itemSize.width;
    cellBounds.height = itemSize.height;

    // See if coords are inside
    // ASSUME: mouse x,y will never be < cell's x,y
    assert ((p.x >= cellBounds.x) && (p.y >= cellBounds.y));

    if ((p.x > (cellBounds.x + cellBounds.width)) || (p.y > (cellBounds.y + cellBounds.height))) {
        return true;
    }

    return false;
}

From source file:Main.java

/**
 * Packs all table rows to their preferred height.
 *
 * @param table table to process//from  ww  w.  j  a v  a  2 s.  co m
 */
public static void packRowHeights(final JTable table) {
    for (int row = 0; row < table.getRowCount(); row++) {
        int maxHeight = 0;
        for (int column = 0; column < table.getColumnCount(); column++) {
            final TableCellRenderer cellRenderer = table.getCellRenderer(row, column);
            final Object valueAt = table.getValueAt(row, column);
            final Component renderer = cellRenderer.getTableCellRendererComponent(table, valueAt, false, false,
                    row, column);
            final int heightPreferable = renderer != null ? renderer.getPreferredSize().height : 0;
            maxHeight = Math.max(heightPreferable, maxHeight);
        }
        table.setRowHeight(row, maxHeight);
    }
}

From source file:Main.java

public static void sizeWidthToFitData(JTable table, int vc, int buf) {
    TableColumn tc = table.getColumnModel().getColumn(vc);

    int max = table.getTableHeader().getDefaultRenderer()
            .getTableCellRendererComponent(table, tc.getHeaderValue(), false, false, 0, vc)
            .getPreferredSize().width;/* w w  w .  j a  v  a  2s . c  o m*/

    int vrows = table.getRowCount();
    for (int i = 0; i < vrows; i++) {
        TableCellRenderer r = table.getCellRenderer(i, vc);
        Object value = table.getValueAt(i, vc);
        Component c = r.getTableCellRendererComponent(table, value, false, false, i, vc);
        int w = c.getPreferredSize().width;
        if (max < w) {
            max = w;
        }
    }

    tc.setPreferredWidth(max + buf);
}

From source file:lectorarchivos.VerCSV.java

public static void mostrarGrafica(JTable jTableInfoCSV) {
    //Fuente de datos
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    //Recorremos la columna del consumo de la tabla
    for (int i = jTableInfoCSV.getRowCount() - 1; i >= 0; i--) {
        if (Double.parseDouble(jTableInfoCSV.getValueAt(i, 4).toString()) > 0)
            dataset.setValue(Double.parseDouble(jTableInfoCSV.getValueAt(i, 4).toString()), "Consumo",
                    jTableInfoCSV.getValueAt(i, 0).toString());
    }//from w  w w.j a va  2s. co m

    //Creando el grfico
    JFreeChart chart = ChartFactory.createBarChart3D("Consumo", "Fecha", "Consumo", dataset,
            PlotOrientation.VERTICAL, true, true, false);
    chart.setBackgroundPaint(Color.cyan);
    chart.getTitle().setPaint(Color.black);
    chart.setBackgroundPaint(Color.white);
    chart.removeLegend();

    //Cambiar color de barras
    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    BarRenderer barRenderer = (BarRenderer) plot.getRenderer();
    barRenderer.setSeriesPaint(0, Color.decode("#5882FA"));

    // Mostrar Grafico
    ChartFrame frame = new ChartFrame("CONSUMO", chart);
    frame.pack();
    frame.getChartPanel().setMouseZoomable(false);
    frame.setVisible(true);

    panel.add(frame);

}

From source file:Main.java

/**
 * Auto fit the column of a table.//  w  w w .  j  a  v a 2 s  .c  om
 * @param table the table for which to auto fit the columns.
 * @param columnIndex the index of the column to auto fit.
 * @param maxWidth the maximum width that a column can take (like Integer.MAX_WIDTH).
 */
public static void autoFitTableColumn(JTable table, int columnIndex, int maxWidth) {
    TableModel model = table.getModel();
    TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();
    int rowCount = table.getRowCount();
    for (int i = columnIndex >= 0 ? columnIndex : model.getColumnCount() - 1; i >= 0; i--) {
        TableColumn column = table.getColumnModel().getColumn(i);
        int headerWidth = headerRenderer
                .getTableCellRendererComponent(table, column.getHeaderValue(), false, false, 0, 0)
                .getPreferredSize().width;
        int cellWidth = 0;
        for (int j = 0; j < rowCount; j++) {
            Component comp = table.getDefaultRenderer(model.getColumnClass(i))
                    .getTableCellRendererComponent(table, table.getValueAt(j, i), false, false, 0, i);
            int preferredWidth = comp.getPreferredSize().width;
            // Artificial space to look nicer.
            preferredWidth += 10;
            cellWidth = Math.max(cellWidth, preferredWidth);
        }
        // Artificial space for the sort icon.
        headerWidth += 20;
        column.setPreferredWidth(Math.min(Math.max(headerWidth, cellWidth) + table.getRowMargin(), maxWidth));
        if (columnIndex >= 0) {
            break;
        }
    }
}

From source file:funcoes.funcoes.java

public static String calculaTotalFormatoDinheiro(JTable tabela, int coluna, String tipo) throws ParseException {
    double valorTotal = 0.0;
    double valorAdd = 0.0;
    String retorno = null;/*from   w w  w. j  a  va2  s. c om*/

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

        valorAdd = Double.parseDouble(funcoes.formatoParaInserir(tabela.getValueAt(a - 1, coluna).toString()
                .substring(3, tabela.getValueAt(a - 1, coluna).toString().length())));
        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);

                }
            }

        }

    }
    return retorno;

}

From source file:funcoes.funcoes.java

public static double calculaTotalMedia(JTable tabela, int coluna, String tipo, int denominador)
        throws ParseException {
    double valorTotal = 0.0;
    double valorAdd = 0.0;

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

        try {// www.ja v  a2s.  co m
            valorAdd = Double
                    .parseDouble(funcoes.formatoParaInserir(tabela.getValueAt(a - 1, coluna).toString()));
            if (valorAdd < 0.0 && tipo.equalsIgnoreCase("-")) {

                valorTotal = valorTotal + valorAdd;

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

                    valorTotal = valorTotal + valorAdd;

                } else {
                    if (tipo.equalsIgnoreCase("0")) {
                        valorTotal = valorTotal + valorAdd;

                    }
                }

            }

        } catch (NullPointerException ex) {
        }
    }
    return valorTotal / denominador;

}