UsingTableModelToContruct.java Source code

Java tutorial

Introduction

Here is the source code for UsingTableModelToContruct.java

Source

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

public class UsingTableModelToContruct {
    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();
            }

            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);

    }
}