import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class JTableDemo4 implements ActionListener{
JTable table = null;
JButton button1,button2;
public JTableDemo4(){
JFrame jf = new JFrame();
button1 = new JButton("數學老師");
button1.addActionListener(this);
button2 = new JButton("學生阿呆");
button2.addActionListener(this);
JPanel panel = new JPanel();
panel.add(button1);
panel.add(button2);
table = new JTable(new MyTableModel(2));
table.setPreferredScrollableViewportSize(new Dimension(500,300));
// table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(new JCheckBox()));
//設置行列的選擇模式
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(true);
// 設置單元格選取時的顏色變化
table.setSelectionBackground(Color.yellow);
table.setSelectionForeground(Color.red);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.getViewport().setBackground(Color.white);
paintCell();
jf.getContentPane().add(panel,BorderLayout.NORTH);
jf.getContentPane().add(scrollPane,BorderLayout.CENTER);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String...args){
new JTableDemo4();
}
public void paintCell(){
for(int i=0;i<table.getColumnCount();i++){
table.getColumn(table.getColumnName(i)).setCellRenderer(new MyTableRenderer());
}
}
@Override
public void actionPerformed(ActionEvent e){
if(e.getSource()==button1){
table.setModel(new MyTableModel(2));
}
if(e.getSource()==button2){
table.setModel(new MyTableModel(1));
}
paintCell();
// System.out.println(table.getColumn("及格").getClass());
table.revalidate();
}
}
class MyTableModel extends AbstractTableModel{
Object[][] p1 = {
{"阿呆",new Integer(78),new Boolean(true)}
};
String[] n1 = {"姓名","成績","及格"};
Object[][] p2 = {
{"阿瓜",new Integer(58),new Boolean(false)},
{"阿呆",new Integer(78),new Boolean(true)}
};
//String[] n2 = {"姓名","成績","及格"};
//mode為1表示角色為阿呆,為2則表示角色為老師
int mode = 2;
public MyTableModel(int i){
mode = i;
}
@Override
public int getColumnCount(){
if(mode==1){
return n1.length;
}
else{
return n1.length;
}
}
@Override
public int getRowCount(){
if(mode==1){
return p1.length;
}else{
return p2.length;
}
}
@Override
public Object getValueAt(int row,int col){
if(mode==1){
return p1[row][col];
}else{
return p2[row][col];
}
}
@Override
public String getColumnName(int col){
if(mode==1){
return n1[col];
}else{
return n1[col];
}
}
@Override
public Class getColumnClass(int col){
//if(col==3) return Boolean.class;
//else
return getValueAt(0,col).getClass();
}
@Override
public boolean isCellEditable(int row,int col){
return true;
}
}
class MyTableRenderer extends DefaultTableCellRenderer{
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if(value!=null){
switch(row%2){
case 0:
setBackground(Color.cyan);
//setForeground(Color.black);
break;
case 1:
setBackground(Color.pink);
//setForeground(Color.black);
break;
default:
setBackground(Color.yellow);
break;
}
if(value instanceof Boolean){
JCheckBox jck =new JCheckBox();
jck.setSelected(Boolean.parseBoolean(value.toString()));
jck.setHorizontalAlignment(JCheckBox.CENTER);
return jck;
}
if(value instanceof String){
super.setHorizontalAlignment(super.LEFT);
}
if(value instanceof Integer){
super.setHorizontalAlignment(super.RIGHT);
}
if(isSelected){
// 為什么在這里設定選取單元格的顏色變化沒有效果呢?
System.out.println(row+" "+column+"被選中");
//this.setForeground(Color.yellow);
}
else{
//this.setBackground(new Color(200,100,100));
//this.setForeground(Color.red);
//setBackground(Color.blue);
}
if(hasFocus){
//System.out.println(row+" "+column+"獲得焦點");
}else{
// System.out.println(row+" "+column+"失去焦點");
}
}
return super.getTableCellRendererComponent(table, value,isSelected, hasFocus, row, column);
}
}
參考:深入淺出Java Swing程序設計
補充:想要實現單元格可更改,應該重寫isCellEidtable()和setValueAt()方法
如
public boolean isCellEditable(int row,int col){
return true;
}
public void setValueAt(Object value,int row,int col){
p[row][col] = value;
fireTableCellUpdated(row,col);//通知系統表格已經作為更改,注釋掉也沒有影響
}