/* (swing1.1beta3) */
package tame.examples;
import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.EventObject;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.CellEditorListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
/**
@author Nobuo Tamemasa
@version 1.0 11/09/98
*/
public class MultiComponentTable extends JFrame {
public MultiComponentTable(){
super("MultiComponent Table");
DefaultTableModel dm = new DefaultTableModel() {
public boolean isCellEditable(int row, int column) {
if (column == 0) {
return true;
}
return false;
}
};
dm.setDataVector(
new Object[][]{
{new ComboString("true") ,"ComboString","JLabel" ,"JComboBox"},
{new ComboString("false"),"ComboString","JLabel" ,"JComboBox"},
{new Boolean(true) ,"Boolean" ,"JCheckBox","JCheckBox"},
{new Boolean(false) ,"Boolean" ,"JCheckBox","JCheckBox"},
{"true" ,"String" ,"JLabel" ,"JTextField"},
{"false" ,"String" ,"JLabel" ,"JTextField"}},
new Object[]{"Component","Data","Renderer","Editor"});
JTable table = new JTable(dm);
table.getColumn("Component").setCellRenderer(
new MultiRenderer());
table.getColumn("Component").setCellEditor(
new MultiEditor());
JScrollPane scroll = new JScrollPane(table);
getContentPane().add( scroll );
setSize( 400, 160 );
setVisible(true);
}
public static void main(String[] args) {
MultiComponentTable frame = new MultiComponentTable();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit(0);
}
});
}
}
/**
@author Nobuo Tamemasa
@version 1.0 11/09/98
*/
class ComboString {
String str;
ComboString(String str) {
this.str = str;
}
public String toString() {
return str;
}
}
/**
@author Nobuo Tamemasa
@version 1.0 11/09/98
*/
class MultiRenderer extends DefaultTableCellRenderer {
JCheckBox checkBox = new JCheckBox();
public Component getTableCellRendererComponent(
JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
if (value instanceof Boolean) { // Boolean
checkBox.setSelected(((Boolean)value).booleanValue());
checkBox.setHorizontalAlignment(JLabel.CENTER);
return checkBox;
}
String str = (value == null) ? "" : value.toString();
return super.getTableCellRendererComponent(
table,str,isSelected,hasFocus,row,column);
}
}
/**
@author Nobuo Tamemasa
@version 1.0 11/09/98
*/
class MultiEditor implements TableCellEditor {
private final static int COMBO = 0;
private final static int BOOLEAN = 1;
private final static int STRING = 2;
private final static int NUM_EDITOR = 3;
DefaultCellEditor[] cellEditors;
JComboBox comboBox;
int flg;
public MultiEditor() {
cellEditors = new DefaultCellEditor[NUM_EDITOR];
//----------------------------------------------------
comboBox = new JComboBox();
comboBox.addItem("true");
comboBox.addItem("false");
cellEditors[COMBO] = new DefaultCellEditor(comboBox);
//----------------------------------------------------
JCheckBox checkBox = new JCheckBox();
checkBox.setHorizontalAlignment(JLabel.CENTER);
cellEditors[BOOLEAN] = new DefaultCellEditor(checkBox);
//----------------------------------------------------
JTextField textField = new JTextField();
cellEditors[STRING] = new DefaultCellEditor(textField);
//----------------------------------------------------
flg = COMBO;
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
System.err.println("getTableCellEditorComponent called:");
System.err.println(" isSelected: " + isSelected);
System.err.println(" row: " + row);
System.err.println(" col: " + column);
if (value instanceof ComboString) { // ComboString
System.err.println(" ComboString");
flg = COMBO;
String str = (value == null) ? "" : value.toString();
return cellEditors[COMBO].getTableCellEditorComponent(
table, str, isSelected, row, column);
} else if (value instanceof Boolean) { // Boolean
System.err.println(" Boolean");
flg = BOOLEAN;
return cellEditors[BOOLEAN].getTableCellEditorComponent(
table, value, isSelected, row, column);
} else if (value instanceof String) { // String
System.err.println(" String");
flg = STRING;
return cellEditors[STRING].getTableCellEditorComponent(
table, value, isSelected, row, column);
}
else {
System.err.println(" Trouble!");
}
return null;
}
public Object getCellEditorValue() {
System.err.println("getCellEditorValue called");
System.err.println(" flg = " + flg);
switch (flg) {
case COMBO:
String str = (String)comboBox.getSelectedItem();
return new ComboString(str);
case BOOLEAN:
case STRING:
return cellEditors[flg].getCellEditorValue();
default: return null;
}
}
public Component getComponent() {
return cellEditors[flg].getComponent();
}
public boolean stopCellEditing() {
return cellEditors[flg].stopCellEditing();
}
public void cancelCellEditing() {
cellEditors[flg].cancelCellEditing();
}
public boolean isCellEditable(EventObject anEvent) {
return cellEditors[flg].isCellEditable(anEvent);
}
public boolean shouldSelectCell(EventObject anEvent) {
return cellEditors[flg].shouldSelectCell(anEvent);
}
public void addCellEditorListener(CellEditorListener l) {
cellEditors[flg].addCellEditorListener(l);
}
public void removeCellEditorListener(CellEditorListener l) {
cellEditors[flg].removeCellEditorListener(l);
}
public void setClickCountToStart(int n) {
cellEditors[flg].setClickCountToStart(n);
}
public int getClickCountToStart() {
return cellEditors[flg].getClickCountToStart();
}
}
|
|