Example usage for javax.swing JTable JTable

List of usage examples for javax.swing JTable JTable

Introduction

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

Prototype

public JTable(TableModel dm) 

Source Link

Document

Constructs a JTable that is initialized with dm as the data model, a default column model, and a default selection model.

Usage

From source file:TableRowSorterWithoutColumnClass.java

public TableRowSorterWithoutColumnClass() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);

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

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

    TableModel model = new DefaultTableModel(rows, columns);
    JTable table = new JTable(model);
    RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
    table.setRowSorter(sorter);// w ww . j av a2  s .  c om
    getContentPane().add(new JScrollPane(table));

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

From source file:Main.java

public Main() {
    ImageIcon aboutIcon = new ImageIcon("about16.gif");
    ImageIcon addIcon = new ImageIcon("add16.gif");
    ImageIcon copyIcon = new ImageIcon("copy16.gif");

    String[] columnNames = { "Picture", "Description" };
    Object[][] data = { { aboutIcon, "About" }, { addIcon, "Add" }, { copyIcon, "Copy" }, };

    DefaultTableModel model = new DefaultTableModel(data, columnNames);
    JTable table = new JTable(model) {
        public Class getColumnClass(int column) {
            return (column == 0) ? Icon.class : Object.class;
        }/*from   w w  w.j a  va 2  s.  c  o  m*/
    };
    table.setPreferredScrollableViewportSize(table.getPreferredSize());

    JScrollPane scrollPane = new JScrollPane(table);
    getContentPane().add(scrollPane);
}

From source file:MainClass.java

public MainClass() {
    super("Table With DefaultCellEditor Example");
    setSize(500, 300);/*from   w w w  .  j av a  2s .co m*/
    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:EvenOddRowCellRenderer.java

public EvenOddRowCellRenderer() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(tmodel);
    table.setDefaultRenderer(Object.class, new MyRenderer());
    getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
    pack();/*w w w.  ja va  2s .  c o  m*/
}

From source file:Main.java

public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(model);
    table.setDefaultEditor(Object.class, new MyEditor());
    getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
    pack();/*from   www  .  ja  v a 2  s . c  om*/
}

From source file:Main.java

public Main() {
    Vector dummyMacData = new Vector(10, 10);
    dummyMacData.addElement(new Data(new Integer(100), "A", "1", "C", "E"));
    dummyMacData.addElement(new Data(new Integer(105), "R", "2", "S", "E"));
    m_simpleTableModel = new SimpleTableModel(dummyMacData);
    m_simpleTable = new JTable(m_simpleTableModel);
    JScrollPane scrollPane = new JScrollPane(m_simpleTable);
    getContentPane().add(scrollPane);/*  ww  w. java2 s . co  m*/
}

From source file:MainClass.java

public MainClass() {
    super("Paged JTable Test");
    setSize(300, 200);/*from   w  w  w  .  ja  v  a2s.com*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    PagingModel pm = new PagingModel();
    pm.setPageSize(20);
    JTable jt = new JTable(pm);

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

From source file:Main.java

public Main() {
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(new JButton("Foo"));
    buttonPanel.add(Box.createHorizontalStrut(10));
    buttonPanel.add(new JButton("Bar"));

    String[] columnNames = { "Mon", "Tues", "Wed" };
    DefaultTableModel model = new DefaultTableModel(columnNames, 25);
    JTable table = new JTable(model);
    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.getViewport().setPreferredSize(table.getPreferredSize());

    JLabel southLabel = new JLabel("OK!");
    southLabel.setForeground(Color.white);
    JPanel southPanel = new JPanel();
    southPanel.add(southLabel);//from  w  w  w  .  j  a va2s . c  o  m

    setLayout(new BorderLayout(5, 5));
    add(buttonPanel, BorderLayout.NORTH);
    add(scrollPane, BorderLayout.CENTER);
    add(southPanel, BorderLayout.SOUTH);
}

From source file:DefaultCellEditorJTextFieldAlignment.java

public DefaultCellEditorJTextFieldAlignment() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(model);
    table.setDefaultEditor(Object.class, new MyEditor());
    getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
    pack();//from  w  w  w  . j a  va2  s  . c om
}

From source file:ThreadViewer.java

public ThreadViewer() {

    JTable table = new JTable(tableModel);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);

    TableColumnModel colModel = table.getColumnModel();
    int numColumns = colModel.getColumnCount();

    for (int i = 0; i < numColumns - 1; i++) {
        TableColumn col = colModel.getColumn(i);

        col.sizeWidthToFit();// www .  j  a v  a2s . c o m
        col.setPreferredWidth(col.getWidth() + 5);
        col.setMaxWidth(col.getWidth() + 5);
    }

    JScrollPane sp = new JScrollPane(table);

    setLayout(new BorderLayout());
    add(sp, BorderLayout.CENTER);
}