Example usage for javax.swing.table AbstractTableModel AbstractTableModel

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

Introduction

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

Prototype

AbstractTableModel

Source Link

Usage

From source file:AbstractSample.java

public static void main(String args[]) {
    TableModel model = new AbstractTableModel() {
        Object rowData[][] = { { "one", "ichi" }, { "two", "ni" }, { "three", "san" }, { "four", "shi" },
                { "five", "go" }, { "six", "roku" }, { "seven", "shichi" }, { "eight", "hachi" },
                { "nine", "kyu" }, { "ten", "ju" } };

        Object columnNames[] = { "English", "Japanese" };

        public String getColumnName(int column) {
            return columnNames[column].toString();
        }/*from  w  ww  .ja  va2 s  . c  om*/

        public int getRowCount() {
            return rowData.length;
        }

        public int getColumnCount() {
            return columnNames.length;
        }

        public Object getValueAt(int row, int col) {
            return rowData[row][col];
        }
    };
    JFrame frame = new JFrame("Abstract Sample");
    JTable table = new JTable(model);
    JScrollPane scrollPane = new JScrollPane(table);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:UsingTableModelToContruct.java

public static void main(String args[]) {

    TableModel model = new AbstractTableModel() {
        Object rowData[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" } };

        Object columnNames[] = { "English", "#" };

        public String getColumnName(int column) {
            return columnNames[column].toString();
        }/*from   www  .ja v a2  s  . c om*/

        public int getRowCount() {
            return rowData.length;
        }

        public int getColumnCount() {
            return columnNames.length;
        }

        public Object getValueAt(int row, int col) {
            return rowData[row][col];
        }
    };
    JTable table = new JTable(model);
    JScrollPane scrollPane = new JScrollPane(table);

    JFrame frame = new JFrame("Resizing Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);

}

From source file:ColumnSample.java

public static void main(String args[]) {
    TableModel model = new AbstractTableModel() {
        Icon icon1 = new ImageIcon("TreeCollapsed.gif");

        Icon icon2 = new ImageIcon("TreeExpanded.gif");

        Object rowData[][] = { { "1", "ichi", Boolean.TRUE, new Date("01/01/2000"), icon1 },
                { "2", "ni", Boolean.TRUE, new Date("04/15/1999"), icon2 },
                { "3", "san", Boolean.FALSE, new Date("12/07/1941"), icon2 },
                { "4", "shi", Boolean.TRUE, new Date("02/29/2000"), icon1 },
                { "5", "go", Boolean.FALSE, new Date("05/23/1995"), icon1 }, };

        String columnNames[] = { "English", "Japanese", "Boolean", "Date", "ImageIcon" };

        public int getColumnCount() {
            return columnNames.length;
        }/*from  w  w  w.  j  av  a2s. c  o m*/

        public String getColumnName(int column) {
            return columnNames[column];
        }

        public int getRowCount() {
            return rowData.length;
        }

        public Object getValueAt(int row, int column) {
            return rowData[row][column];
        }

        public Class getColumnClass(int column) {
            return (getValueAt(0, column).getClass());
        }
    };

    JFrame frame = new JFrame("Column Renderer Table");
    JTable table = new JTable(model);
    JScrollPane scrollPane = new JScrollPane(table);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(final String args[]) {
    final Object rows[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" } };
    final String headers[] = { "English", "Digit" };

    TableModel fixedColumnModel = new AbstractTableModel() {
        public int getColumnCount() {
            return 2;
        }/*from  w  w w. j a va 2s  .c o  m*/

        public String getColumnName(int column) {
            return headers[column];
        }

        public int getRowCount() {
            return 3;
        }

        public Object getValueAt(int row, int column) {
            return rows[row][column];
        }
    };

    JFrame frame = new JFrame("Scrollless Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(fixedColumnModel);

    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    frame.add(new JScrollPane(table), BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(final String args[]) {
    final Object rows[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" } };
    final String headers[] = { "English", "Digit" };

    TableModel fixedColumnModel = new AbstractTableModel() {
        public int getColumnCount() {
            return 2;
        }/*from w  w  w  .  j av a2 s .c o m*/

        public String getColumnName(int column) {
            return headers[column];
        }

        public int getRowCount() {
            return 3;
        }

        public Object getValueAt(int row, int column) {
            return rows[row][column];
        }
    };

    JFrame frame = new JFrame("Scrollless Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(fixedColumnModel);

    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    frame.add(new JScrollPane(table), BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:EditableColumn.java

public static void main(String args[]) {
    TableModel model = new AbstractTableModel() {
        Icon icon1 = new ImageIcon("TreeCollapsed.gif");

        Icon icon2 = new ImageIcon("TreeExpanded.gif");

        Object rowData[][] = { { new Integer(1), "ichi", Boolean.TRUE, new Date("01/01/2000"), icon1 },
                { new Integer(2), "ni", Boolean.TRUE, new Date("04/15/1999"), icon2 },
                { new Integer(3), "san", Boolean.FALSE, new Date("12/07/1941"), icon2 },
                { new Integer(4), "shi", Boolean.TRUE, new Date("02/29/2000"), icon1 },
                { new Integer(5), "go", Boolean.FALSE, new Date("05/23/1995"), icon1 }, };

        String columnNames[] = { "English", "Japanese", "Boolean", "Date", "ImageIcon" };

        public int getColumnCount() {
            return columnNames.length;
        }//  w ww  .  j  a v a  2 s . c om

        public String getColumnName(int column) {
            return columnNames[column];
        }

        public int getRowCount() {
            return rowData.length;
        }

        public Object getValueAt(int row, int column) {
            return rowData[row][column];
        }

        public Class getColumnClass(int column) {
            return (getValueAt(0, column).getClass());
        }

        public void setValueAt(Object value, int row, int column) {
            rowData[row][column] = value;
        }

        public boolean isCellEditable(int row, int column) {
            return (column != 4);
        }
    };

    JFrame frame = new JFrame("Column Renderer Table");
    JTable table = new JTable(model);
    JScrollPane scrollPane = new JScrollPane(table);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(400, 150);
    frame.setVisible(true);
}

From source file:FixedColumnModel.java

public static void main(String args[]) {

    final Object rowData[][] = { { "1", "one", "I" }, { "2", "two", "II" }, { "3", "three", "III" } };

    final String columnNames[] = { "#", "English", "Roman" };

    final TableModel fixedColumnModel = new AbstractTableModel() {
        public int getColumnCount() {
            return 1;
        }/*  w w w.  j  a  v a 2 s.  c o  m*/

        public String getColumnName(int column) {
            return columnNames[column];
        }

        public int getRowCount() {
            return rowData.length;
        }

        public Object getValueAt(int row, int column) {
            return rowData[row][column];
        }
    };

    final TableModel mainModel = new AbstractTableModel() {
        public int getColumnCount() {
            return columnNames.length - 1;
        }

        public String getColumnName(int column) {
            return columnNames[column + 1];
        }

        public int getRowCount() {
            return rowData.length;
        }

        public Object getValueAt(int row, int column) {
            return rowData[row][column + 1];
        }
    };

    JTable fixedTable = new JTable(fixedColumnModel);
    fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    JTable mainTable = new JTable(mainModel);
    mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    ListSelectionModel model = fixedTable.getSelectionModel();
    mainTable.setSelectionModel(model);

    JScrollPane scrollPane = new JScrollPane(mainTable);
    Dimension fixedSize = fixedTable.getPreferredSize();
    JViewport viewport = new JViewport();
    viewport.setView(fixedTable);
    viewport.setPreferredSize(fixedSize);
    viewport.setMaximumSize(fixedSize);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedTable.getTableHeader());
    scrollPane.setRowHeaderView(viewport);

    JFrame frame = new JFrame("Fixed Column Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:ShowCurrencies.java

public ShowCurrencies() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    final Locale[] locales = Locale.getAvailableLocales();
    TableModel model = new AbstractTableModel() {
        public int getColumnCount() {
            return 3;
        }/*from   w  ww  . ja va2 s  . c o m*/

        public String getColumnName(int column) {
            if (column == 0)
                return "Locale";
            else if (column == 1)
                return "Currency Code";
            else
                return "Currency Symbol";
        }

        public int getRowCount() {
            return locales.length;
        }

        public Object getValueAt(int row, int col) {
            if (col == 0)
                return locales[row];
            else
                try {
                    if (col == 1)
                        return Currency.getInstance(locales[row]).getCurrencyCode();
                    else
                        return Currency.getInstance(locales[row]).getSymbol(locales[row]);
                } catch (IllegalArgumentException iae) {
                    return null;
                }
        }
    };

    JTable table = new JTable(model);
    JScrollPane sp = new JScrollPane(table);
    getContentPane().add(sp);
    pack();
    setVisible(true);
}

From source file:MainClass.java

public MainClass() {
    super("Table With DefaultCellEditor Example");
    setSize(500, 300);/*from   w  w  w.j a v a2s.com*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JTable table = new JTable(new AbstractTableModel() {
        ColorName data[] = { colors[0], colors[1], colors[2], colors[3], colors[4], colors[0], colors[1],
                colors[2], colors[3], colors[4] };

        public int getColumnCount() {
            return 3;
        }

        public int getRowCount() {
            return 10;
        }

        public Object getValueAt(int r, int c) {
            switch (c) {
            case 0:
                return (r + 1) + ".";
            case 1:
                return "Some pithy quote #" + r;
            case 2:
                return data[r];
            }
            return "Bad Column";
        }

        public Class getColumnClass(int c) {
            if (c == 2)
                return ColorName.class;
            return String.class;
        }

        public boolean isCellEditable(int r, int c) {
            return c == 2;
        }

        public void setValueAt(Object value, int r, int c) {
            data[r] = (ColorName) value;
        }
    });

    table.setDefaultEditor(ColorName.class, new DefaultCellEditor(new JComboBox(colors)));
    table.setDefaultRenderer(ColorName.class, new DefaultTableCellRenderer());
    table.setRowHeight(20);
    getContentPane().add(new JScrollPane(table));
}

From source file:MainClass.java

public MainClass() {
    super("Row Header Test");
    setSize(300, 200);//from   w w  w. j  a  v  a2s  . c o  m
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    TableModel tm = new AbstractTableModel() {
        String data[] = { "", "a", "b", "c", "d", "e" };

        String headers[] = { "Row #", "Column 1", "Column 2", "Column 3", "Column 4", "Column 5" };

        public int getColumnCount() {
            return data.length;
        }

        public int getRowCount() {
            return 1000;
        }

        public String getColumnName(int col) {
            return headers[col];
        }

        public Object getValueAt(int row, int col) {
            return data[col] + row;
        }
    };

    TableColumnModel cm = new DefaultTableColumnModel() {
        boolean first = true;

        public void addColumn(TableColumn tc) {
            if (first) {
                first = false;
                return;
            }
            tc.setMinWidth(150);
            super.addColumn(tc);
        }
    };

    TableColumnModel rowHeaderModel = new DefaultTableColumnModel() {
        boolean first = true;

        public void addColumn(TableColumn tc) {
            if (first) {
                tc.setMaxWidth(tc.getPreferredWidth());
                super.addColumn(tc);
                first = false;
            }
        }
    };

    JTable jt = new JTable(tm, cm);
    JTable headerColumn = new JTable(tm, rowHeaderModel);
    jt.createDefaultColumnsFromModel();
    headerColumn.createDefaultColumnsFromModel();

    jt.setSelectionModel(headerColumn.getSelectionModel());

    headerColumn.setBackground(Color.lightGray);
    headerColumn.setColumnSelectionAllowed(false);
    headerColumn.setCellSelectionEnabled(false);

    JViewport jv = new JViewport();
    jv.setView(headerColumn);
    jv.setPreferredSize(headerColumn.getMaximumSize());

    jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    JScrollPane jsp = new JScrollPane(jt);
    jsp.setRowHeader(jv);
    jsp.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, headerColumn.getTableHeader());
    getContentPane().add(jsp, BorderLayout.CENTER);
}