JTable背景色與Boolean類型數據顯示衝突的問題

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(1));
		table.setPreferredScrollableViewportSize(new Dimension(500,300));
		//設置行列的選擇模式
		table.setColumnSelectionAllowed(true);
		table.setRowSelectionAllowed(true);
		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(1));
		}
		if(e.getSource()==button2){
			table.setModel(new MyTableModel(2));
		}
		//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 = 1;
	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){
		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;
			}
		}
		 return   super.getTableCellRendererComponent(table,   value,isSelected,   hasFocus,   row,   column);
	}
	
}


這樣運行時,boolean類型數據是以JCheckBox來顯示的,可是如果把"第一處"與"第二處"都打開的話,boolean類型數據就用true false來顯示了,,請問怎么解決?
大家看到的話就幫我下忙啊。。。

你可能感兴趣的:(swing)