将CSV文件加载到JTable中
我的代码:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JTable;
import java.io.*;
import java.util.*;
import java.awt.Color;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.Font;
import java.io.*;
import java.util.*;
import java.awt.Color;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.Font;
public class students extends JFrame {
private JPanel contentPane;
private JTable table;
private DefaultTableModel m;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
students frame = new students();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public students() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1280, 740);
contentPane = new JPanel();
contentPane.setBackground(new Color(24,24,24));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
studentTable();
System.out.println("students();");
}
public void studentTable() {
try {
System.out.println("studentTable();");
String datafile = "data.txt";
FileReader fin = new FileReader(datafile);
DefaultTableModel m = createTableModel(fin, null);
JPanel panel = new JPanel();
panel.setBackground(new Color(35,35,35));
panel.setBounds(240, 163, 800, 360);
contentPane.add(panel);
panel.setLayout(null);
table = new JTable(m);
table.setBounds(0, 0, 800, 360);
table.getTableHeader().setFont(new Font("Arial", Font.BOLD, 12));
table.getTableHeader().setOpaque(false);
table.setBackground(new Color(35, 35, 35));
table.getTableHeader().setBackground(new Color(35,35,35));
table.setForeground(new Color(255,255,255));
table.getTableHeader().setForeground(new Color(255,255,255));
table.setRowHeight(25);
table.setFocusable(false);
table.setIntercellSpacing(new java.awt.Dimension(0, 0));
table.setRowHeight(25);
table.setSelectionBackground(new Color(32, 32, 32));
table.setShowVerticalLines(false);
table.getTableHeader().setReorderingAllowed(false);
FileWriter out = new FileWriter("data.csv");
defaultTableModelToStream(m, out);
out.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void defaultTableModelToStream(DefaultTableModel dtm,
Writer out) throws IOException {
System.out.println("defaultTableModelToStream();");
final String LINE_SEP = System.getProperty("line.separator");
int numCols = dtm.getColumnCount();
int numRows = dtm.getRowCount();
// Write headers
String sep = "";
for (int i = 0; i < numCols; i++) {
out.write(sep);
out.write(dtm.getColumnName(i));
sep = ",";
}
out.write(LINE_SEP);
for (int r = 0; r < numRows; r++) {
sep = "";
for (int c = 0; c < numCols; c++) {
out.write(sep);
out.write(dtm.getValueAt(r, c).toString());
sep = ",";
}
out.write(LINE_SEP);
}
}
public static DefaultTableModel createTableModel(Reader in,
Vector<Object> headers) {
System.out.println("DefaultTableModel();");
DefaultTableModel model = null;
Scanner s = null;
try {
Vector<Vector<Object>> rows = new Vector<Vector<Object>>();
s = new Scanner(in);
while (s.hasNextLine()) {
rows.add(new Vector<Object>(Arrays.asList(s.nextLine()
.split("\\s*,\\s*",
-1))));
}
if (headers == null) {
headers = rows.remove(0);
model = new DefaultTableModel(rows, headers);
} else {
model = new DefaultTableModel(rows, headers);
}
return model;
} finally {
s.close();
}
}
}
发生了什么: 该表不会加载,也不会加载“ data.txt”。 执行令是: 并且在控制台中没有崩溃或错误。 找到了文本文件。
应该发生的事情 jtable应该在jpanel上,并从文本文件中提取数据并加载它。
我试图自己修复它,但没有解决。也许有人会有解决方案。谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的答案是,您永远不会将
jtable
添加到任何长期答案中,更复杂。
null
布局(Pixel Perfect Layouts)是现代UI开发的幻想,对于许多因素,确定和维护组件的大小和关系供您浪费时间,以浪费时间来重塑已经,轻松,可用的东西给你。取而代之的是,花时间阅读并学习如何使用布局管理器。
您的
学生
课程做得太多(并且很难阅读和推理),它应该专注于做一项工作并做得好,即显示数据表。也没有理由从jframe
延伸,您没有在课程中添加任何新功能,而只是使其比需要的更复杂。取而代之的是,从一个简单的容器类开始,例如jpanel
...现在,面板不在乎数据来自何处,只是它负责显示提供的模型。
将数据的加载/节省分开为自己的类(看看 出于原因),并确保您正确管理资源,请参见 try-with-with-resources语句例如,有关如何实现这一目标的更多详细信息,例如……
当您准备就绪时,只需加载数据并显示面板...
可运行的 面板...示例...
The short answer is, you never add the
JTable
to anythingThe long answer is, well, a lot more complicated.
null
layouts (pixel perfect layouts) are an illusion in modern UI development, to many factors go into determine and maintaining the size and relationships of components for you to waste your time reinventing what is already, easily, available to you.Instead, take the time read and learn how to use layout managers.
Your
students
class is doing too much work (and is very hard to read and reason about), it should focus on doing one job and doing it well, that is, displaying the table of data. There's also no reason to extend fromJFrame
, you're not adding any new functionality to the class and are just making it more complex than it needs to be. Instead, start with a simple container class, likeJPanel
, for example...Now, the panel doesn't care where the data comes from, only that it is responsible for displaying the supplied model.
Seperate the loading/saving of the data to it's own class (take a look at Single Responsibility Principle for the reasons why) and make sure that you are managing your resources correctly, see The try-with-resources Statement for more details on how this might be achieved, for example...
And when you're ready, simply load the data and display the panel...
Runnable example...