【发布时间】:2017-05-11 07:30:46
【问题描述】:
我需要从scores.dat 文件中读取数据并将其弹出到JTable,这里有更多信息:
-
Scores.dat看起来像Team Name Score1 Score2 Red John 55 7 Blue Michael 33 6 Green Burrs 55 5位置 =
C:\Documents\scores.dat 第一行是列名。所以需要将列名弹出到表的第一行。
Scores.dat是动态的,通过用户操作添加/删除行。此外,我还需要表格下方的“最高得分者”按钮,单击该按钮时,会突出显示得分最高的行。
这是我的代码:
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) {
}
}
【问题讨论】:
-
欢迎来到 Stack Overflow!寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建 minimal reproducible example。具体来说:您不只是发布您的 assignment 和一些代码;然后期望我们阅读您的所有代码,以了解它与您的作业不匹配的地方。到目前为止,这读起来像:“这是我老师给我的模板。用代码填充它”。没有。
-
为了让人们更容易测试@GhostCat 建议的minimal reproducible example,假设数据已经被读入了
String和硬代码String中的MCVE。将数据限制为两个条目以保持简短。 -
顺便说一句 - “从 .dat 文件中读取并填充到 JTable 中?” - 您在哪个部分遇到问题,读取还是填充?如果是“两者”,那么您需要将问题分成两部分,并让每个部分分别工作。
-
@AndrewThompson 填充数据,因为 .dat 文件中的第一行是所有字符串(列名),后续行也包含整数。我使用 BufferReader 读取数据。虽然我创建了 Team 对象(参数名称、score1、score2)。当我阅读第一行时,我正在尝试设置 Team 参数,但由于 Team.setScore1(int) 需要整数,而来自 BufferReader 的 readLine() 包含字符串(列名)
-
请专注于一个问题。