JTableバックグラウンドカラーとBooleanタイプのデータ表示が競合する問題
4141 ワード
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で表示されますが、「第1箇所」と「第2箇所」を両方開くと、booleanタイプデータはtrue falseで表示されますが、どうすればいいですか?
皆さんが見たら手伝ってください...