一个简单的问题:
我正在尝试将CSV中的某些信息导入程序中旋转面板上的jtable中。
我已经使用inputstream将信息导入到actionListener中,并使用bufferedreader对其进行了解析。之后,我只是使用了一个简单的while循环来添加到Jtable中。但是,当我运行该应用程序时,我的jtable中没有显示任何信息。
当我尝试调试程序时,在两个jpanel(以及其中的其他信息)之间切换时,我没有问题,只是我的jtable显示为空。
这是我的代码:
public class wert extends JFrame {
private JPanel contentPane;
private JPanel panel;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
wert frame = new wert();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public wert() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 925, 486);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
Path filePath = Paths.get("\\test folder\\newNeetest.csv\\");
InputStream input = null;
input = Files.newInputStream(filePath);
BufferedReader reader = new BufferedReader (new InputStreamReader(input));
DefaultTableModel model = (DefaultTableModel)table.getModel();
Object [] lines = reader.lines().toArray();
for (int q =0; q > lines.length; q++) {
String dsa = lines[q].toString();
String [] dataRow = dsa.split(",");
model.addRow(dataRow);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
);
contentPane.add(btnNewButton, BorderLayout.NORTH);
panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
table = new JTable();
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(475)
.addComponent(table, GroupLayout.PREFERRED_SIZE, 230, GroupLayout.PREFERRED_SIZE)
.addContainerGap(188, Short.MAX_VALUE))
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
.addContainerGap()
.addComponent(table, GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE)
.addGap(375))
);
panel.setLayout(gl_panel);
}
}
答案 0 :(得分:3)
您发布的代码存在一些问题:
建议:
例如,您的代码更改为可以正常工作的MCVE:
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;
@SuppressWarnings("serial")
public class Wert2 extends JFrame {
private JPanel contentPane;
private JTable table;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Wert2 frame = new Wert2();
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Wert2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// file I/O code removed for sake of MCVE
DefaultTableModel model = (DefaultTableModel) table.getModel();
for (int i = 0; i < 10; i++) {
Vector<String> rowData = new Vector<>();
for (int j = 0; j < 3; j++) {
rowData.add(String.format("[%d, %d]", i, j));
}
model.addRow(rowData);
}
}
});
contentPane.add(btnNewButton, BorderLayout.NORTH);
// contentPane.add(panel, BorderLayout.CENTER);
String[] colNames = {"A", "B", "C"};
table = new JTable(new DefaultTableModel(colNames, 0));
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
contentPane.add(scrollPane);
}
}