Example usage for javax.swing.table DefaultTableModel getColumnCount

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

Introduction

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

Prototype

public int getColumnCount() 

Source Link

Document

Returns the number of columns in this data table.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);

    model = (DefaultTableModel) table.getModel();
    TableColumn col = new TableColumn(model.getColumnCount());

    // Ensure that auto-create is off
    if (table.getAutoCreateColumnsFromModel()) {
        throw new IllegalStateException();
    }/*from ww  w. j  av  a2  s .c  o m*/
    col.setHeaderValue("Col3");
    table.addColumn(col);
    model.addColumn("Col3", new Object[] { "v3" });

}

From source file:Main.java

public static void main(String[] args) {
    int maximum = 100;
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Integer[] oneRow = { 0, 0, 0, 0 };
    String[] headers = { "A", "B", "C", "D" };
    Integer[][] data = { oneRow, oneRow, oneRow, oneRow, oneRow, };
    DefaultTableModel model = new DefaultTableModel(data, headers);
    JTable table = new JTable(model);
    table.setDefaultRenderer(Object.class, new ProgressRenderer(0, maximum));
    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    frame.add(new JScrollPane(table));
    frame.pack();/*from   w  w  w.  ja v a2  s .c o  m*/
    frame.setVisible(true);
    new Thread(new Runnable() {
        @Override
        public void run() {
            Object waiter = new Object();
            synchronized (waiter) {
                int rows = model.getRowCount();
                int columns = model.getColumnCount();
                Random random = new Random(System.currentTimeMillis());
                boolean done = false;
                while (!done) {
                    int row = random.nextInt(rows);
                    int column = random.nextInt(columns);
                    Integer value = (Integer) model.getValueAt(row, column);
                    value++;
                    if (value <= maximum) {
                        model.setValueAt(value, row, column);
                        try {
                            waiter.wait(15);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    done = true;
                    for (row = 0; row < rows; row++) {
                        for (column = 0; column < columns; column++) {
                            if (!model.getValueAt(row, column).equals(maximum)) {
                                done = false;
                                break;
                            }
                        }
                        if (!done) {
                            break;
                        }
                    }
                }
            }
        }
    }).start();
}

From source file:Main.java

public static Object[] getData(int index, final JTable table) {
    DefaultTableModel model = (DefaultTableModel) table.getModel();
    Object[] rowData = new Object[model.getColumnCount()];
    for (int i = 0; i < rowData.length; i++) {
        rowData[i] = model.getValueAt(index, i);
    }// ww  w  .ja v  a  2s .  com
    return rowData;
}

From source file:com.emr.mappings.MappingsForm.java

/**
 * Method to get the contents of a {@link JTable} as an array
 * @param table {@link JTable} The table to be parsed
 * @return {@link Object[][]} A multidimensional array 
 *//*from   w  w  w.  j  ava  2  s .c  o m*/
public Object[][] getTableData(JTable table) {
    DefaultTableModel dtm = (DefaultTableModel) table.getModel();
    int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();
    Object[][] tableData = new Object[nRow][nCol];
    for (int i = 0; i < nRow; i++)
        for (int j = 0; j < nCol; j++) {

            if (dtm.getValueAt(i, j) != null) {
                tableData[i][j] = dtm.getValueAt(i, j);
            }
        }
    return tableData;
}

From source file:Software_Jframes.chart.java

void print_excel_report(JTable jTable5) {
    Access ac = new Access();
    try {/*from   w  w  w  . ja v a 2  s . c o  m*/
        String filename = ac.chooseFile();

        WritableWorkbook wworkbook;
        wworkbook = Workbook.createWorkbook(new File(filename + ".xls"));
        WritableSheet wsheet = wworkbook.createSheet("First Sheet", 0);

        DefaultTableModel dtm = (DefaultTableModel) jTable5.getModel();
        Vector v = new Vector();
        int count_row = dtm.getRowCount();
        int count_col = dtm.getColumnCount();

        jxl.write.Label label1 = new jxl.write.Label(0, 0, "Good Name");
        wsheet.addCell(label1);
        jxl.write.Label label2 = new jxl.write.Label(1, 0, "Project Name");
        wsheet.addCell(label2);
        jxl.write.Label label3 = new jxl.write.Label(2, 0, "Cost");
        wsheet.addCell(label3);

        int i = 0;
        int j = 0;

        for (i = 0; i < count_row; i++) {
            for (j = 0; j < count_col; j++) {
                int temp_i = i + 2;
                jxl.write.Label label = new jxl.write.Label(j, temp_i, dtm.getValueAt(i, j) + "");
                wsheet.addCell(label);
            }
        }
        wworkbook.write();
        wworkbook.close();
        JOptionPane.showMessageDialog(null, "Successfully Create a excel file");

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Software_Jframes.chart.java

void print_pdf_report(JTable jTable5) {
    Access ac = new Access();
    try {//  w ww  .  ja  va 2  s  .  co m
        String filename = ac.chooseFile();

        com.itextpdf.text.Document document = new com.itextpdf.text.Document();
        PdfWriter.getInstance(document, new FileOutputStream(filename + ".pdf"));

        document.open();
        document.add(new Paragraph(""));

        Image image1 = Image.getInstance("src/images/ROPA_Logo_without_claim.jpg");
        document.add(new Paragraph(""));
        image1.scaleAbsolute(80, 50);
        image1.setAbsolutePosition(50, 800);

        document.add(image1);
        PdfPTable table = new PdfPTable(3); // 3 columns.

        DefaultTableModel dtm = (DefaultTableModel) jTable5.getModel();
        Vector v = new Vector();
        int count_row = dtm.getRowCount();
        int count_col = dtm.getColumnCount();

        for (int i = 0; i < count_row; i++) {
            for (int j = 0; j < count_col; j++) {

                table.addCell(new PdfPCell(new Paragraph(dtm.getValueAt(i, j) + "")));

            }
        }
        table.setWidthPercentage(100);
        float[] columnWidths = { 2f, 1f, 1f };

        table.setWidths(columnWidths);

        document.add(table);
        document.close();
        JOptionPane.showMessageDialog(null, "Successfully Created.");

    } catch (Exception e) {
        e.printStackTrace();
    }
}

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 av a 2 s. co  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: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;//from w ww . ja v a  2 s  .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

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;//  www.  ja  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;/* w  w w  .jav  a 2 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);
}