[nbusers] Re: JTable issue

  • From: trhouse < >
  • To:
  • Subject: [nbusers] Re: JTable issue
  • Date: Sat, 27 Oct 2012 01:21:41 -0700

cut n paste rewrite below. YMMV.

can't  test it b/c I don't have the files.

Main sins you committed against Duke:
http://www.bloomberg.com/slideshow/2012-06-01/tech-mascots-tails-fails-and-hails.html#slide7

1) everything was a static method
2) methods were too long
3) methods functions were unfocused; did too many things.


Here's a couple tricks.

First thing to do is create an instance of your object (OPP1 here) in your PSVM and then make all methods instance methods you invoke on that object (opp1.blah() in this instance) . The problem with making everything static is everything has global scope and pretty soon you don't know what's changing what because anything can change anything any time. Everyone who isn't explicitly taught different starts programming this way, because, hey, it's easy to get at what you need..... ;)

Write the code you *wish* you had using methods that have great names and you *wish* actually existed. have them call each other so that, if they worked, you'd have a solution.

Then go through and write each method so it does what its name says it does- and only that (keep the method focused)

Little methods (two lines, four lines) that just do exactly what they say are so transparently correct that you know they work and you can ignore them once they're written.

Return what the next method needs to do it's work in the return statement of your method rather than setting global state if you can.

If you have to return more than one object , then use a return object (like pattern: parameter object but it's a return value) . Use inner classes to define those return objects.

Then Duke is happy and the things are Good.

note: OPP stands for "other people's problems"


HTH

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

/**
 *
 *
 */
import java.awt.BorderLayout;
import java.awt.HeadlessException;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
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(opp1, tableData);
        JFrame frame = opp1.createMainFrame();
        opp1.displayMainFrame(frame, table);
    }

    private JTable createTable(OPP1 opp1, List<String> tableData)
    {
        TableModel tableModel =opp1.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;
                //Read File Line By Line
                // int p=0;
                // Object[][] Table=null;
                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;
                    }

                    listOfRetrievedStrings.add(line);

                    // Print the content on the console
                    //System.out.println (strLine);
                }


            }
        }
        catch (Exception e)
        {//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
        return listOfRetrievedStrings;
    }

    List<String> retrieveAllStringsForTable()
    {
        String path = "C:/Users/Nikica/Desktop/text files";
        List<String> listOfStringsFromAllFiles = new ArrayList<>(10);

        String files;
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();

        for (int i = 0; i < listOfFiles.length; i++)
        {
            if (listOfFiles[i].isFile())
            {
                files = listOfFiles[i].getName();
                if (files.endsWith(".txt") || files.endsWith(".TXT"))
                {

                    System.out.println(files);

listOfStringsFromAllFiles.addAll(retrieveAllStringsInFile(folder, files));
                }
            }

        }
        return listOfStringsFromAllFiles;
    }



    private TableModel createTableModel(List<String> tableData)
    {
        int numberOfLinesReadFromAllFiles = tableData.size();
        Object rowData[][] = new Object[numberOfLinesReadFromAllFiles][2];

        String nextLine;
        int  ID =0;
        int RSSI =1;

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

        }


        Object columnNames[] =
        {
            "ID", "RSSI"
        };

        return new DefaultTableModel(rowData, columnNames);

    }
}



[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 20140418.2d69abc). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close