我需要从scores.dat
文件中读取数据并将其弹出到JTable
,这里有更多信息:
Scores.dat
看起来像
Team Name Score1 Score2
Red John 55 7
Blue Michael 33 6
Green Burrs 55 5
location = C:\Documents\scores.dat
第一行是列名。因此需要将列名称弹出到表格的第一行。
Scores.dat
是动态的,用户操作添加/删除行。
另外,我需要一个按钮" Top Scorer"在表格下方,点击时,会突出显示得分最高的行。
这是我的代码:
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
/**
*
* @author singh_000
*/
public class SampleJTableDemo extends JFrame implements ActionListener {
/**
* Creates new form OrderMateRequestForm
*/
public SampleJTableDemo() {
initComponents();
}
private void initComponents() {
//headers for the table
String[] columns = new String[]{
"Team", "Name", "Score1", "Score2"//Todo: pop names from 1st row from scores.dat
};
//actual data for the table in a 2d array
Object[][] data = new Object[][]{
{"Red", "John", 40.0, 7},
{"Blue", "Rambo", 70.0, 6},
{"Gree", "Zorro", 60.0, 7},
{"Black", "Curran", 70.0, 5},};
final Class[] columnClass = new Class[]{
String.class, String.class, Double.class, Integer.class
};
//create table model with data
DefaultTableModel model = new DefaultTableModel(data, columns) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return columnClass[columnIndex];
}
};
JTable table = new JTable(model);
Container contentPane = this.getContentPane();
contentPane.setLayout(new FlowLayout());
// contentPane.add(table);
JScrollPane scrollPane = new JScrollPane(table);
contentPane.add(scrollPane);
JSeparator separator = new JSeparator();
separator.setVisible(true);
contentPane.add(separator);
//add the table to the frame
// this.add(new JScrollPane(table));
//ToDO: Add buton for top scorer
JButton button = new JButton("Top Scorer");
button.setVisible(true);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
this.setTitle("Score-List");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new SampleJTableDemo();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
答案 0 :(得分:0)
当我读第一行时,我试图设置Team参数但得到NumberFormat Exception,因为Team.setScore1(int)需要整数,而来自BufferReader的readLine()包含字符串(列名)
因此,当您阅读第一行时,您将数据解析为字符串
String[] columnNames = line.split(" ");
当您阅读其他行时,需要将String数据转换为正确的数据类型:
String[] lineData = theNextLine.split(" " );
String team = lineData[0];
String name = lineData[1];
Double score1 = Double.parseDouble( lineData[2] );
Integer score2 = Integer.parseInt( lineData[3] );
现在,您可以使用DefaultTableModel的addRow(...)方法将数据添加到表中。