Java j表格显示值错误/未显示整个值
我有一个从另一个程序生成的文件,我正在尝试创建一个不同的程序,该程序将创建一个包含文件的JTable。我已经尝试使用StackOverflow教程来修复它,但我不知道出了什么问题。我使用了一个教程来帮助我开始创建JTable,所以我甚至不知道它哪里出错了 这是文件的内容 使用正确的文件输入进行编辑Java j表格显示值错误/未显示整个值,java,swing,file-io,jtable,Java,Swing,File Io,Jtable,我有一个从另一个程序生成的文件,我正在尝试创建一个不同的程序,该程序将创建一个包含文件的JTable。我已经尝试使用StackOverflow教程来修复它,但我不知道出了什么问题。我使用了一个教程来帮助我开始创建JTable,所以我甚至不知道它哪里出错了 这是文件的内容 使用正确的文件输入进行编辑 5|10.540000| 12.181217| 4482.000000| 109.155482| 12.020000| 8.563800| 4834.000000| 56.715967| 10
5|10.540000| 12.181217| 4482.000000| 109.155482| 12.020000| 8.563800| 4834.000000| 56.715967|
10|29.480000| 8.425604| 50772.000000| 563.527552| 33.000000| 5.844637| 4310.000000| 111.075842|
50|259.120000| 2.885537| 21648.000000| 114.723483| 267.920000| 1.648783| 17158.000000| 56.444652|
100|624.700000| 2.039771| 13300.000000| 21.205140| 633.140000| 1.202862| 15864.000000| 36.446637|
250|1887.700000| 1.377592| 41134.000000| 22.781148| 1896.880000| 0.693851| 41924.000000| 45.683431|
500|4291.800000| 0.836839| 191150.000000| 79.367962| 4294.560000| 0.457202| 80876.000000| 86.643983|
1000|9598.000000| 0.436951| 244238.000000| 149.945987| 9585.120000| 0.266139| 219474.000000| 184.652819|
10000|129582.240000| 0.129851| 1219346.000000| 9.823825| 129214.380000| 0.053029| 1410626.000000| 6.414845|
50000|764303.200000| 0.042775| 8392266.000000| 29.384328| 762430.500000| 0.024571| 8935228.000000| 14.952530|
100000|1628728.920000| 0.029737| 18302506.000000| 18.204143| 1624946.720000| 0.016495| 22086830.000000| 23.866814|
这是填充值的代码部分:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String filePath = "C:\\Users\\hafiz\\OneDrive\\Documents\\test\\file.txt";
File file = new File(filePath);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String[] columnNames = {
"Size", "Iterative Avg Count", "Iterative Coef Count",
"Iterative Avg Time", "Iterative Coef Time", "Recurive Avg Count",
"Recurive Coef Count", "Recurive Avg Time", "Recurive Coef Time"
};
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.setColumnIdentifiers(columnNames);
Object[] tableLines = br.lines().toArray();
for (int i = 0; i < tableLines.length; i++) {
String line = tableLines[i].toString().trim();
String[] dataRow = line.split("|");
model.addRow(dataRow);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Table.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
String filePath=“C:\\Users\\hafiz\\OneDrive\\Documents\\test\\file.txt”;
文件文件=新文件(文件路径);
试一试{
BufferedReader br=新的BufferedReader(新文件读取器(文件));
字符串[]列名称={
“大小”、“迭代平均计数”、“迭代系数计数”,
“迭代平均时间”、“迭代系数时间”、“重现平均计数”,
“恢复系数计数”、“恢复平均时间”、“恢复系数时间”
};
DefaultTableModel=(DefaultTableModel)jTable1.getModel();
model.setColumnIdentifiers(columnNames);
对象[]tableLines=br.lines().toArray();
对于(int i=0;i
如果有任何用途,这就是全部代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author hafiz
*/
public class Table extends javax.swing.JFrame {
/**
* Creates new form Table
*/
public Table() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
}
));
jScrollPane1.setViewportView(jTable1);
jButton2.setText("Import Text");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 745, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGap(331, 331, 331)
.addComponent(jButton2)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String filePath = "C:\\Users\\hafiz\\OneDrive\\Documents\\test\\file.txt";
File file = new File(filePath);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String[] columnNames = {
"Size", "Iterative Avg Count", "Iterative Coef Count",
"Iterative Avg Time", "Iterative Coef Time", "Recurive Avg Count",
"Recurive Coef Count", "Recurive Avg Time", "Recurive Coef Time"
};
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
model.setColumnIdentifiers(columnNames);
Object[] tableLines = br.lines().toArray();
for (int i = 0; i < tableLines.length; i++) {
String line = tableLines[i].toString().trim();
String[] dataRow = line.split("|");
model.addRow(dataRow);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Table.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Table.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Table.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Table.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Table.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Table().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.swing.table.DefaultTableModel;
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
/**
*
*@作者哈菲兹
*/
公共类表扩展了javax.swing.JFrame{
/**
*创建新的表单表
*/
公共表(){
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
jScrollPane1=newjavax.swing.JScrollPane();
jTable1=newjavax.swing.JTable();
jButton2=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setModel(新的javax.swing.table.DefaultTableModel(
新对象[][]{
},
新字符串[]{
}
));
jScrollPane1.setViewportView(jTable1);
jButton2.setText(“导入文本”);
jButton2.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1,javax.swing.GroupLayout.Alignment.TRAILING,javax.swing.GroupLayout.DEFAULT\u SIZE,745,Short.MAX\u值)
.addGroup(layout.createSequentialGroup()
.addGap(331、331、331)
.addComponent(jButton2)
.addContainerGap(javax.swing.GroupLayout.DEFAULT\u SIZE,Short.MAX\u VALUE))
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,8,Short.MAX_值)
.addComponent(jScrollPane1,javax.swing.GroupLayout.PREFERRED_SIZE,269,javax.swing.GroupLayout.PREFERRED_SIZE))
);
包装();
}//
私有void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
String filePath=“C:\\Users\\hafiz\\OneDrive\\Documents\\test\\file.txt”;
文件文件=新文件(文件路径);
试一试{
BufferedReader br=新的BufferedReader(新文件读取器(文件));
字符串[]列名称={
“大小”、“迭代平均计数”、“迭代系数计数”,
“迭代平均时间”、“迭代系数时间”、“重现平均计数”,
“恢复系数计数”、“恢复平均时间”、“恢复系数时间”
};
DefaultTableModel=(DefaultTableModel)jTable1.getModel();
model.setColumnIdentifiers(columnNames);
对象[]tableLines=br.lines().toArray();
对于(int i=0;iString[] dataRow = line.split("\\|");