A quick Question:
I'm trying to import some information from a CSV into a jtable that is located on a swing panel in my program.
I've imported the information into the actionListener using inputstream and parsed through it with bufferedreader. I just used a simple while loop afterwards to add to the Jtable. however, when I run the app, no information shows up in my jtable.
When I try to debug the program, I have no issues toggling between the two jpanels (and the other information that is on them) its just my jtable that shows up empty.
Here's my code:
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);
}
}
A few problems with your posted code:
Suggestions:
For example, your code changed to an MCVE that works:
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.