Example usage for javax.swing JTable setModel

List of usage examples for javax.swing JTable setModel

Introduction

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

Prototype

@BeanProperty(description = "The model that is the source of the data for this view.")
public void setModel(final TableModel dataModel) 

Source Link

Document

Sets the data model for this table to dataModel and registers with it for listener notifications from the new data model.

Usage

From source file:Main.java

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

    model.addColumn("Col1");
    model.addColumn("Col2");
    model.addColumn("Col3");
    model.addRow(new Object[] { "v1" });

    table.removeColumn(table.getColumnModel().getColumn(0));

}

From source file:invoice.GetInvoice.java

/**
 * @param args the command line arguments
 *///from  w  w  w .  j a v  a2 s . c om
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 main(String[] args) {
    Object[][] rowData = { { "Hello", "World" } };
    Object[] columnNames = { "A", "B" };

    JTable table;
    DefaultTableModel model;/*from  w  w w .ja  va  2s.  com*/

    model = new DefaultTableModel(rowData, columnNames);
    table = new JTable();
    table.setModel(model);
    JButton add = new JButton("Add");

    add.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            model.addRow(rowData[0]);
        }
    });
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container c = f.getContentPane();
    c.setLayout(new BorderLayout());
    c.add(add, BorderLayout.SOUTH);
    c.add(new JScrollPane(table), BorderLayout.CENTER);

    f.pack();

    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:de.codesourcery.eve.skills.ui.spreadsheet.SpreadSheetTableModel.java

public static void main(String[] args) {

    final ITableFactory cellFactory = new ITableFactory() {

        @Override/*from   w  w  w. j  a v  a  2  s  . co m*/
        public ITableCell createEmptyCell(int row, int column) {
            return new SimpleCell();
        }

        @Override
        public TableRow createRow(SpreadSheetTableModel tableModel) {
            return new TableRow(tableModel);
        }

    };

    final SpreadSheetTableModel model = new SpreadSheetTableModel(cellFactory);

    final JTable table = new JTable(model);

    table.setFillsViewportHeight(true);

    final JFrame frame = new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //      table.setPreferredSize( new Dimension(400,200 ) );
    table.setBorder(BorderFactory.createLineBorder(Color.black));

    frame.getContentPane().add(new JScrollPane(table));

    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            frame.pack();
            frame.setLocationRelativeTo(null);

            frame.setVisible(true);

            model.addRow(new SimpleCell("First row") {
                public boolean isEditable() {
                    return true;
                }

                public void setValue(Object value) {
                    System.out.println("new value: " + value);
                }
            });
            model.addRow(new SimpleCell("Second row #1"), new SimpleCell("Second row #2"));
            model.addRow(new SimpleCell("Third row #1"), new SimpleCell("Third row #2"),
                    new SimpleCell("Third row #3"));

            JTextField tf = new JTextField();

            table.setModel(model);
            table.setDefaultEditor(ITableCell.class, new DefaultCellEditor(tf));
        }
    });

}

From source file:org.eclipse.swt.snippets.Snippet135.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("SWT and Swing/AWT Example");

    Listener exitListener = e -> {//from  w ww . j a va2s .co  m
        MessageBox dialog = new MessageBox(shell, SWT.OK | SWT.CANCEL | SWT.ICON_QUESTION);
        dialog.setText("Question");
        dialog.setMessage("Exit?");
        if (e.type == SWT.Close)
            e.doit = false;
        if (dialog.open() != SWT.OK)
            return;
        shell.dispose();
    };
    Listener aboutListener = e -> {
        final Shell s = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
        s.setText("About");
        GridLayout layout = new GridLayout(1, false);
        layout.verticalSpacing = 20;
        layout.marginHeight = layout.marginWidth = 10;
        s.setLayout(layout);
        Label label = new Label(s, SWT.NONE);
        label.setText("SWT and AWT Example.");
        Button button = new Button(s, SWT.PUSH);
        button.setText("OK");
        GridData data = new GridData();
        data.horizontalAlignment = GridData.CENTER;
        button.setLayoutData(data);
        button.addListener(SWT.Selection, event -> s.dispose());
        s.pack();
        Rectangle parentBounds = shell.getBounds();
        Rectangle bounds = s.getBounds();
        int x = parentBounds.x + (parentBounds.width - bounds.width) / 2;
        int y = parentBounds.y + (parentBounds.height - bounds.height) / 2;
        s.setLocation(x, y);
        s.open();
        while (!s.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    };
    shell.addListener(SWT.Close, exitListener);
    Menu mb = new Menu(shell, SWT.BAR);
    MenuItem fileItem = new MenuItem(mb, SWT.CASCADE);
    fileItem.setText("&File");
    Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
    fileItem.setMenu(fileMenu);
    MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH);
    exitItem.setText("&Exit\tCtrl+X");
    exitItem.setAccelerator(SWT.CONTROL + 'X');
    exitItem.addListener(SWT.Selection, exitListener);
    MenuItem aboutItem = new MenuItem(fileMenu, SWT.PUSH);
    aboutItem.setText("&About\tCtrl+A");
    aboutItem.setAccelerator(SWT.CONTROL + 'A');
    aboutItem.addListener(SWT.Selection, aboutListener);
    shell.setMenuBar(mb);

    RGB color = shell.getBackground().getRGB();
    Label separator1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    Label locationLb = new Label(shell, SWT.NONE);
    locationLb.setText("Location:");
    Composite locationComp = new Composite(shell, SWT.EMBEDDED);
    ToolBar toolBar = new ToolBar(shell, SWT.FLAT);
    ToolItem exitToolItem = new ToolItem(toolBar, SWT.PUSH);
    exitToolItem.setText("&Exit");
    exitToolItem.addListener(SWT.Selection, exitListener);
    ToolItem aboutToolItem = new ToolItem(toolBar, SWT.PUSH);
    aboutToolItem.setText("&About");
    aboutToolItem.addListener(SWT.Selection, aboutListener);
    Label separator2 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    final Composite comp = new Composite(shell, SWT.NONE);
    final Tree fileTree = new Tree(comp, SWT.SINGLE | SWT.BORDER);
    Sash sash = new Sash(comp, SWT.VERTICAL);
    Composite tableComp = new Composite(comp, SWT.EMBEDDED);
    Label separator3 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    Composite statusComp = new Composite(shell, SWT.EMBEDDED);

    java.awt.Frame locationFrame = SWT_AWT.new_Frame(locationComp);
    final java.awt.TextField locationText = new java.awt.TextField();
    locationFrame.add(locationText);

    java.awt.Frame fileTableFrame = SWT_AWT.new_Frame(tableComp);
    java.awt.Panel panel = new java.awt.Panel(new java.awt.BorderLayout());
    fileTableFrame.add(panel);
    final JTable fileTable = new JTable(new FileTableModel(null));
    fileTable.setDoubleBuffered(true);
    fileTable.setShowGrid(false);
    fileTable.createDefaultColumnsFromModel();
    JScrollPane scrollPane = new JScrollPane(fileTable);
    panel.add(scrollPane);

    java.awt.Frame statusFrame = SWT_AWT.new_Frame(statusComp);
    statusFrame.setBackground(new java.awt.Color(color.red, color.green, color.blue));
    final java.awt.Label statusLabel = new java.awt.Label();
    statusFrame.add(statusLabel);
    statusLabel.setText("Select a file");

    sash.addListener(SWT.Selection, e -> {
        if (e.detail == SWT.DRAG)
            return;
        GridData data = (GridData) fileTree.getLayoutData();
        Rectangle trim = fileTree.computeTrim(0, 0, 0, 0);
        data.widthHint = e.x - trim.width;
        comp.layout();
    });

    File[] roots = File.listRoots();
    for (int i = 0; i < roots.length; i++) {
        File file = roots[i];
        TreeItem treeItem = new TreeItem(fileTree, SWT.NONE);
        treeItem.setText(file.getAbsolutePath());
        treeItem.setData(file);
        new TreeItem(treeItem, SWT.NONE);
    }
    fileTree.addListener(SWT.Expand, e -> {
        TreeItem item = (TreeItem) e.item;
        if (item == null)
            return;
        if (item.getItemCount() == 1) {
            TreeItem firstItem = item.getItems()[0];
            if (firstItem.getData() != null)
                return;
            firstItem.dispose();
        } else {
            return;
        }
        File root = (File) item.getData();
        File[] files = root.listFiles();
        if (files == null)
            return;
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if (file.isDirectory()) {
                TreeItem treeItem = new TreeItem(item, SWT.NONE);
                treeItem.setText(file.getName());
                treeItem.setData(file);
                new TreeItem(treeItem, SWT.NONE);
            }
        }
    });
    fileTree.addListener(SWT.Selection, e -> {
        TreeItem item = (TreeItem) e.item;
        if (item == null)
            return;
        final File root = (File) item.getData();
        EventQueue.invokeLater(() -> {
            statusLabel.setText(root.getAbsolutePath());
            locationText.setText(root.getAbsolutePath());
            fileTable.setModel(new FileTableModel(root.listFiles()));
        });
    });

    GridLayout layout = new GridLayout(4, false);
    layout.marginWidth = layout.marginHeight = 0;
    layout.horizontalSpacing = layout.verticalSpacing = 1;
    shell.setLayout(layout);
    GridData data;
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator1.setLayoutData(data);
    data = new GridData();
    data.horizontalSpan = 1;
    data.horizontalIndent = 10;
    locationLb.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 2;
    data.heightHint = locationText.getPreferredSize().height;
    locationComp.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 1;
    toolBar.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator2.setLayoutData(data);
    data = new GridData(GridData.FILL_BOTH);
    data.horizontalSpan = 4;
    comp.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator3.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    data.heightHint = statusLabel.getPreferredSize().height;
    statusComp.setLayoutData(data);

    layout = new GridLayout(3, false);
    layout.marginWidth = layout.marginHeight = 0;
    layout.horizontalSpacing = layout.verticalSpacing = 1;
    comp.setLayout(layout);
    data = new GridData(GridData.FILL_VERTICAL);
    data.widthHint = 200;
    fileTree.setLayoutData(data);
    data = new GridData(GridData.FILL_VERTICAL);
    sash.setLayoutData(data);
    data = new GridData(GridData.FILL_BOTH);
    tableComp.setLayoutData(data);

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Snippet135.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("SWT and Swing/AWT Example");

    Listener exitListener = new Listener() {
        public void handleEvent(Event e) {
            MessageBox dialog = new MessageBox(shell, SWT.OK | SWT.CANCEL | SWT.ICON_QUESTION);
            dialog.setText("Question");
            dialog.setMessage("Exit?");
            if (e.type == SWT.Close)
                e.doit = false;/*from   w  w w  .j a v a 2s.c  om*/
            if (dialog.open() != SWT.OK)
                return;
            shell.dispose();
        }
    };
    Listener aboutListener = new Listener() {
        public void handleEvent(Event e) {
            final Shell s = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
            s.setText("About");
            GridLayout layout = new GridLayout(1, false);
            layout.verticalSpacing = 20;
            layout.marginHeight = layout.marginWidth = 10;
            s.setLayout(layout);
            Label label = new Label(s, SWT.NONE);
            label.setText("SWT and AWT Example.");
            Button button = new Button(s, SWT.PUSH);
            button.setText("OK");
            GridData data = new GridData();
            data.horizontalAlignment = GridData.CENTER;
            button.setLayoutData(data);
            button.addListener(SWT.Selection, new Listener() {
                public void handleEvent(Event event) {
                    s.dispose();
                }
            });
            s.pack();
            Rectangle parentBounds = shell.getBounds();
            Rectangle bounds = s.getBounds();
            int x = parentBounds.x + (parentBounds.width - bounds.width) / 2;
            int y = parentBounds.y + (parentBounds.height - bounds.height) / 2;
            s.setLocation(x, y);
            s.open();
            while (!s.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
        }
    };
    shell.addListener(SWT.Close, exitListener);
    Menu mb = new Menu(shell, SWT.BAR);
    MenuItem fileItem = new MenuItem(mb, SWT.CASCADE);
    fileItem.setText("&File");
    Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
    fileItem.setMenu(fileMenu);
    MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH);
    exitItem.setText("&Exit\tCtrl+X");
    exitItem.setAccelerator(SWT.CONTROL + 'X');
    exitItem.addListener(SWT.Selection, exitListener);
    MenuItem aboutItem = new MenuItem(fileMenu, SWT.PUSH);
    aboutItem.setText("&About\tCtrl+A");
    aboutItem.setAccelerator(SWT.CONTROL + 'A');
    aboutItem.addListener(SWT.Selection, aboutListener);
    shell.setMenuBar(mb);

    RGB color = shell.getBackground().getRGB();
    Label separator1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    Label locationLb = new Label(shell, SWT.NONE);
    locationLb.setText("Location:");
    Composite locationComp = new Composite(shell, SWT.EMBEDDED);
    ToolBar toolBar = new ToolBar(shell, SWT.FLAT);
    ToolItem exitToolItem = new ToolItem(toolBar, SWT.PUSH);
    exitToolItem.setText("&Exit");
    exitToolItem.addListener(SWT.Selection, exitListener);
    ToolItem aboutToolItem = new ToolItem(toolBar, SWT.PUSH);
    aboutToolItem.setText("&About");
    aboutToolItem.addListener(SWT.Selection, aboutListener);
    Label separator2 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    final Composite comp = new Composite(shell, SWT.NONE);
    final Tree fileTree = new Tree(comp, SWT.SINGLE | SWT.BORDER);
    Sash sash = new Sash(comp, SWT.VERTICAL);
    Composite tableComp = new Composite(comp, SWT.EMBEDDED);
    Label separator3 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
    Composite statusComp = new Composite(shell, SWT.EMBEDDED);

    java.awt.Frame locationFrame = SWT_AWT.new_Frame(locationComp);
    final java.awt.TextField locationText = new java.awt.TextField();
    locationFrame.add(locationText);

    java.awt.Frame fileTableFrame = SWT_AWT.new_Frame(tableComp);
    java.awt.Panel panel = new java.awt.Panel(new java.awt.BorderLayout());
    fileTableFrame.add(panel);
    final JTable fileTable = new JTable(new FileTableModel(null));
    fileTable.setDoubleBuffered(true);
    fileTable.setShowGrid(false);
    fileTable.createDefaultColumnsFromModel();
    JScrollPane scrollPane = new JScrollPane(fileTable);
    panel.add(scrollPane);

    java.awt.Frame statusFrame = SWT_AWT.new_Frame(statusComp);
    statusFrame.setBackground(new java.awt.Color(color.red, color.green, color.blue));
    final java.awt.Label statusLabel = new java.awt.Label();
    statusFrame.add(statusLabel);
    statusLabel.setText("Select a file");

    sash.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            if (e.detail == SWT.DRAG)
                return;
            GridData data = (GridData) fileTree.getLayoutData();
            Rectangle trim = fileTree.computeTrim(0, 0, 0, 0);
            data.widthHint = e.x - trim.width;
            comp.layout();
        }
    });

    File[] roots = File.listRoots();
    for (int i = 0; i < roots.length; i++) {
        File file = roots[i];
        TreeItem treeItem = new TreeItem(fileTree, SWT.NONE);
        treeItem.setText(file.getAbsolutePath());
        treeItem.setData(file);
        new TreeItem(treeItem, SWT.NONE);
    }
    fileTree.addListener(SWT.Expand, new Listener() {
        public void handleEvent(Event e) {
            TreeItem item = (TreeItem) e.item;
            if (item == null)
                return;
            if (item.getItemCount() == 1) {
                TreeItem firstItem = item.getItems()[0];
                if (firstItem.getData() != null)
                    return;
                firstItem.dispose();
            } else {
                return;
            }
            File root = (File) item.getData();
            File[] files = root.listFiles();
            if (files == null)
                return;
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                if (file.isDirectory()) {
                    TreeItem treeItem = new TreeItem(item, SWT.NONE);
                    treeItem.setText(file.getName());
                    treeItem.setData(file);
                    new TreeItem(treeItem, SWT.NONE);
                }
            }
        }
    });
    fileTree.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event e) {
            TreeItem item = (TreeItem) e.item;
            if (item == null)
                return;
            final File root = (File) item.getData();
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    statusLabel.setText(root.getAbsolutePath());
                    locationText.setText(root.getAbsolutePath());
                    fileTable.setModel(new FileTableModel(root.listFiles()));
                }
            });
        }
    });

    GridLayout layout = new GridLayout(4, false);
    layout.marginWidth = layout.marginHeight = 0;
    layout.horizontalSpacing = layout.verticalSpacing = 1;
    shell.setLayout(layout);
    GridData data;
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator1.setLayoutData(data);
    data = new GridData();
    data.horizontalSpan = 1;
    data.horizontalIndent = 10;
    locationLb.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 2;
    data.heightHint = locationText.getPreferredSize().height;
    locationComp.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 1;
    toolBar.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator2.setLayoutData(data);
    data = new GridData(GridData.FILL_BOTH);
    data.horizontalSpan = 4;
    comp.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    separator3.setLayoutData(data);
    data = new GridData(GridData.FILL_HORIZONTAL);
    data.horizontalSpan = 4;
    data.heightHint = statusLabel.getPreferredSize().height;
    statusComp.setLayoutData(data);

    layout = new GridLayout(3, false);
    layout.marginWidth = layout.marginHeight = 0;
    layout.horizontalSpacing = layout.verticalSpacing = 1;
    comp.setLayout(layout);
    data = new GridData(GridData.FILL_VERTICAL);
    data.widthHint = 200;
    fileTree.setLayoutData(data);
    data = new GridData(GridData.FILL_VERTICAL);
    sash.setLayoutData(data);
    data = new GridData(GridData.FILL_BOTH);
    tableComp.setLayoutData(data);

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Main.java

@SuppressWarnings("serial")
public static void setupTable(JTable jTable, TableCellRenderer tableCellRenderer, String[][] content) {

    jTable.setModel(new DefaultTableModel(content, content[0]) {
        @Override//from  w  w w. jav a2  s. co  m
        public boolean isCellEditable(int i, int j) {
            return false;
        }
    });

    for (int j = 0; j < jTable.getColumnModel().getColumnCount(); j++) {
        jTable.getColumnModel().getColumn(j).setCellRenderer(tableCellRenderer);
    }
    jTable.doLayout();
}

From source file:Main.java

public static void LimpaTabela(JTable tabela) {
    DefaultTableModel apaga = new DefaultTableModel(new String[0][0], new String[0]);
    apaga.setRowCount(0);/*from w  w w  .  java2s . co m*/
    tabela.setModel(apaga);
}

From source file:simulation.AureoZauleckAnsLab2.java

public static void DisplayTable(ArrayList<ArrayList> all, ArrayList percentages, String title) {
    JFrame frame = new JFrame();
    //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                        
    JTable table = new JTable();
    table.setModel(new DefaultTableModel((int) (all.size() + 2), 2));

    table.setValueAt("VALUE LABELS", 0, 0);
    table.setValueAt("PERCENTAGE", 0, 1);

    table.setValueAt("TOTAL = 100%", (int) (all.size() + 1), 1);

    for (int i = 0; i < all.size(); i++) {
        table.setValueAt(all.get(i).get(0), i + 1, 0);

        table.setValueAt(new DecimalFormat("#.##").format(percentages.get(i)), i + 1, 1);

    }//from   ww w  .jav a2s.  c o  m

    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setBorder(BorderFactory.createTitledBorder(title));
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:simulation.AureoZauleckAnsLab2.java

public static void DisplayTables(double k, ArrayList<Double> cl, ArrayList<Double> cl2, ArrayList<Double> tlcl,
        ArrayList<Double> tucl, ArrayList<Double> midList, ArrayList<ArrayList<Double>> freq,
        ArrayList<Double> freqPercent, ArrayList<Double> cfs, ArrayList<Double> cps, String title) {

    JFrame frame = new JFrame();
    //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                        
    JTable table = new JTable();
    table.setModel(new DefaultTableModel((int) (k + 2), 7));

    table.setValueAt("CLASS LIMITS", 0, 0);
    table.setValueAt("TRUE CLASS LIMITS", 0, 1);
    table.setValueAt("MIDPOINTS", 0, 2);
    table.setValueAt("FREQUENCY", 0, 3);
    table.setValueAt("%", 0, 4);
    table.setValueAt("CF", 0, 5);
    table.setValueAt("C%", 0, 6);
    table.setValueAt("n = " + N, (int) (k + 1), 3);
    table.setValueAt("TOTAL = 100%", (int) (k + 1), 4);

    for (int i = 0; i < k; i++) {
        table.setValueAt(cl.get(i) + " - " + cl2.get(i), i + 1, 0);
        table.setValueAt(tlcl.get(i) + " - " + tucl.get(i), i + 1, 1);
        table.setValueAt(midList.get(i), i + 1, 2);
        table.setValueAt(freq.get(i).size(), i + 1, 3);
        table.setValueAt(new DecimalFormat("#.##").format(freqPercent.get(i)), i + 1, 4);
        table.setValueAt(cfs.get(i), i + 1, 5);
        table.setValueAt(new DecimalFormat("#.##").format(cps.get(i)), i + 1, 6);
    }/*from w  w w .  j  av a  2  s .c o m*/

    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setBorder(BorderFactory.createTitledBorder(title));
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);

    Scanner sc = new Scanner(System.in);

    int type = 0, testT = 0;
    String typeTest = "";
    do {

        System.out.println();
        System.out.println("COLLAPSE CLASS LIMITS?");
        System.out.println("[1] COLLAPSE LOWER CLASS LIMIT");
        System.out.println("[2] COLLAPSE UPPPER CLASS LIMIT");
        System.out.println("[3] COLLAPSE BOTH");
        System.out.println("[4] DON'T COLLAPSE");

        System.out.println();
        System.out.println("Please pick a number from the choices above.");

        typeTest = sc.next();

        if (IsNumber(typeTest)) {
            testT = Convert(typeTest);
        } else {
            do {
                System.out.println("Please enter a number only.");
                typeTest = sc.next();
            } while (!IsNumber(typeTest));

            testT = Convert(typeTest);
        }
        type = testT;
    } while (type < 1 || type > 4);

    if (type == 1) {

        JFrame frame2 = new JFrame();
        //frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                        
        JTable table2 = new JTable();
        table2.setModel(new DefaultTableModel((int) (k + 2), 7));

        table2.setValueAt("CLASS LIMITS", 0, 0);
        table2.setValueAt("TRUE CLASS LIMITS", 0, 1);
        table2.setValueAt("MIDPOINTS", 0, 2);
        table2.setValueAt("FREQUENCY", 0, 3);
        table2.setValueAt("%", 0, 4);
        table2.setValueAt("CF", 0, 5);
        table2.setValueAt("C%", 0, 6);
        table2.setValueAt("n = " + N, (int) (k + 1), 3);
        table2.setValueAt("TOTAL = 100%", (int) (k + 1), 4);

        for (int i = 0; i < k; i++) {
            table2.setValueAt(cl.get(i) + " - " + cl2.get(i), i + 1, 0);
            table2.setValueAt(tlcl.get(i) + " - " + tucl.get(i), i + 1, 1);
            table2.setValueAt(midList.get(i), i + 1, 2);
            table2.setValueAt(freq.get(i).size(), i + 1, 3);
            table2.setValueAt(new DecimalFormat("#.##").format(freqPercent.get(i)), i + 1, 4);
            table2.setValueAt(cfs.get(i), i + 1, 5);
            table2.setValueAt(new DecimalFormat("#.##").format(cps.get(i)), i + 1, 6);
        }
        table2.setValueAt("below " + cl2.get(0), 1, 0);
        table2.setValueAt(" - ", 1, 1);
        table2.setValueAt(" - ", 1, 2);

        JScrollPane scrollPane2 = new JScrollPane(table2);

        scrollPane2.setBorder(BorderFactory.createTitledBorder(title));
        frame2.add(scrollPane2, BorderLayout.CENTER);
        frame2.setSize(300, 150);
        frame2.setVisible(true);

    } else if (type == 2) {

        JFrame frame3 = new JFrame();
        //frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                        
        JTable table3 = new JTable();
        table3.setModel(new DefaultTableModel((int) (k + 2), 7));

        table3.setValueAt("CLASS LIMITS", 0, 0);
        table3.setValueAt("TRUE CLASS LIMITS", 0, 1);
        table3.setValueAt("MIDPOINTS", 0, 2);
        table3.setValueAt("FREQUENCY", 0, 3);
        table3.setValueAt("%", 0, 4);
        table3.setValueAt("CF", 0, 5);
        table3.setValueAt("C%", 0, 6);
        table3.setValueAt("n = " + N, (int) (k + 1), 3);
        table3.setValueAt("TOTAL = 100%", (int) (k + 1), 4);
        int a = (int) k;

        for (int i = 0; i < k; i++) {
            table3.setValueAt(cl.get(i) + " - " + cl2.get(i), i + 1, 0);
            table3.setValueAt(tlcl.get(i) + " - " + tucl.get(i), i + 1, 1);
            table3.setValueAt(midList.get(i), i + 1, 2);
            table3.setValueAt(freq.get(i).size(), i + 1, 3);
            table3.setValueAt(new DecimalFormat("#.##").format(freqPercent.get(i)), i + 1, 4);
            table3.setValueAt(cfs.get(i), i + 1, 5);
            table3.setValueAt(new DecimalFormat("#.##").format(cps.get(i)), i + 1, 6);
        }
        table3.setValueAt(cl.get(a - 1) + " above", a, 0);
        table3.setValueAt(" - ", a, 1);
        table3.setValueAt(" - ", a, 2);

        JScrollPane scrollPane3 = new JScrollPane(table3);
        scrollPane3.setBorder(BorderFactory.createTitledBorder(title));
        frame3.add(scrollPane3, BorderLayout.CENTER);

        frame3.setSize(300, 150);
        frame3.setVisible(true);
    }

    else if (type == 3) {
        JFrame frame4 = new JFrame();
        //frame4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                        
        JTable table4 = new JTable();
        table4.setModel(new DefaultTableModel((int) (k + 2), 7));
        table4.setValueAt("CLASS LIMITS", 0, 0);
        table4.setValueAt("TRUE CLASS LIMITS", 0, 1);
        table4.setValueAt("MIDPOINTS", 0, 2);
        table4.setValueAt("FREQUENCY", 0, 3);
        table4.setValueAt("%", 0, 4);
        table4.setValueAt("CF", 0, 5);
        table4.setValueAt("C%", 0, 6);
        table4.setValueAt("n = " + N, (int) (k + 1), 3);
        table4.setValueAt("TOTAL = 100%", (int) (k + 1), 4);

        int a = (int) k;

        for (int i = 0; i < k; i++) {
            table4.setValueAt(cl.get(i) + " - " + cl2.get(i), i + 1, 0);
            table4.setValueAt(tlcl.get(i) + " - " + tucl.get(i), i + 1, 1);
            table4.setValueAt(midList.get(i), i + 1, 2);
            table4.setValueAt(freq.get(i).size(), i + 1, 3);
            table4.setValueAt(new DecimalFormat("#.##").format(freqPercent.get(i)), i + 1, 4);
            table4.setValueAt(cfs.get(i), i + 1, 5);
            table4.setValueAt(new DecimalFormat("#.##").format(cps.get(i)), i + 1, 6);
        }
        table4.setValueAt("below " + cl2.get(0), 1, 0);
        table4.setValueAt(cl.get(a - 1) + " above", a, 0);
        table4.setValueAt(" - ", 1, 1);
        table4.setValueAt(" - ", 1, 2);
        table4.setValueAt(" - ", a, 1);
        table4.setValueAt(" - ", a, 2);

        JScrollPane scrollPane4 = new JScrollPane(table4);
        scrollPane4.setBorder(BorderFactory.createTitledBorder(title));
        frame4.add(scrollPane4, BorderLayout.CENTER);

        frame4.setSize(300, 150);
        frame4.setVisible(true);
    } else {

    }
}