JAVA JTable Cell editors
This example shows how to insert different cell
edtiors in a single column. If a cell editor
is not specified, then the default cell editor for the column is
used.
This example has 2 class files:
EachRowEditorExample.java
EachRowEditor.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class EachRowEditorExample extends JFrame {
public EachRowEditorExample(){
super("EachRow Editor Example");
DefaultTableModel dm = new DefaultTableModel();
dm.setDataVector(
new Object[][]{{"Name" ,"MyName"},
{"Gender","Male"}},
new Object[]{"Column1","Column2"});
JTable table = new JTable(dm);
JComboBox comboBox = new JComboBox();
comboBox.addItem("Male");
comboBox.addItem("Female");
EachRowEditor rowEditor = new EachRowEditor();
rowEditor.add(1, new DefaultCellEditor(comboBox));
table.getColumn("Column2").setCellEditor(rowEditor);
JScrollPane scroll = new JScrollPane(table);
getContentPane().add( scroll );
setSize( 400, 100 );
setVisible(true);
}
public static void main(String[] args) {
EachRowEditorExample frame = new EachRowEditorExample();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit(0);
}
});
}
}
|