TableColumn: setCellEditor(TableCellEditor cellEditor) : TableColumn « javax.swing.table « Java by API






TableColumn: setCellEditor(TableCellEditor cellEditor)

 
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.new TableColumn(int modelIndex)
2.TableColumn: getModelIndex()
3.TableColumn: setCellRenderer(TableCellRenderer cellRenderer)
4.TableColumn: setHeaderRenderer(TableCellRenderer headerRenderer)
5.TableColumn: setHeaderValue(Object headerValue)
6.TableColumn: setMaxWidth(int maxWidth)
7.TableColumn: setMinWidth(int minWidth)
8.TableColumn: setPreferredWidth(int preferredWidth)