Example usage for javax.swing JTable getRowCount

List of usage examples for javax.swing JTable getRowCount

Introduction

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

Prototype

@BeanProperty(bound = false)
public int getRowCount() 

Source Link

Document

Returns the number of rows that can be shown in the JTable, given unlimited space.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object[][] cellData = { { "1-1", "1-2" }, { "2-1", "2-2" } };
    String[] columnNames = { "col1", "col2" };

    JTable table = new JTable(cellData, columnNames);

    int rows = table.getRowCount();
    int cols = table.getColumnCount();

    System.out.println(rows);/*from w  w w  .j  a  v a  2s.  co m*/
    System.out.println(cols);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
    model.addColumn("Col1");
    model.addRow(new Object[] { "r1" });
    model.addRow(new Object[] { "r2" });
    model.addRow(new Object[] { "r3" });

    Vector data = model.getDataVector();
    Vector row = (Vector) data.elementAt(1);

    int mColIndex = 0;
    List colData = new ArrayList(table.getRowCount());
    for (int i = 0; i < table.getRowCount(); i++) {
        row = (Vector) data.elementAt(i);
        colData.add(row.get(mColIndex));
    }/*w  ww  .j  a v  a 2s. c  om*/

    // Append a new column with copied data
    model.addColumn("Col3", colData.toArray());

    JFrame f = new JFrame();
    f.setSize(300, 300);
    f.add(new JScrollPane(table));
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
    model.addColumn("Col1");
    model.addRow(new Object[] { "r1" });
    model.addRow(new Object[] { "r2" });
    model.addRow(new Object[] { "r3" });

    Vector data = model.getDataVector();
    Vector row = (Vector) data.elementAt(1);
    // Copy the first column
    int mColIndex = 0;
    List colData = new ArrayList(table.getRowCount());
    for (int i = 0; i < table.getRowCount(); i++) {
        row = (Vector) data.elementAt(i);
        colData.add(row.get(mColIndex));
    }//from  w w  w  . j a v  a2  s. co m
    JFrame f = new JFrame();
    f.setSize(300, 300);
    f.add(new JScrollPane(table));
    f.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;/*from ww  w .j  a v  a  2  s.c o m*/
            break;
        }
    }

    return resp;
}

From source file:Main.java

/**
 * Returns the visual height of the given table.
 *
 * @param table the table./*from   w w  w .  ja  v  a 2  s  .  c  o  m*/
 *
 * @return the table height.
 */
public static int getTableHeight(JTable table) {
    int result = 0;
    int rowHeight = 0;

    for (int i = 0, rows = table.getRowCount(); i < rows; i++) {
        int height = table.getRowHeight(i);
        result += height;

        if (height > rowHeight) {
            rowHeight = height;
        }
    }

    return result + rowHeight + (table.getRowCount() * table.getRowMargin());
}

From source file:Main.java

/**
 * Packs all table rows to their preferred height.
 *
 * @param table table to process/*from  w ww .  ja  va2 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

/**
 * Auto fit the column of a table.//from   w  ww .  j  a  va 2  s  .  c o  m
 * @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: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;//  ww  w .  ja  v a 2 s. com

    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:OAT.ui.util.UiUtil.java

public static void updateTable(Object value, int rowId, int columnId, JTable table) {
    if (rowId > -1 && rowId < table.getRowCount() && columnId > -1 && columnId < table.getColumnCount()) {
        table.setValueAt(value, rowId, columnId);
    }//from  w  ww. ja va2 s  .co  m
}

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());
    }// w w w  .jav a 2s .c  o 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);

}