import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
class TableCellRendererTest extends JFrame{
private static final int DEFAULT_WIDTH=600;
private static final int DEFAULT_HEIGHT=400;
TableCellRendererTest(){
setTitle("Table Cell Render Test");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
TableModel model=new CreateTableModel();
JTable table=new JTable(model);
JComboBox foodCombo=new JComboBox();
String[]foods={"กระเพราไก่","ข้าวผัดกุ้ง","ข้าวต้มกุ้ง","หมูทอดกระเทียม","ผัดผักรวมมิตร","แกงจืดเต้าหู้","มาม่า","ต้มยำรวมมิตร","ผัดเผ็ดปลาดุก"};
for(int i=0;i<foods.length;i++)
foodCombo.addItem(foods[i]);
//
TableColumnModel columnModel=table.getColumnModel();
TableColumn foodColumn=columnModel.getColumn(CreateTableModel.FOOD_COLUMN);
foodColumn.setCellEditor(new DefaultCellEditor(foodCombo));
foodColumn.setHeaderRenderer(table.getDefaultRenderer(ImageIcon.class));
foodColumn.setHeaderValue(new ImageIcon("meun.gif"));//
//
table.setRowHeight(50);
table.setRowSelectionAllowed(false);
table.setDefaultRenderer(Color.class,new ColorTableCellRenderer());
table.setDefaultEditor(Color.class,new ColorTableCellEditor());
table.setForeground(Color.blue);
add(new JScrollPane(table),BorderLayout.CENTER);
}
public static void main(String[]args){
JFrame frame=new TableCellRendererTest();
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
frame.setVisible(true);
}
}
class CreateTableModel extends AbstractTableModel{
public static final int DAY_COLUMN=0;
public static final int FOOD_COLUMN=1;
public static final int CHECKBOX_COLUMN=2;
public static final int COLORPAINT_COLUMN=4;
private Object[][]cells={
{"อาทิตย์","กระเพราไก่",true,new ImageIcon("p11.gif"),Color.red},
{"จันทร์","ข้าวผัดกุ้ง",true,new ImageIcon("p22.gif"),Color.yellow},
{"อังคาร","ข้าวต้มกุ้ง",false,new ImageIcon("p33.gif"),Color.green},
{"พุธ","หมูทอดกระเทียม",true,new ImageIcon("p44.gif"),Color.pink},
{"พฤหัสบดี","ผัดผักรวมมิตร",true,new ImageIcon("p55.gif"),Color.blue},
{"ศุกร์","แกงจืดเต้าหู้",true,new ImageIcon("p66.gif"),Color.white},
{"เสาร์","มาม่า",false,new ImageIcon("p77.gif"),Color.orange}
};
private String[]columnNames={"วัน","อาหารเช้า","ใส่ไข่","รูปภาพ","สี"};
public Object getValueAt(int r,int c){return cells[r][c];}
public int getColumnCount(){return cells[0].length;}
public int getRowCount(){return cells.length;}
public String getColumnName(int c){return columnNames[c];}
public Class getColumnClass(int c){return cells[0][c].getClass();}
public void setValueAt(Object obj,int r,int c){cells[r][c]=obj;}//
public boolean isCellEditable(int r,int c){//
return c==DAY_COLUMN||c==FOOD_COLUMN||c==CHECKBOX_COLUMN||c==COLORPAINT_COLUMN;
}
}//
class ColorTableCellRenderer extends JPanel implements TableCellRenderer{
public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected
,boolean hasFocus,int row,int column){
setBackground((Color)value);
return this;
}
}
class ColorTableCellEditor extends AbstractCellEditor implements TableCellEditor{
JColorChooser colorChoose;
JDialog colorDialog;
JPanel panel;
public ColorTableCellEditor(){
panel=new JPanel();
colorChoose=new JColorChooser();
colorDialog=JColorChooser.createDialog(null,"Chooser Color",false,colorChoose,
new ActionListener(){//
public void actionPerformed(ActionEvent e){stopCellEditing();}
},
new ActionListener(){//
public void actionPerformed(ActionEvent e){cancelCellEditing();}
});
colorDialog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){cancelCellEditing();}
});
}
//
public Component getTableCellEditorComponent(JTable table,Object value,boolean isSelected
,int row,int column){
colorChoose.setColor((Color)value);
return panel;
}
public Object getCellEditorValue(){
return colorChoose.getColor();
}
//
public boolean shouldSelectCell(EventObject anEvent){
colorDialog.setVisible(true);
return true;
}
public void cancelCellEditing(){
colorDialog.setVisible(false);
super.cancelCellEditing();
}
public boolean stopCellEdiying(){
colorDialog.setVisible(false);
super.stopCellEditing();
return true;
}
};
ไม่มีความคิดเห็น:
แสดงความคิดเห็น