[nbusers] Re: JTable issue

  • From: trhouse < >
  • To:
  • Subject: [nbusers] Re: JTable issue
  • Date: Sat, 27 Oct 2012 16:29:18 -0700

Prettier, better rationalized, cruft removed and all that jazz....


cut n paste version below pretty HTL version attached.

and if it all blows up... *shrugs* ...meh.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package OPP;

/**
 *
 *
 */
import java.awt.HeadlessException;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class OPP1
{

    public static void main(String[] args)
    {
        OPP1 opp1 = new OPP1();
        List<String> tableData = opp1.retrieveAllStringsForTable();
        JTable table = opp1.createTable(tableData);
        JFrame frame = opp1.createMainFrame();
        opp1.displayMainFrame(frame, table);
    }

    private JTable createTable(List<String> tableData)
    {
        TableModel tableModel = createTableModel(tableData);
        JTable table = new JTable(tableModel);

        return table;
    }

    private JFrame createMainFrame() throws HeadlessException
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        return frame;
    }

    private void displayMainFrame(JFrame frame, JTable table)
    {
        frame.add(table);
        frame.setVisible(true);

    }

private List<String> retrieveAllStringsInFile(File folder, String files)
    {

        List<String> listOfRetrievedStrings = new ArrayList<String>(10);
File textFile = new File(folder.getAbsolutePath() + File.separator + files);
        try
        {
            // Open the file that is the first
            // command line parameter
            FileInputStream fstream = new FileInputStream(textFile);
            try (DataInputStream in = new DataInputStream(fstream))
            {
BufferedReader br = new BufferedReader(new InputStreamReader(in));

                String line;

                while ((line = br.readLine()) != null)
                {
if (line.startsWith("17/10/2012 10:00:06.67 [RX] - E usbR<LF>") || line.startsWith("E qEnd<LF>"))
                    {
                        continue;
                    }//if

                    listOfRetrievedStrings.add(line);
                }//while

            }//try
        }//try
        catch (Exception e)
        {
            System.err.println("Error: " + e.getMessage());
        }//catch

        return listOfRetrievedStrings;
    }// retrieveAllStringsInFile

private List<String>addAllFileStringsToList(File[] listOfFilesInFolder, File folder)
    {
        List<String> listOfStringsFromAllFiles = new ArrayList<>(10);
        String files;
        for (int i = 0; i < listOfFilesInFolder.length; i++)
        {
            if (listOfFilesInFolder[i].isFile())
            {
                files = listOfFilesInFolder[i].getName();
                if (files.endsWith(".txt") || files.endsWith(".TXT"))
                {
listOfStringsFromAllFiles.addAll(retrieveAllStringsInFile(folder, files));
                }//if
            }//if
        }//for

        return listOfStringsFromAllFiles;
    }//addAllFileStringsToList

    private File getFolderContainingFilesToBeProcessed()
    {
        String path = "C:/Users/Nikica/Desktop/text files";
        File folder = new File(path);

        return folder;
    }

    private Object[][] createRowData(List<String> tableData)
    {
        int numberOfLinesReadFromAllFiles = tableData.size();
        int numberOfColumns = 2;

Object rowData[][] = new Object[numberOfLinesReadFromAllFiles][numberOfColumns];


putRawDataIntoTableRows(numberOfLinesReadFromAllFiles, tableData, rowData);

        return rowData;
    }

    private Object[] createColumnNames()
    {
        Object columnNames[] =
        {
            "ID", "RSSI"
        };
        return columnNames;
    }

private void putRawDataIntoTableRows(int numberOfLinesReadFromAllFiles, List<String> tableData, Object[][] rowData)
    {
        int ID = 0;
        int RSSI = 1;
        String nextLine;


        for (int i = 0; i < numberOfLinesReadFromAllFiles; i++)
        {
            nextLine = tableData.get(i);
            rowData[i][ID] = nextLine.substring(0, 12);
            rowData[i][RSSI] = nextLine.substring(13, 15);

        }
    }

    private List<String> retrieveAllStringsForTable()
    {
        File folder = getFolderContainingFilesToBeProcessed();
        File[] listOfFilesInFolder = folder.listFiles();

        return addAllFileStringsToList(listOfFilesInFolder, folder);
    }

    private TableModel createTableModel(List<String> tableData)
    {
        Object[][] rowData = createRowData(tableData);
        Object columnNames[] =createColumnNames();

        return new DefaultTableModel(rowData, columnNames);

    }
}

Attachment: OPP1.html
Description: HTML document



[nbusers] JTable issue

nikicamk 10/24/2012

[nbusers] Re: JTable issue

Chuck Davis 10/25/2012

[nbusers] Re: JTable issue

Chuck Davis 10/25/2012

[nbusers] JTable issue

nikicamk 10/26/2012

[nbusers] Re: JTable issue

Chuck Davis 10/27/2012

[nbusers] Re: JTable issue

trhouse 10/27/2012

[nbusers] Re: JTable issue

trhouse 10/27/2012

[nbusers] JTable issue

nikicamk 10/29/2012

Project Features

About this Project

www was started in November 2009, is owned by jpirek, and has 21 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20131025.e7cbc9d). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close