new DefaultTableModel(Object[][] data, Object[] columnNames) : DefaultTableModel « javax.swing.table « Java by API






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

 
import java.awt.BorderLayout;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;

public class MainClass {
  public static void main(String args[]) {
    String rows[][] = { { "A", "a" }, { "B", "b" }, { "E", "e" } };
    String headers[] = { "Upper", "Lower" };

    JComboBox comboBox = new JComboBox(rows[0]);
    comboBox.setMaximumRowCount(4);
    
    TableCellEditor editor = new DefaultCellEditor(comboBox);

    JFrame frame = new JFrame("JTable Anatomy");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTable table = new JTable(new DefaultTableModel(rows, headers));

    table.getColumnModel().getColumn(1).setCellEditor(editor);

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


           
         
  








Related examples in the same category

1.DefaultTableModel: addColumn(Object columnName)
2.DefaultTableModel: addRow(Object[] rowData)
3.DefaultTableModel: insertRow(int row, Object[] rowData)
4.DefaultTableModel: moveRow(int start, int end, int to)
5.DefaultTableModel: removeRow(int row)