Example usage for javax.swing.table DefaultTableModel addColumn

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

Introduction

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

Prototype

public void addColumn(Object columnName, Object[] columnData) 

Source Link

Document

Adds a column to the model.

Usage

From source file:Main.java

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

    model.addColumn("A", new Object[] { "item1" });
    model.addColumn("B", new Object[] { "item2" });

    String[] values = new String[] { "1", "2", "3" };
    TableColumn col = table.getColumnModel().getColumn(0);
    col.setCellEditor(new SpinnerEditor(values));
}

From source file:Main.java

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

    model.addColumn("A", new Object[] { "item1" });
    model.addColumn("B", new Object[] { "item2" });

    String[] values = new String[] { "1", "2", "3" };

    TableColumn col = table.getColumnModel().getColumn(0);
    col.setCellEditor(new MyComboBoxEditor(values));
    col.setCellRenderer(new MyComboBoxRenderer(values));
}

From source file:Main.java

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

    // Add a column with values.
    model.addColumn("Col2", new Object[] { "v2" });

}

From source file:Main.java

public static void main(String[] argv) {
    DefaultTableModel model = new DefaultTableModel() {
        public Class getColumnClass(int columnIndex) {
            Object o = getValueAt(0, columnIndex);
            if (o == null) {
                return Object.class;
            } else {
                return o.getClass();
            }/*from   ww  w. jav  a 2  s.c  om*/
        }
    };
    JTable table = new JTable(model);

    model.addColumn("Boolean", new Object[] { Boolean.TRUE });
    model.addColumn("Date", new Object[] { new Date() });
    model.addColumn("Double", new Object[] { new Double(Math.PI) });
    model.addColumn("Float", new Object[] { new Float(1.2) });
    model.addColumn("Icon", new Object[] { new ImageIcon("icon.gif") });
    model.addColumn("Number", new Object[] { new Integer(1) });
    model.addColumn("Object", new Object[] { "object" });

    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) {
    DefaultTableModel model = new DefaultTableModel() {
        public Class getColumnClass(int columnIndex) {
            Object o = getValueAt(0, columnIndex);
            if (o == null) {
                return Object.class;
            } else {
                return o.getClass();
            }//  w w w  .  ja v  a2s  .c  o  m
        }
    };
    JTable table = new JTable(model);

    model.addColumn("Boolean", new Object[] { Boolean.TRUE });
    model.addColumn("Date", new Object[] { new Date() });
    model.addColumn("Double", new Object[] { new Double(Math.PI) });
    model.addColumn("Float", new Object[] { new Float(1.2) });
    model.addColumn("Icon", new Object[] { new ImageIcon("icon.gif") });
    model.addColumn("Number", new Object[] { new Integer(1) });
    model.addColumn("Object", new Object[] { "object" });

    Enumeration e = table.getColumnModel().getColumns();
    TableColumn col = (TableColumn) e.nextElement();

    col.setCellRenderer(table.getDefaultRenderer(Boolean.class));
    col.setCellEditor(table.getDefaultEditor(Boolean.class));

    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 = (DefaultTableModel) table.getModel();
    TableColumn col = new TableColumn(model.getColumnCount());

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

}

From source file:Main.java

public static void main(String[] argv) {
    DefaultTableModel model = new DefaultTableModel() {
        public Class getColumnClass(int mColIndex) {
            int rowIndex = 0;
            Object o = getValueAt(rowIndex, mColIndex);
            if (o == null) {
                return Object.class;
            } else {
                return o.getClass();
            }/* w w  w .j ava  2s . c  o  m*/
        }
    };
    JTable table = new JTable(model);
    model.addColumn("Col1", new Object[] { Color.red });
    model.addRow(new Object[] { Color.green });
    model.addRow(new Object[] { Color.blue });

    table.setDefaultRenderer(Color.class, new ColorTableCellRenderer());

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

From source file:dbseer.gui.chart.DBSeerChartFactory.java

public static JTable createErrorTable(PredictionCenter center) throws Exception {
    JTable errorTable = null;/*from www  . ja v  a2 s .  c o m*/
    DefaultTableModel errorTableModel = null;
    StatisticalPackageRunner runner = DBSeerGUI.runner;

    Object[] maeList = (Object[]) runner.getVariableCell("meanAbsError");
    Object[] mreList = (Object[]) runner.getVariableCell("meanRelError");
    Object[] headers = (Object[]) runner.getVariableCell("errorHeader");

    if (maeList.length > 0 || mreList.length > 0) {
        errorTableModel = new DefaultTableModel() {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        //errorTable = new JTable();
        errorTableModel.addColumn(null, new String[] { "", "MAE", "MRE" }); // first empty column

        final java.util.List<String> transactionNames = center.getTrainConfig().getDataset(0)
                .getTransactionTypeNames();
        for (int i = 0; i < maeList.length; ++i) {
            Object maeObj = maeList[i];
            Object mreObj = mreList[i];

            double[] mae = (double[]) maeObj;
            double[] mre = (double[]) mreObj;

            String header = (String) headers[i];
            for (int j = 0; j < transactionNames.size(); ++j) {
                if (header.contains("Type " + (j + 1))) {
                    headers[i] = header.replace("Type " + (j + 1), transactionNames.get(j));
                    break;
                }
            }

            errorTableModel.addColumn(null,
                    new Object[] { headers[i], String.format("%.3f", mae[0]), String.format("%.3f", mre[0]) });
        }
        errorTable = new JTable(errorTableModel);
    }

    return errorTable;
}