日期:2014-05-20  浏览次数:20825 次

请教一个关于JTable中boolean列的问题,
我想写个选课系统,很简单的那种啊,用了一个表格来包含课程信息,最后一列就是是否选择该课程的一个boolean列,在勾选完自己想选的课程之后就按提交按钮,选择的课程信息会出现在另一个面板上的JTextArea上,可是那个那个getValueAt函数总是报错,不知原因啊,我把我写的代码发一下啊



import javax.swing.*;
import javax.swing.table.AbstractTableModel;

import java.awt.*;
import java.awt.event.*;

public class test implements ActionListener{
JTable table=null;
MyTable mt=null;
JTextArea ta=null;
JFrame f=null;
Container contentPane=null;
JButton b1=null;
JButton b2=null;
public test(){
f=new JFrame();
contentPane = f.getContentPane();
contentPane.setLayout(new CardLayout());

mt=new MyTable();
table = new JTable(mt);
table.setPreferredScrollableViewportSize(new Dimension(550,90));
JScrollPane s=new JScrollPane(table);

JPanel p1=new JPanel();
p1.setLayout(new GridLayout(2,1));
JPanel p11=new JPanel();
p1.add(s);
b1=new JButton("tijiao");
b1.addActionListener(this);
p11.add(b1,BorderLayout.CENTER);
p1.add(p11);

JPanel p2=new JPanel();
ta=new JTextArea("yixuan"+"\n");
ta.setSize(550, 30);
p2.setLayout(new GridLayout(2,1));
p2.add(ta);
JPanel p21=new JPanel();
b2=new JButton("queding");
b2.addActionListener(this);
p21.add(b2,BorderLayout.CENTER);
p2.add(p21);

contentPane.add("one",p1);contentPane.add("two",p2);
((CardLayout)contentPane.getLayout()).show(contentPane, "one");

f.setTitle("bixiu");
f.pack();
f.setVisible(true);

f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
for(int i=1;i<5;i++)
{
//boolean choice=(Boolean) mt.getValueAt(i, 3);
boolean choice=Boolean.parseBoolean(""+mt.getValueAt(i,3));
if(choice)
{
ta.append("kecheng: "+mt.getValueAt(i, 1)+"\t"+"xuefen: "+mt.getValueAt(i, 2));

}
}
((CardLayout)contentPane.getLayout()).show(contentPane,"two");
}
if(e.getSource()==b2){

}
table.revalidate();
f.validate();
}
public static void main(String args[]){
new test();
}
}
class MyTable extends AbstractTableModel{
/**

*/
private static final long serialVersionUID = 1L;
Object[][]p={
{"math",new Integer(6),new Boolean(false)},
{"C++",new Integer(5),new Boolean(false)},
{"Chinese",new Integer(3),new Boolean(false)},
{"English",new Integer(4),new Boolean(false)},
};
String[]n={"Name","XueFen","Choose"};
Class columnsClass[]={String.class,Integer.class,Boolean.class};
public int getColumnCount(){
return n.length;
}
public int getRowCount(){
return p.length;
}
public String getColumnName(int col){
return n[col];
}
public Object getValueAt(int row,int col){
return p[row][col];
}

public Class<?> getColumnClass(int c){
return columnsClass[c];
}
public boolean isCellEditable(int rowIndex,int columnIndex){
return true;
}
public void setValueAt(Object value,int row,int col){
p[row][col]=value;
fireTableCellUpdated(row,col);
}

}

------解决方案--------------------
已经改好了,改的地方给你标记了一下
Java code


import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.*;

public class Test implements ActionListener{
    JTable table=null;
    MyTable mt=null;
    JTextArea ta=null;
    JFrame f=null;
    Container contentPane=null;
    JButton b1=null;
    JButton b2=null;
    public Test(){
        f=new JFrame();
        contentPane = f.getContentPane();
        contentPane.setLayout(new CardLayout());

        mt=new MyTable();
        table = new JTable(mt);
        table.setPreferredScrollableViewportSize(new Dimension(550,90));
        JScrollPane s=new JScrollPane(table);

        JPanel p1=new JPanel();
        p1.setLayout(new GridLayout(2,1));
        JPanel p11=new JPanel();
        p1.add(s);
        b1=new JButton("提交");
        b1.addActionListener(this);
        p11.add(b1,BorderLayout.CENTER);
        p1.add(p11);

        JPanel p2=new JPanel();
        ta=new JTextArea("已选"+"\n");
        ta.setSize(550, 30);
        p2.setLayout(new GridLayout(2,1));
        p2.add(ta);
        JPanel p21=new JPanel();
        b2=new JButton("确定");
        b2.addActionListener(this);
        p21.add(b2,BorderLayout.CENTER);
        p2.add(p21);

        contentPane.add("one",p1);contentPane.add("two",p2);
        ((CardLayout)contentPane.getLayout()).show(contentPane, "one");

        f.setTitle("bixiu");
        f.pack();
        f.setVisible(true);

        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
    }
    @Override
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==b1){//提交按钮
            for(int i=0;i<table.getRowCount();i++){   //这里修改了
                //boolean choice=(Boolean) mt.getValueAt(i, 3);
                boolean choice=Boolean.parseBoolean(""+mt.getValueAt(i,2));
                if(choice){
                    //下面这行修改了
                    ta.append("课程: "+mt.getValueAt(i, 0)+"\t"+"学分: "+mt.getValueAt(i, 1));

                }
            }
            ((CardLayout)contentPane.getLayout()).show(contentPane,"two");
        }
        if(e.getSource()==b2){

        }
        table.revalidate();
        f.validate();
    }
    public static void main(String args[]){
        new Test();
    }
}
class MyTable extends AbstractTableModel{
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    Object[][]p={
            {"math",new Integer(6),new Boolean(false)},
            {"C++",new Integer(5),new Boolean(false)},
            {"Chinese",new Integer(3),new Boolean(false)},
            {"English",new Integer(4),new Boolean(false)},
    };
    String[]n={"Name","XueFen","Choose"};
    Class columnsClass[]={String.class,Integer.class,Boolean.class};
    public int getColumnCount(){
        return n.length;
    }
    public int getRowCount(){
        return p.length;
    }
    public String getColumnName(int col){
        return n[col];
    }
    public Object getValueAt(int row,int col){
        return p[row][col];
    }

    public Class<?> getColumnClass(int c){
        return columnsClass[c];
    }
    public boolean isCellEditable(int rowIndex,int columnIndex){
        return true;
    }
    public void setValueAt(Object value,int row,int col){
        p[row][col]=value;
        fireTableCellUpdated(row,col);
    }

}