JTable displaying values wrong/not displaying whole values

nbaq :

I have a file that's generated from another program, and I'm trying to create a different program that will create a JTable with files in it. I've already tried using StackOverflow tutorials to fix it, but I can't figure out what's wrong. I used a tutorial to help me create the JTable to begin with, so I can't even figure out where it went wrong.

This is the content of the file

EDITED WITH CORRECT FILE INPUT


 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| 

And this is the part of the code that fills the values:

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);
        }

    }                                        

This is the whole code if it comes of any use:


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                   
}

The first column (which is size) is empty for value records.

This is how the output looks right nowenter image description here

Lajos Arpad :

You have two main issues:

  1. Your input of |10.140000| 13.084819| 4392.000000| 82.699923| 11.900000| 10.118987| 5166.000000| 59.443868| starts with your separator, so the size column will be empty, because it will contain the substring left from the first | instance

  2. Your input has 8 values, but you have 9 columns, so at least a column will have no values.

EDIT

You also need to escape the | character, so you will need to do something like

String[] dataRow = line.split("\\|");

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=31365&siteId=1