Example usage for javax.swing JTable getModel

List of usage examples for javax.swing JTable getModel

Introduction

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

Prototype

public TableModel getModel() 

Source Link

Document

Returns the TableModel that provides the data displayed by this JTable .

Usage

From source file:invoice.GetInvoice.java

/**
 * @param args the command line arguments
 *///from ww  w.j  av  a 2 s  . co  m
public static void main(String[] args) {

    NumberFormat nf = NumberFormat.getInstance();
    nf.setGroupingUsed(false);
    nf.setMaximumFractionDigits(2);
    nf.setMinimumFractionDigits(2);
    nf.setRoundingMode(RoundingMode.HALF_UP);

    try {
        JSONObject arg_json = new JSONObject(args[0]);
    } catch (JSONException ex) {
        Logger.getLogger(GetInvoice.class.getName()).log(Level.SEVERE, null, ex);
    }
    HashMap<String, Object> hm = new HashMap<>();
    hm.put("duplicate", "");
    hm.put("distributor", "//oshan" + "\n" + "//kapuhempala" + "\n\nArea: " + "//galle");
    hm.put("customer", "//owner" + "\n" + "//Agro" + "\n" + "//Agro add" + "\n" + "//0771894851");
    hm.put("invNo", "GSLTS" + String.format("%04d", Integer.parseInt("//100")));
    hm.put("invDate", "2014-01-10");
    hm.put("invCode", "300");

    double invoiceTotal = 500000;
    if (5 > 0) {//ShopDiscount
        double discountprice = (invoiceTotal * 99) / 100;//getShopDiscount()
        hm.put("invoiceDiscount", nf.format((invoiceTotal) * 99 / 100));//getRetail_discount()

    } else {
        hm.put("invoiceDiscount", "");
    }
    hm.put("gross_total", nf.format(invoiceTotal));
    hm.put("invoiceTotal", nf.format(((invoiceTotal) * (100 - 99) / 100)));//getRetail_discount()
    hm.put("salesPersonName", "rep");
    hm.put("salesPersonContactNo", "0772189584");

    JTable jTable1 = new JTable();
    jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {},
            new String[] { "ITEMCODE", "DESCRIPTION", "QTY", "FREEQTY", "PRICE", "AMOUNT" }));
    String reportSource = "./ireports/invoice.jrxml";
    DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
    try {
        JasperReport jr = JasperCompileManager.compileReport(reportSource);
        JasperPrint jp = JasperFillManager.fillReport(jr, hm, new JRTableModelDataSource(dtm));
        JasperPrintManager.printReport(jp, false);
    } catch (JRException ex) {
        Logger.getLogger(GetInvoice.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println("1");
}

From source file:Main.java

public static void table_clean(JTable table) {
    DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
    tableModel.setRowCount(0);//from  ww w  .ja  v  a  2  s.co m
}

From source file:Main.java

public static void clearAllRows(JTable table) {
    DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
    int rowCount = tableModel.getRowCount();
    for (int i = 0; i < rowCount; i++) {
        tableModel.removeRow(rowCount - i - 1);
    }/*w ww  . ja  v a 2s. co m*/
}

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);
    }//w w w  .  j a  va  2  s.c  om
    return rowData;
}

From source file:Main.java

public static void removeRow(JTable table, int row) {
    DefaultTableModel model = (DefaultTableModel) table.getModel();
    model.removeRow(row);//from   ww w . j a  v a 2 s  . c  om
}

From source file:Main.java

/**
 * Adjusts the widths and heights of the cells of the supplied table to fit their contents.
 *//* w w w  . j a va 2  s  . c om*/
public static void sizeToContents(JTable table) {
    TableModel model = table.getModel();
    TableColumn column = null;
    Component comp = null;
    int ccount = table.getColumnModel().getColumnCount(), rcount = model.getRowCount(), cellHeight = 0;

    for (int cc = 0; cc < ccount; cc++) {
        int headerWidth = 0, cellWidth = 0;
        column = table.getColumnModel().getColumn(cc);
        try {
            comp = column.getHeaderRenderer().getTableCellRendererComponent(null, column.getHeaderValue(),
                    false, false, 0, 0);
            headerWidth = comp.getPreferredSize().width;
        } catch (NullPointerException e) {
            // getHeaderRenderer() this doesn't work in 1.3
        }

        for (int rr = 0; rr < rcount; rr++) {
            Object cellValue = model.getValueAt(rr, cc);
            comp = table.getDefaultRenderer(model.getColumnClass(cc)).getTableCellRendererComponent(table,
                    cellValue, false, false, 0, cc);
            Dimension psize = comp.getPreferredSize();
            cellWidth = Math.max(psize.width, cellWidth);
            cellHeight = Math.max(psize.height, cellHeight);
        }
        column.setPreferredWidth(Math.max(headerWidth, cellWidth));
    }

    if (cellHeight > 0) {
        table.setRowHeight(cellHeight);
    }
}

From source file:Main.java

public static Object[] getValueBySelectedRow(JTable table, int rows[], int col) {
    DefaultTableModel model = (DefaultTableModel) table.getModel();
    Object[] ret = new Object[rows.length];
    for (int i = 0; i < rows.length; i++)
        ret[i] = model.getValueAt(rows[i], col);
    return ret;/*w  w  w .  j  a v a2 s  .c o m*/
}

From source file:Main.java

public static void setValueAt(JTable table, Object element, int row, int col) {
    DefaultTableModel model = (DefaultTableModel) table.getModel();
    model.setValueAt(element, row, col);
}

From source file:Main.java

/**
 * Auto fit the column of a table.//  w w  w . j av a 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:au.com.jwatmuff.eventmanager.export.CSVExporter.java

public static void generateFromTable(JTable t, OutputStream out) {
    generateFromTable(t.getModel(), out);
}