Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

java - Displaying whatever has been read from a file in a GUI

So, my data are stored in a file, called log.txt, and I want to view the content of it, in GUI. So i've got these two classes which i'm using, one is Engine, where I read the log.txt file, and the other one is GUI, where is used to apply the methods used in Engine.

So, in my engine, I've got these codes:

public void loadLog()
    {
        try
        {
            java.io.File cpFile = new java.io.File( "log.txt" );

            if ( cpFile.exists() == true )
            {
                File file = cpFile;

                FileInputStream fis = null;
                BufferedInputStream bis = null;
                DataInputStream dis = null;

                String strLine="";
                String logPrint="";

                fis = new FileInputStream ( file );

                // Here is BufferedInputStream added for fast reading
                bis = new BufferedInputStream ( fis );
                dis = new DataInputStream ( bis );

                // New Buffer Reader
                BufferedReader br = new BufferedReader( new InputStreamReader( fis ) );

                while ( ( strLine = br.readLine() ) != null )
                {
                    StringTokenizer st = new StringTokenizer ( strLine, ";" );

                    while ( st.hasMoreTokens() )
                    {
                        logPrint       = st.nextToken();
                        System.out.println(logPrint);

                    }

                    log = new Log();
                    //regis.addLog( log );




                }
            }

        }
        catch ( Exception e ){
        }
    }

and then, in my GUI, I'd try to apply the codes used in my engine:

                    // create exit menu
        Logout = new JMenu("Exit");

        // create JMenuItem for about menu
        reportItem   = new JMenuItem ( "Report" );

        // add about menu to menuBar
        menuBar.add ( Logout );
        menuBar.setBorder ( new BevelBorder(BevelBorder.RAISED) );

        Logout.add ( reportItem );

    /* --------------------------------- ACTION LISTENER FOR ABOUT MENU ------------------------------------------ */

        reportItem.addActionListener ( new ActionListener()
        {
            public void actionPerformed ( ActionEvent e )
            {

                    engine.loadLog();
                    mainPanel.setVisible (false);
                    mainPanel = home();
                    toolBar.setVisible(false);
                    vasToolBar.setVisible(false);
                    cpToolBar.setVisible(false);
                    add ( mainPanel, BorderLayout.CENTER );
                    add ( toolBar, BorderLayout.NORTH );
                    toolBar.setVisible(false);
                    mainPanel.setVisible ( true );
                    pack();
                    setSize(500, 500);
            }
        });

NOW,

my question is, how can I print out whatever is read in my Engine's method, inside the GUI part? I want them to be either inside a JLabel or JTextArea. How am I supposed to do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

File IO operations are considered blocking/time consuming.

You should avoid running them in the Event Dispatching Thread, as this will prevent the UI from begin updated, making your application look like its hung/crashed

You could use a SwingWorker to perform the file loading part, passing each line to the publish method and adding the lines to the text area via the process method...

public class FileReaderWorker extends SwingWorker<List<String>, String> {

    private final File inFile;
    private final JTextArea output;

    public FileReaderWorker(File file, JTextArea output) {
        inFile = file;
        this.output = output;
    }

    public File getInFile() {
        return inFile;
    }

    public JTextArea getOutput() {
        return output;
    }

    @Override
    protected void process(List<String> chunks) {
        for (String line : chunks) {
            output.append(line);
        }
    }

    @Override
    protected List<String> doInBackground() throws Exception {
        List<String> lines = new ArrayList<String>(25);
        java.io.File cpFile = getInFile();
        if (cpFile != null && cpFile.exists() == true) {
            File file = cpFile;

            BufferedReader br = null;

            String strLine = "";
            String logPrint = "";
            try {
                // New Buffer Reader
                br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
                while ((strLine = br.readLine()) != null) {
                    StringTokenizer st = new StringTokenizer(strLine, ";");
                    while (st.hasMoreTokens()) {
                        logPrint = st.nextToken();
                        publish(logPrint);
                    }
                }
            } catch (Exception e) {
                publish("Failed read in file: " + e);
                e.printStackTrace();
            } finally {
                try {
                    if (br != null) {
                        br.close();
                    }
                } catch (Exception e) {
                }
            }
        } else {
            publish("Input file does not exist/hasn't not begin specified");
        }            
        return lines;
    }
}

Take a look at Lesson: Concurrency in Swing for more information


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...