当前位置: 代码迷 >> J2SE >> 小弟我写了一个java代码,里面有两个有关问题,1、JTable的数据无法更新;2、JTable的列名无法显示就。还请高人指点
  详细解决方案

小弟我写了一个java代码,里面有两个有关问题,1、JTable的数据无法更新;2、JTable的列名无法显示就。还请高人指点

热度:2714   发布时间:2013-02-25 00:00:00.0
我写了一个java代码,里面有两个问题,1、JTable的数据无法更新;2、JTable的列名无法显示就。还请高人指点
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;
class Student extends JScrollPane implements ActionListener
{
Image student;
JLabel jl;
Toolkit tool;
File file;
JMenuItem first,second;
Student()
{
setLayout(null);
Icon i=new ImageIcon("manager.gif");
jl=new JLabel("",i,SwingConstants.CENTER);
jl.setBounds(0,0,1000,200);
add(jl);
first=new JMenuItem("第一学期");
first.addActionListener(this);
second=new JMenuItem("第二学期");
second.addActionListener(this);
JMenu menu=new JMenu("成绩查询");
menu.add(first);
menu.add(second);
JMenuBar bar=new JMenuBar();
bar.setBounds(0,200,1000,30);
tool=getToolkit();
student=tool.getImage("student.gif");
bar.add(menu);
add(bar);
validate();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(student,0,0,getWidth(),getHeight(),this);
}
public void actionPerformed(ActionEvent ae)
{
try
{
File file=new File("first");
if(ae.getSource()==first)
{
file=new File("first.txt");
}
if(ae.getSource()==second)
{
file=new File("second.txt");
}
MyTableModel m=new MyTableModel(file);m.fireTableCellUpdated(2,5);
JTable table=new JTable(m);
table.setPreferredScrollableViewportSize(new Dimension(500,70));
table.setBounds(0,250,500,100);
add(table);
validate();
}
catch(Exception ex){}
}
}
public class test extends JFrame
{
Container c;
Student s;
test()
{
c=getContentPane();
c.setLayout(new BorderLayout());
s=new Student();
c.add(s,"Center");
setSize(1000,700);
validate();
setVisible(true);
}
public static void main(String args[])
{
new test();
}
}
class MyTableModel extends AbstractTableModel
{
File file;
FileReader reader;
BufferedReader b_reader;
Object[][] data=new Object[1][5];
public MyTableModel(File file)
{
try
{
this.file=file;
reader=new FileReader(file);
b_reader=new BufferedReader(reader);
String s=b_reader.readLine();
StringTokenizer st=new StringTokenizer(s,",");
int i=0;
while(st.hasMoreTokens())
{
String str=st.nextToken();
data[0][i]=str;
i++;
}
}
catch(Exception ex){}
}
final String[] columnNames={"学号","姓名","java","英语","高数"};
public int getColumnCount()
{
return columnNames.length;
}
public int getRowCount()
{
return data.length;
}
public String getColumnName(int col)
{
return columnNames[col];
}
public Object getValueAt(int row,int col)
{
return data[row][col];
}
public Class getColumnClass(int c)
{
return getValueAt(0,c).getClass();
}
public boolean isCellEditable(int row,int col)
{
if(col<2)
{
return false;
}
else
{
return true;
}
}
public void setValueAt(Object value,int row,int col)
{
if(data[0][col] instanceof Integer&&!(value instanceof Integer))
{
try
{
data[row][col]=new Integer(value.toString());
fireTableCellUpdated(row,col);
}
catch(NumberFormatException e)
{
}
}
else
{
data[row][col]=value;
fireTableCellUpdated(row,col);
  相关解决方案