Example usage for javax.swing.table DefaultTableModel DefaultTableModel

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

Introduction

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

Prototype

public DefaultTableModel(Object[][] data, Object[] columnNames) 

Source Link

Document

Constructs a DefaultTableModel and initializes the table by passing data and columnNames to the setDataVector method.

Usage

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   ww  w  .jav  a2s . c o m*/
    tabela.setModel(apaga);
}

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// w w  w  .j  ava2s .  c o  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 Main() {
    DefaultTableModel model = new DefaultTableModel(0, 5) {
        @Override//from  w  w w  .  ja v  a 2 s.  c  o  m
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    };
    JTable table = new JTable(model);
    for (int i = 0; i < 20; i++) {
        model.addRow(new String[] { i + ".1", i + ".2", i + ".3", i + ".4", i + ".5", });
    }
    add(table, BorderLayout.CENTER);
    table.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            System.out.println("pressed");
            char key = e.getKeyChar();
            int selectedColumn = table.getSelectedColumn();
            for (int i = 0; i < model.getRowCount(); i++) {
                String value = (String) model.getValueAt(i, selectedColumn);
                model.setValueAt(value + key, i, selectedColumn);
            }
        }
    });
}

From source file:Main.java

/**
 * Make DefaultTableModel by overriding getColumnClass and isCellEditable of
 * DefaultTableModel./*from w  w  w  . ja  va 2s. c  o  m*/
 *
 * @param titleA The title of each column.
 * @param typesA The type of each column value.
 * @param canEditA Is column editable?
 *
 * @return
 */
public static DefaultTableModel makeDefaultTableModel(String[] titleA, final Class<?>[] typesA,
        final boolean[] canEditA) {
    return new DefaultTableModel(new Object[][] {}, titleA) {
        Class<?>[] types = typesA;
        boolean[] canEdit = canEditA;

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return types[columnIndex];
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit[columnIndex];
        }
    };
}

From source file:Main.java

public Main() {
    DefaultTableModel m = new DefaultTableModel(new Object[][] { { "2", 2, 3 }, { "1", 4, 5 } },
            new Object[] { 1, 2, 3 });
    JTable t = new JTable(m);
    t.getColumnModel().getColumn(0)/*  w w w .  j  a v a 2  s. c om*/
            .setCellEditor(new DefaultCellEditor(new JComboBox(new String[] { "1", "2" })));
    t.getColumnModel().getColumn(0).setCellRenderer(getCellRenderer());
    t.setRowHeight(25);
    getContentPane().add(new JScrollPane(t));
    pack();
}

From source file:Main.java

public Main() {
    setSize(300, 200);/*  www . j a  v  a  2  s  . c o m*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    DefaultTableModel dtm = new DefaultTableModel(new String[][] { { "1", "2", "3" }, { "4", "5", "6" } },
            new String[] { "Names", "In", "Order" });
    SortingColumnModel scm = new SortingColumnModel();
    JTable jt = new JTable(dtm, scm);
    jt.createDefaultColumnsFromModel();

    JScrollPane jsp = new JScrollPane(jt);
    getContentPane().add(jsp, BorderLayout.CENTER);
}

From source file:MainClass.java

public MainClass() {
    super("Abstract Model JTable Test");
    setSize(300, 200);//from   w ww  .j  a v  a2 s.co  m
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    DefaultTableModel dtm = new DefaultTableModel(new String[][] { { "1", "2", "3" }, { "4", "5", "6" } },
            new String[] { "Names", "In", "Order" });
    SortingColumnModel scm = new SortingColumnModel();
    JTable jt = new JTable(dtm, scm);
    jt.createDefaultColumnsFromModel();

    JScrollPane jsp = new JScrollPane(jt);
    getContentPane().add(jsp, BorderLayout.CENTER);
}

From source file:ColumnExample.java

public ColumnExample() {
    super("Abstract Model JTable Test");
    setSize(300, 200);//from  ww  w. ja  va2s.c o m
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    DefaultTableModel dtm = new DefaultTableModel(new String[][] { { "1", "2", "3" }, { "4", "5", "6" } },
            new String[] { "Names", "In", "Order" });
    SortingColumnModel scm = new SortingColumnModel();
    JTable jt = new JTable(dtm, scm);
    jt.createDefaultColumnsFromModel();

    JScrollPane jsp = new JScrollPane(jt);
    getContentPane().add(jsp, BorderLayout.CENTER);
}

From source file:at.nullpointer.trayrss.gui.tablemodel.TableModelFactory.java

public static DefaultTableModel getTableModel(Set<Feed> feeds) {

    DefaultTableModel tableModel = new DefaultTableModel(getHeader(), 0) {

        Class[] columnTypes = new Class[] { String.class, String.class, Long.class, Boolean.class };

        public Class<?> getColumnClass(int columnIndex) {

            return columnTypes[columnIndex];
        }//from w  w  w. j  a v  a 2  s . c  om

        boolean[] columnEditables = new boolean[] { false, false, false, false };

        public boolean isCellEditable(int row, int column) {

            return columnEditables[column];
        }
    };

    for (Feed feed : feeds) {
        tableModel.addRow(feedToObjectRow(feed));
    }

    return tableModel;

}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    String[] columns = { "Item", "Price" };

    Object[][] rows = { { "Potatoes", 10.98 }, { "Magazine", 7.99 }, { "Can of soup", 0.89 },
            { "DVD movie", 39.99 } };

    TableModel model = new DefaultTableModel(rows, columns) {
        public Class getColumnClass(int column) {
            if (column >= 0 && column <= getColumnCount())
                return getValueAt(0, column).getClass();
            else/*  w ww.j  a  v a  2 s  .  co  m*/
                return Object.class;
        }
    };
    JTable table = new JTable(model);
    RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
    table.setRowSorter(sorter);
    getContentPane().add(new JScrollPane(table));

    setSize(200, 150);
    setVisible(true);
}