Java-我需要在Swing GUI程序中更新JTextFields [英] Java - I need to update JTextFields in a Swing GUI program

查看:78
本文介绍了Java-我需要在Swing GUI程序中更新JTextFields的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序可以查看一个记事本文件,该文件包含类似于以下内容的信息:

Cardiff : 3245658

Bristol : 4726485

Manchester : 4728945

这些位置和数字来自自动系统,数字每20秒左右更改一次. 我需要创建一个循环(我猜是这样),以便当数字更改时,我的程序将回溯到记事本文件中并更新JTextField

中的数字

秒 我已经创建了程序,以便使用ArrayList

查找文件,获取信息需求并将其放置在正确的位置.

我需要帮助的是如何在文件发生更改时使代码自动更新,而我需要不使用任何Jbuttons来执行此操作,它只需要自己执行即可.

我的完整代码如下:

package window;

import java.awt.Color;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MapTest {

    private JFrame frame;
    static JTextField txtCardiff;
    static JTextField txtBristol;
    static JTextField txtSheffield;
    static JTextField txtDerby;
    static JTextField txtGlasgow;
    static JTextField txtFalkirk;
    static JTextField txtNewcastle;
    static JTextField txtLeeds;
    static JTextField txtPreston;
    static JTextField txtManchester;
    static JTextField txtWolverhampton;
    static JTextField txtBirmingham;
    static JTextField txtMiltonKeynes;
    static JTextField txtPeterborough;
    static JTextField txtColindale;
    static JTextField txtStepneyGreen;
    static JTextField txtSlough;
    static JTextField txtFaraday;
    static JTextField txtGuildford;
    static JTextField txtSouthbank;

    static BufferedReader CSVFile = new BufferedReader(new FileReader
        ("C:/Users/606854007/workspace/NetworkAppTest/jar/window/Cardiff.txt"));
// putting static infront of this Br ' CSVFile' gives: 
//Unhandled exception type FileNotFoundException. 
//but when i take away the 'static' it causes a problem in the 'FileCheckerWorker' class.
// (Will comment where and what)

    ArrayList<String> csv = new ArrayList<String>();

    /**
    private String output = "";
    private String output1 = "";
    private String output2 = "";
    private String output3 = "";
    private String output4 = "";
    private String output5 = "";
    private String output6 = "";
    private String output7 = "";
    private String output8 = "";
    private String output9 = "";
    private String output10 = "";
    private String output11 = "";
    private String output12 = "";
    private String output13 = "";
    private String output14 = "";
    private String output15 = "";
    private String output16 = "";
    private String output17 = "";
    private String output18 = "";
    private String output19 = "";
    **/
    /** * Launch the application. */
    public static void main(String[] args) {
         try {
                UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());

            } catch (ClassNotFoundException e1) {
                e1.printStackTrace();

            } catch (InstantiationException e1) {
                e1.printStackTrace();

            } catch (IllegalAccessException e1) {
                e1.printStackTrace();

            } catch (UnsupportedLookAndFeelException e1) {
                e1.printStackTrace();
            }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MapTest window = new MapTest();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    /**
     * Create the application.
     * 
     * @throws IOException
     */
    public MapTest() throws IOException {
        try {
            initialize();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * Initialise the contents of the frame.
     * 
     * @param output
     * @throws IOException
     */

    private void initialize() throws IOException {

                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());

                System.out.println(csv.get(0));
                System.out.println(csv.get(1));
                System.out.println(csv.get(2));
                System.out.println(csv.get(3));
                System.out.println(csv.get(4));
                System.out.println(csv.get(5));
                System.out.println(csv.get(6));
                System.out.println(csv.get(7));
                System.out.println(csv.get(8));
                System.out.println(csv.get(9));
                System.out.println(csv.get(10));
                System.out.println(csv.get(11));
                System.out.println(csv.get(12));
                System.out.println(csv.get(13));
                System.out.println(csv.get(14));
                System.out.println(csv.get(15));
                System.out.println(csv.get(16));
                System.out.println(csv.get(17));
                System.out.println(csv.get(18));
                System.out.println(csv.get(19));


        /**
         * JPanel container = new JPanel(); container.add(txtBirmingham);
         * container.add(txtBristol); container.add(txtCardiff);
         * container.add(txtColindale); container.add(txtDerby);
         * container.add(txtFalkirk); container.add(txtGlasgow);
         * container.add(txtGuildford); container.add(txtLeeds);
         * container.add(txtManchester); container.add(txtMiltonKeynes);
         * container.add(txtNewcastle); container.add(txtPeterborough);
         * container.add(txtPreston); container.add(txtSheffield);
         * container.add(txtSlough); container.add(txtSouthbank);
         * container.add(txtStepneyGreen); container.add(txtWolverhampton);
         * //container.add(); JScrollPane jsp = new JScrollPane(container);
         * frame.add(jsp);
         **/

        frame = new JFrame();
        frame.setBounds(0, 0, 1000, 1086);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setBackground(Color.CYAN);

        txtCardiff = new JTextField();
        txtCardiff.setText(csv.get(0));
        txtCardiff.setBackground(Color.GREEN);
        txtCardiff.setBounds(305, 699, 102, 20);
        frame.getContentPane().add(txtCardiff);
        txtCardiff.setColumns(10);

        txtBristol = new JTextField();
        txtBristol.setText(csv.get(1));
        txtBristol.setBackground(Color.GREEN);
        txtBristol.setBounds(416, 780, 102, 20);
        frame.getContentPane().add(txtBristol);
        txtBristol.setColumns(10);

        txtSheffield = new JTextField();
        txtSheffield.setText(csv.get(2));
        txtSheffield.setBackground(Color.GREEN);
        txtSheffield.setBounds(517, 356, 110, 20);
        frame.getContentPane().add(txtSheffield);
        txtSheffield.setColumns(10);

        txtDerby = new JTextField();
        txtDerby.setText(csv.get(3));
        txtDerby.setBackground(Color.GREEN);
        txtDerby.setBounds(582, 451, 90, 20);
        frame.getContentPane().add(txtDerby);
        txtDerby.setColumns(10);

        txtGlasgow = new JTextField();
        txtGlasgow.setText(csv.get(4));
        txtGlasgow.setBackground(Color.GREEN);
        txtGlasgow.setBounds(250, 94, 102, 20);
        frame.getContentPane().add(txtGlasgow);
        txtGlasgow.setColumns(10);

        txtFalkirk = new JTextField();
        txtFalkirk.setText(csv.get(5));
        txtFalkirk.setColumns(10);
        txtFalkirk.setBackground(Color.GREEN);
        txtFalkirk.setBounds(331, 41, 102, 20);
        frame.getContentPane().add(txtFalkirk);

        txtNewcastle = new JTextField();
        txtNewcastle.setText(csv.get(6));
        txtNewcastle.setColumns(10);
        txtNewcastle.setBackground(Color.GREEN);
        txtNewcastle.setBounds(424, 141, 111, 20);
        frame.getContentPane().add(txtNewcastle);

        txtLeeds = new JTextField();
        txtLeeds.setText(csv.get(7));
        txtLeeds.setColumns(10);
        txtLeeds.setBackground(Color.GREEN);
        txtLeeds.setBounds(481, 244, 102, 20);
        frame.getContentPane().add(txtLeeds);

        txtPreston = new JTextField();
        txtPreston.setText(csv.get(8));
        txtPreston.setColumns(10);
        txtPreston.setBackground(Color.GREEN);
        txtPreston.setBounds(356, 221, 102, 20);
        frame.getContentPane().add(txtPreston);

        txtManchester = new JTextField();
        txtManchester.setText(csv.get(9));
        txtManchester.setColumns(10);
        txtManchester.setBackground(Color.GREEN);
        txtManchester.setBounds(394, 394, 124, 20);
        frame.getContentPane().add(txtManchester);

        txtWolverhampton = new JTextField();
        txtWolverhampton.setText(csv.get(10));
        txtWolverhampton.setColumns(10);
        txtWolverhampton.setBackground(Color.GREEN);
        txtWolverhampton.setBounds(360, 486, 153, 20);
        frame.getContentPane().add(txtWolverhampton);

        txtBirmingham = new JTextField();
        txtBirmingham.setText(csv.get(11));
        txtBirmingham.setColumns(10);
        txtBirmingham.setBackground(Color.GREEN);
        txtBirmingham.setBounds(424, 553, 128, 20);
        frame.getContentPane().add(txtBirmingham);

        txtMiltonKeynes = new JTextField();
        txtMiltonKeynes.setText(csv.get(12));
        txtMiltonKeynes.setColumns(10);
        txtMiltonKeynes.setBackground(Color.GREEN);
        txtMiltonKeynes.setBounds(474, 604, 135, 20);
        frame.getContentPane().add(txtMiltonKeynes);

        txtPeterborough = new JTextField();
        txtPeterborough.setText(csv.get(13));
        txtPeterborough.setColumns(10);
        txtPeterborough.setBackground(Color.GREEN);
        txtPeterborough.setBounds(655, 576, 135, 20);
        frame.getContentPane().add(txtPeterborough);

        txtColindale = new JTextField();
        txtColindale.setText(csv.get(14));
        txtColindale.setColumns(10);
        txtColindale.setBackground(Color.GREEN);
        txtColindale.setBounds(541, 699, 107, 20);
        frame.getContentPane().add(txtColindale);

        txtStepneyGreen = new JTextField();
        txtStepneyGreen.setText(csv.get(15));
        txtStepneyGreen.setColumns(10);
        txtStepneyGreen.setBackground(Color.GREEN);
        txtStepneyGreen.setBounds(667, 699, 137, 20);
        frame.getContentPane().add(txtStepneyGreen);

        txtSlough = new JTextField();
        txtSlough.setText(csv.get(16));
        txtSlough.setColumns(10);
        txtSlough.setBackground(Color.GREEN);
        txtSlough.setBounds(525, 756, 102, 20);
        frame.getContentPane().add(txtSlough);

        txtFaraday = new JTextField();
        txtFaraday.setText(csv.get(17));
        txtFaraday.setColumns(10);
        txtFaraday.setBackground(Color.GREEN);
        txtFaraday.setBounds(650, 756, 102, 20);
        frame.getContentPane().add(txtFaraday);

        txtGuildford = new JTextField();
        txtGuildford.setText(csv.get(18));
        txtGuildford.setColumns(10);
        txtGuildford.setBackground(Color.GREEN);
        txtGuildford.setBounds(525, 822, 102, 20);
        frame.getContentPane().add(txtGuildford);

        txtSouthbank = new JTextField();
        txtSouthbank.setText(csv.get(19));
        txtSouthbank.setColumns(10);
        txtSouthbank.setBackground(Color.GREEN);
        txtSouthbank.setBounds(655, 807, 115, 20);
        frame.getContentPane().add(txtSouthbank);

        JLabel lblNewLabel = new JLabel("New label");
        lblNewLabel.setForeground(Color.BLACK);
        lblNewLabel.setBackground(Color.BLACK);
        lblNewLabel
                .setIcon(new ImageIcon(
                        "C:\\Users\\606854007\\workspace\\NetworkAppTest\\Test\\window\\England_Wales.gif"));
        lblNewLabel.setBounds(33, 11, 890, 1086);
        frame.getContentPane().add(lblNewLabel);

        new FileCheckerWorker(null).execute();
\\ if i dont but null in the '()' then it comes up with an error, 
\\and this just makes it execute:
    /**
public FileCheckerWorker(MapTest mapTest) {
        this.mapTest = mapTest;
    }
**/
\\ which is not what I want, I have tried to add commands 
\\to run the methods from within:
\\ ' public FileCheckerWorker(MapTest mapTest)' method
\\ but it gives me a null pointer exception,


}

    }

下面是'FileCheckerWorker'类,希望我做对了:p

package window;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import javax.swing.SwingWorker;


public class FileCheckerWorker extends SwingWorker<Object, List<String>> {

    private MapTest mapTest;

    public FileCheckerWorker(MapTest mapTest) {
        this.mapTest = mapTest;
    }


        @Override
        protected void process(List<List<String>> chunks) {
           List<String> values = chunks.get(chunks.size() - 1);

           mapTest.txtCardiff.setText(values.get(0));
           mapTest.txtBristol.setText(values.get(1));
           mapTest.txtSheffield.setText(values.get(2));
           mapTest.txtDerby.setText(values.get(3));
           mapTest.txtGlasgow.setText(values.get(4));
           mapTest.txtFalkirk.setText(values.get(5));
           mapTest.txtNewcastle.setText(values.get(6));
           mapTest.txtLeeds.setText(values.get(7));
           mapTest.txtPreston.setText(values.get(8));
           mapTest.txtManchester.setText(values.get(9));
           mapTest.txtWolverhampton.setText(values.get(10));
           mapTest.txtBirmingham.setText(values.get(11));
           mapTest.txtMiltonKeynes.setText(values.get(12));
           mapTest.txtPeterborough.setText(values.get(13));
           mapTest.txtColindale.setText(values.get(14));
           mapTest.txtStepneyGreen.setText(values.get(15));
           mapTest.txtSlough.setText(values.get(16));
           mapTest.txtFaraday.setText(values.get(17));
           mapTest.txtGuildford.setText(values.get(18));
           mapTest.txtSouthbank.setText(values.get(19));
        }

        @Override
        protected Object doInBackground() throws Exception {
            long lastUpdated = -1;
            File csvFile = new File("C:/Users/606854007/workspace/NetworkAppTest/Jar/window/Cardiff.txt");
            while (true) {
                if (csvFile.lastModified() != lastUpdated) {
                    BufferedReader csvReader = null;
                    List<String> values = new ArrayList<String>(20);
                    try {
                        csvReader = new BufferedReader(new FileReader("C:/Users/606854007/workspace/NetworkAppTest/Jar/window/Cardiff.txt"));
                        for (int index = 0; index < 20; index++) {
                            values.add(mapTest.CSVFile.readLine());
                        }
                        publish(values);
                        lastUpdated = csvFile.lastModified();
                    } finally {
                        try {
                            csvReader.close();
                        } catch (Exception e) {
                        }
                    }

                    Thread.sleep(5000);
                    System.out.println("Check For Change");
                }
            }
        }

}

更新后的两个代码都与现在的代码相同.

当我以为我在线上出现了NullPointerException时,我还以为'doInBackground'可以正常工作 values.add(mapTest.CSVFile.readLine());

解决方案

将查找"代码包装在SwingWorker中,使用Thread.sleep允许后台线程在给定的时间段内空闲(例如20秒)

加载文件的内容,确定文件是否已更改,然后使用pusblish方法将结果发送到process方法,该方法可让您使用事件分派线程处理结果

有关更多详细信息,请参见 SwingWorker

简单示例

public class PeriodicalWorker extends SwingWorker<Object, String> {
    private boolean running = true;

    public Object doInBackground() throws Exception {
        while (running) {
            try {
                Thread.sleep(20000);
            } catch (Exception exp) {}
            // Load the values...
            // Check to see if anything needs to updated...
            publish(values); // List of String array values
        }
        return null;
    }

    public void process(List<String> values) {
        // Assign the values to the UI...
    }
}

我确定您可以使用停止"方法将running设置为false;)

已更新

基本概念非常简单.您需要跟踪文件的上次更新时间,如果值在两次睡眠之间更改,则需要重新读取文件并更新屏幕上的值.

在Java 7中,此过程已大大简化,但在此之前,您可以使用File#lastModified来检查文件的最后修改日期/时间.

下面是一个简单的示例.

public class FileCheckerWorker extends SwingWorker<Object, List<String>> {

    @Override
    protected void process(List<List<String>> chunks) {
        List<String> values = chunks.get(chunks.size() - 1);

        txtCardiff.setText(values.get(0));
        txtBristol.setText(values.get(1));
        txtSheffield.setText(values.get(2));
        txtDerby.setText(values.get(3));
        txtGlasgow.setText(values.get(4));
        txtFalkirk.setText(values.get(5));
        txtNewcastle.setText(values.get(6));
        txtLeeds.setText(values.get(7));
        txtPreston.setText(values.get(8));
        txtManchester.setText(values.get(9));
        txtWolverhampton.setText(values.get(10));
        txtBirmingham.setText(values.get(11));
        txtMiltonKeynes.setText(values.get(12));
        txtPeterborough.setText(values.get(13));
        txtColindale.setText(values.get(14));
        txtStepneyGreen.setText(values.get(15));
        txtSlough.setText(values.get(16));
        txtFaraday.setText(values.get(17));
        txtGuildford.setText(values.get(18));
        txtSouthbank.setText(values.get(19));
    }

    @Override
    protected Object doInBackground() throws Exception {
        long lastUpdated = -1;
        File csvFile = new File("...");
        while (true) {
            if (csvFile.lastModified() != lastUpdated) {
                BufferedReader csvReader = null;
                List<String> values = new ArrayList<String>(20);
                try {
                    csvReader = new BufferedReader(new FileReader("..."));
                    for (int index = 0; index < 20; index++) {
                        values.add(CSVFile.readLine());
                    }
                    publish(values);
                    lastUpdated = csvFile.lastModified();
                } finally {
                    try {
                        csvReader.close();
                    } catch (Exception e) {
                    }
                }

                Thread.sleep(20000);

            }
        }
    }
}

您可以使用new FileCheckerWorker.execute()将其放置在initialise方法的末尾.

已更新

不是将字段设置为static,而是将对MapTest的引用传递给工作人员...

import java.awt.Color;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MapTest {

    private JFrame frame;
    protected JTextField txtCardiff;
    protected JTextField txtBristol;
    protected JTextField txtSheffield;
    protected JTextField txtDerby;
    protected JTextField txtGlasgow;
    protected JTextField txtFalkirk;
    protected JTextField txtNewcastle;
    protected JTextField txtLeeds;
    protected JTextField txtPreston;
    protected JTextField txtManchester;
    protected JTextField txtWolverhampton;
    protected JTextField txtBirmingham;
    protected JTextField txtMiltonKeynes;
    protected JTextField txtPeterborough;
    protected JTextField txtColindale;
    protected JTextField txtStepneyGreen;
    protected JTextField txtSlough;
    protected JTextField txtFaraday;
    protected JTextField txtGuildford;
    protected JTextField txtSouthbank;
    private BufferedReader CSVFile = new BufferedReader(new FileReader("C:/Users/606854007/workspace/NetworkAppTest/jar/window/Cardiff.txt"));

    ArrayList<String> csv = new ArrayList<String>();

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MapTest window = new MapTest();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     *
     * @throws IOException
     */
    public MapTest() throws IOException {
        try {
            initialize();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * Initialise the contents of the frame.
     *
     * @param output
     * @throws IOException
     */
    private void initialize() throws IOException {
        frame = new JFrame();
        frame.setBounds(0, 0, 1000, 1086);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setBackground(Color.CYAN);

        txtCardiff = new JTextField();
        txtCardiff.setBackground(Color.GREEN);
        txtCardiff.setBounds(305, 699, 102, 20);
        frame.getContentPane().add(txtCardiff);
        txtCardiff.setColumns(10);

        txtBristol = new JTextField();
        txtBristol.setBackground(Color.GREEN);
        txtBristol.setBounds(416, 780, 102, 20);
        frame.getContentPane().add(txtBristol);
        txtBristol.setColumns(10);

        txtSheffield = new JTextField();
        txtSheffield.setBackground(Color.GREEN);
        txtSheffield.setBounds(517, 356, 110, 20);
        frame.getContentPane().add(txtSheffield);
        txtSheffield.setColumns(10);

        txtDerby = new JTextField();
        txtDerby.setBackground(Color.GREEN);
        txtDerby.setBounds(582, 451, 90, 20);
        frame.getContentPane().add(txtDerby);
        txtDerby.setColumns(10);

        txtGlasgow = new JTextField();
        txtGlasgow.setBackground(Color.GREEN);
        txtGlasgow.setBounds(250, 94, 102, 20);
        frame.getContentPane().add(txtGlasgow);
        txtGlasgow.setColumns(10);

        txtFalkirk = new JTextField();
        txtFalkirk.setColumns(10);
        txtFalkirk.setBackground(Color.GREEN);
        txtFalkirk.setBounds(331, 41, 102, 20);
        frame.getContentPane().add(txtFalkirk);

        txtNewcastle = new JTextField();
        txtNewcastle.setColumns(10);
        txtNewcastle.setBackground(Color.GREEN);
        txtNewcastle.setBounds(424, 141, 111, 20);
        frame.getContentPane().add(txtNewcastle);

        txtLeeds = new JTextField();
        txtLeeds.setColumns(10);
        txtLeeds.setBackground(Color.GREEN);
        txtLeeds.setBounds(481, 244, 102, 20);
        frame.getContentPane().add(txtLeeds);

        txtPreston = new JTextField();
        txtPreston.setColumns(10);
        txtPreston.setBackground(Color.GREEN);
        txtPreston.setBounds(356, 221, 102, 20);
        frame.getContentPane().add(txtPreston);

        txtManchester = new JTextField();
        txtManchester.setColumns(10);
        txtManchester.setBackground(Color.GREEN);
        txtManchester.setBounds(394, 394, 124, 20);
        frame.getContentPane().add(txtManchester);

        txtWolverhampton = new JTextField();
        txtWolverhampton.setColumns(10);
        txtWolverhampton.setBackground(Color.GREEN);
        txtWolverhampton.setBounds(360, 486, 153, 20);
        frame.getContentPane().add(txtWolverhampton);

        txtBirmingham = new JTextField();
        txtBirmingham.setColumns(10);
        txtBirmingham.setBackground(Color.GREEN);
        txtBirmingham.setBounds(424, 553, 128, 20);
        frame.getContentPane().add(txtBirmingham);

        txtMiltonKeynes = new JTextField();
        txtMiltonKeynes.setColumns(10);
        txtMiltonKeynes.setBackground(Color.GREEN);
        txtMiltonKeynes.setBounds(474, 604, 135, 20);
        frame.getContentPane().add(txtMiltonKeynes);

        txtPeterborough = new JTextField();
        txtPeterborough.setColumns(10);
        txtPeterborough.setBackground(Color.GREEN);
        txtPeterborough.setBounds(655, 576, 135, 20);
        frame.getContentPane().add(txtPeterborough);

        txtColindale = new JTextField();
        txtColindale.setColumns(10);
        txtColindale.setBackground(Color.GREEN);
        txtColindale.setBounds(541, 699, 107, 20);
        frame.getContentPane().add(txtColindale);

        txtStepneyGreen = new JTextField();
        txtStepneyGreen.setColumns(10);
        txtStepneyGreen.setBackground(Color.GREEN);
        txtStepneyGreen.setBounds(667, 699, 137, 20);
        frame.getContentPane().add(txtStepneyGreen);

        txtSlough = new JTextField();
        txtSlough.setColumns(10);
        txtSlough.setBackground(Color.GREEN);
        txtSlough.setBounds(525, 756, 102, 20);
        frame.getContentPane().add(txtSlough);

        txtFaraday = new JTextField();
        txtFaraday.setColumns(10);
        txtFaraday.setBackground(Color.GREEN);
        txtFaraday.setBounds(650, 756, 102, 20);
        frame.getContentPane().add(txtFaraday);

        txtGuildford = new JTextField();
        txtGuildford.setColumns(10);
        txtGuildford.setBackground(Color.GREEN);
        txtGuildford.setBounds(525, 822, 102, 20);
        frame.getContentPane().add(txtGuildford);

        txtSouthbank = new JTextField();
        txtSouthbank.setColumns(10);
        txtSouthbank.setBackground(Color.GREEN);
        txtSouthbank.setBounds(655, 807, 115, 20);
        frame.getContentPane().add(txtSouthbank);

        JLabel lblNewLabel = new JLabel("New label");
        lblNewLabel.setForeground(Color.BLACK);
        lblNewLabel.setBackground(Color.BLACK);
        lblNewLabel.setIcon(new ImageIcon(
                        "C:\\Users\\606854007\\workspace\\NetworkAppTest\\Test\\window\\England_Wales.gif"));
        lblNewLabel.setBounds(33, 11, 890, 1086);
        frame.getContentPane().add(lblNewLabel);

        new FileCheckerWorker(this).execute();

    }

    public class FileCheckerWorker extends SwingWorker<Object, List<String>> {

        private MapTest mapTest;

        public FileCheckerWorker(MapTest mapTest) {
            this.mapTest = mapTest;
        }

        @Override
        protected void process(List<List<String>> chunks) {
            List<String> values = chunks.get(chunks.size() - 1);

            mapTest.txtCardiff.setText(values.get(0));
            mapTest.txtBristol.setText(values.get(1));
            mapTest.txtSheffield.setText(values.get(2));
            mapTest.txtDerby.setText(values.get(3));
            mapTest.txtGlasgow.setText(values.get(4));
            mapTest.txtFalkirk.setText(values.get(5));
            mapTest.txtNewcastle.setText(values.get(6));
            mapTest.txtLeeds.setText(values.get(7));
            mapTest.txtPreston.setText(values.get(8));
            mapTest.txtManchester.setText(values.get(9));
            mapTest.txtWolverhampton.setText(values.get(10));
            mapTest.txtBirmingham.setText(values.get(11));
            mapTest.txtMiltonKeynes.setText(values.get(12));
            mapTest.txtPeterborough.setText(values.get(13));
            mapTest.txtColindale.setText(values.get(14));
            mapTest.txtStepneyGreen.setText(values.get(15));
            mapTest.txtSlough.setText(values.get(16));
            mapTest.txtFaraday.setText(values.get(17));
            mapTest.txtGuildford.setText(values.get(18));
            mapTest.txtSouthbank.setText(values.get(19));
        }

        @Override
        protected Object doInBackground() throws Exception {
            long lastUpdated = -1;
            File csvFile = new File("C:/Users/606854007/workspace/NetworkAppTest/Jar/window/Cardiff.txt");
            while (true) {
                if (csvFile.lastModified() != lastUpdated) {
                    BufferedReader csvReader = null;
                    List<String> values = new ArrayList<String>(20);
                    try {
                        csvReader = new BufferedReader(new FileReader("C:/Users/606854007/workspace/NetworkAppTest/Jar/window/Cardiff.txt"));
                        for (int index = 0; index < 20; index++) {
                            values.add(mapTest.CSVFile.readLine());
                        }
                        publish(values);
                        lastUpdated = csvFile.lastModified();
                    } finally {
                        try {
                            csvReader.close();
                        } catch (Exception e) {
                        }
                    }

                    Thread.sleep(5000);
                    System.out.println("Check For Change");
                }
            }
        }
    }
}

I have a program that looks into a single Notepad file which contains information similar to:

Cardiff : 3245658

Bristol : 4726485

Manchester : 4728945

These places and numbers are coming out of a automatic system, and the numbers change every 20 seconds or so. I need to create a loop(I'm guessing) so that when the numbers change, my program will look back into the notepad file and update the number in the JTextField

P.s I have already created the program so that it looks into the file, gets the information needs and puts it in the correct place, using an ArrayList,

All I need help with is how to make the code automatically update when there is a change in the file, and I need to do this without using any sort of Jbuttons, it just needs to do it by itself.

My whole code as it stands:

package window;

import java.awt.Color;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MapTest {

    private JFrame frame;
    static JTextField txtCardiff;
    static JTextField txtBristol;
    static JTextField txtSheffield;
    static JTextField txtDerby;
    static JTextField txtGlasgow;
    static JTextField txtFalkirk;
    static JTextField txtNewcastle;
    static JTextField txtLeeds;
    static JTextField txtPreston;
    static JTextField txtManchester;
    static JTextField txtWolverhampton;
    static JTextField txtBirmingham;
    static JTextField txtMiltonKeynes;
    static JTextField txtPeterborough;
    static JTextField txtColindale;
    static JTextField txtStepneyGreen;
    static JTextField txtSlough;
    static JTextField txtFaraday;
    static JTextField txtGuildford;
    static JTextField txtSouthbank;

    static BufferedReader CSVFile = new BufferedReader(new FileReader
        ("C:/Users/606854007/workspace/NetworkAppTest/jar/window/Cardiff.txt"));
// putting static infront of this Br ' CSVFile' gives: 
//Unhandled exception type FileNotFoundException. 
//but when i take away the 'static' it causes a problem in the 'FileCheckerWorker' class.
// (Will comment where and what)

    ArrayList<String> csv = new ArrayList<String>();

    /**
    private String output = "";
    private String output1 = "";
    private String output2 = "";
    private String output3 = "";
    private String output4 = "";
    private String output5 = "";
    private String output6 = "";
    private String output7 = "";
    private String output8 = "";
    private String output9 = "";
    private String output10 = "";
    private String output11 = "";
    private String output12 = "";
    private String output13 = "";
    private String output14 = "";
    private String output15 = "";
    private String output16 = "";
    private String output17 = "";
    private String output18 = "";
    private String output19 = "";
    **/
    /** * Launch the application. */
    public static void main(String[] args) {
         try {
                UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());

            } catch (ClassNotFoundException e1) {
                e1.printStackTrace();

            } catch (InstantiationException e1) {
                e1.printStackTrace();

            } catch (IllegalAccessException e1) {
                e1.printStackTrace();

            } catch (UnsupportedLookAndFeelException e1) {
                e1.printStackTrace();
            }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MapTest window = new MapTest();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    /**
     * Create the application.
     * 
     * @throws IOException
     */
    public MapTest() throws IOException {
        try {
            initialize();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * Initialise the contents of the frame.
     * 
     * @param output
     * @throws IOException
     */

    private void initialize() throws IOException {

                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());
                csv.add(CSVFile.readLine());

                System.out.println(csv.get(0));
                System.out.println(csv.get(1));
                System.out.println(csv.get(2));
                System.out.println(csv.get(3));
                System.out.println(csv.get(4));
                System.out.println(csv.get(5));
                System.out.println(csv.get(6));
                System.out.println(csv.get(7));
                System.out.println(csv.get(8));
                System.out.println(csv.get(9));
                System.out.println(csv.get(10));
                System.out.println(csv.get(11));
                System.out.println(csv.get(12));
                System.out.println(csv.get(13));
                System.out.println(csv.get(14));
                System.out.println(csv.get(15));
                System.out.println(csv.get(16));
                System.out.println(csv.get(17));
                System.out.println(csv.get(18));
                System.out.println(csv.get(19));


        /**
         * JPanel container = new JPanel(); container.add(txtBirmingham);
         * container.add(txtBristol); container.add(txtCardiff);
         * container.add(txtColindale); container.add(txtDerby);
         * container.add(txtFalkirk); container.add(txtGlasgow);
         * container.add(txtGuildford); container.add(txtLeeds);
         * container.add(txtManchester); container.add(txtMiltonKeynes);
         * container.add(txtNewcastle); container.add(txtPeterborough);
         * container.add(txtPreston); container.add(txtSheffield);
         * container.add(txtSlough); container.add(txtSouthbank);
         * container.add(txtStepneyGreen); container.add(txtWolverhampton);
         * //container.add(); JScrollPane jsp = new JScrollPane(container);
         * frame.add(jsp);
         **/

        frame = new JFrame();
        frame.setBounds(0, 0, 1000, 1086);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setBackground(Color.CYAN);

        txtCardiff = new JTextField();
        txtCardiff.setText(csv.get(0));
        txtCardiff.setBackground(Color.GREEN);
        txtCardiff.setBounds(305, 699, 102, 20);
        frame.getContentPane().add(txtCardiff);
        txtCardiff.setColumns(10);

        txtBristol = new JTextField();
        txtBristol.setText(csv.get(1));
        txtBristol.setBackground(Color.GREEN);
        txtBristol.setBounds(416, 780, 102, 20);
        frame.getContentPane().add(txtBristol);
        txtBristol.setColumns(10);

        txtSheffield = new JTextField();
        txtSheffield.setText(csv.get(2));
        txtSheffield.setBackground(Color.GREEN);
        txtSheffield.setBounds(517, 356, 110, 20);
        frame.getContentPane().add(txtSheffield);
        txtSheffield.setColumns(10);

        txtDerby = new JTextField();
        txtDerby.setText(csv.get(3));
        txtDerby.setBackground(Color.GREEN);
        txtDerby.setBounds(582, 451, 90, 20);
        frame.getContentPane().add(txtDerby);
        txtDerby.setColumns(10);

        txtGlasgow = new JTextField();
        txtGlasgow.setText(csv.get(4));
        txtGlasgow.setBackground(Color.GREEN);
        txtGlasgow.setBounds(250, 94, 102, 20);
        frame.getContentPane().add(txtGlasgow);
        txtGlasgow.setColumns(10);

        txtFalkirk = new JTextField();
        txtFalkirk.setText(csv.get(5));
        txtFalkirk.setColumns(10);
        txtFalkirk.setBackground(Color.GREEN);
        txtFalkirk.setBounds(331, 41, 102, 20);
        frame.getContentPane().add(txtFalkirk);

        txtNewcastle = new JTextField();
        txtNewcastle.setText(csv.get(6));
        txtNewcastle.setColumns(10);
        txtNewcastle.setBackground(Color.GREEN);
        txtNewcastle.setBounds(424, 141, 111, 20);
        frame.getContentPane().add(txtNewcastle);

        txtLeeds = new JTextField();
        txtLeeds.setText(csv.get(7));
        txtLeeds.setColumns(10);
        txtLeeds.setBackground(Color.GREEN);
        txtLeeds.setBounds(481, 244, 102, 20);
        frame.getContentPane().add(txtLeeds);

        txtPreston = new JTextField();
        txtPreston.setText(csv.get(8));
        txtPreston.setColumns(10);
        txtPreston.setBackground(Color.GREEN);
        txtPreston.setBounds(356, 221, 102, 20);
        frame.getContentPane().add(txtPreston);

        txtManchester = new JTextField();
        txtManchester.setText(csv.get(9));
        txtManchester.setColumns(10);
        txtManchester.setBackground(Color.GREEN);
        txtManchester.setBounds(394, 394, 124, 20);
        frame.getContentPane().add(txtManchester);

        txtWolverhampton = new JTextField();
        txtWolverhampton.setText(csv.get(10));
        txtWolverhampton.setColumns(10);
        txtWolverhampton.setBackground(Color.GREEN);
        txtWolverhampton.setBounds(360, 486, 153, 20);
        frame.getContentPane().add(txtWolverhampton);

        txtBirmingham = new JTextField();
        txtBirmingham.setText(csv.get(11));
        txtBirmingham.setColumns(10);
        txtBirmingham.setBackground(Color.GREEN);
        txtBirmingham.setBounds(424, 553, 128, 20);
        frame.getContentPane().add(txtBirmingham);

        txtMiltonKeynes = new JTextField();
        txtMiltonKeynes.setText(csv.get(12));
        txtMiltonKeynes.setColumns(10);
        txtMiltonKeynes.setBackground(Color.GREEN);
        txtMiltonKeynes.setBounds(474, 604, 135, 20);
        frame.getContentPane().add(txtMiltonKeynes);

        txtPeterborough = new JTextField();
        txtPeterborough.setText(csv.get(13));
        txtPeterborough.setColumns(10);
        txtPeterborough.setBackground(Color.GREEN);
        txtPeterborough.setBounds(655, 576, 135, 20);
        frame.getContentPane().add(txtPeterborough);

        txtColindale = new JTextField();
        txtColindale.setText(csv.get(14));
        txtColindale.setColumns(10);
        txtColindale.setBackground(Color.GREEN);
        txtColindale.setBounds(541, 699, 107, 20);
        frame.getContentPane().add(txtColindale);

        txtStepneyGreen = new JTextField();
        txtStepneyGreen.setText(csv.get(15));
        txtStepneyGreen.setColumns(10);
        txtStepneyGreen.setBackground(Color.GREEN);
        txtStepneyGreen.setBounds(667, 699, 137, 20);
        frame.getContentPane().add(txtStepneyGreen);

        txtSlough = new JTextField();
        txtSlough.setText(csv.get(16));
        txtSlough.setColumns(10);
        txtSlough.setBackground(Color.GREEN);
        txtSlough.setBounds(525, 756, 102, 20);
        frame.getContentPane().add(txtSlough);

        txtFaraday = new JTextField();
        txtFaraday.setText(csv.get(17));
        txtFaraday.setColumns(10);
        txtFaraday.setBackground(Color.GREEN);
        txtFaraday.setBounds(650, 756, 102, 20);
        frame.getContentPane().add(txtFaraday);

        txtGuildford = new JTextField();
        txtGuildford.setText(csv.get(18));
        txtGuildford.setColumns(10);
        txtGuildford.setBackground(Color.GREEN);
        txtGuildford.setBounds(525, 822, 102, 20);
        frame.getContentPane().add(txtGuildford);

        txtSouthbank = new JTextField();
        txtSouthbank.setText(csv.get(19));
        txtSouthbank.setColumns(10);
        txtSouthbank.setBackground(Color.GREEN);
        txtSouthbank.setBounds(655, 807, 115, 20);
        frame.getContentPane().add(txtSouthbank);

        JLabel lblNewLabel = new JLabel("New label");
        lblNewLabel.setForeground(Color.BLACK);
        lblNewLabel.setBackground(Color.BLACK);
        lblNewLabel
                .setIcon(new ImageIcon(
                        "C:\\Users\\606854007\\workspace\\NetworkAppTest\\Test\\window\\England_Wales.gif"));
        lblNewLabel.setBounds(33, 11, 890, 1086);
        frame.getContentPane().add(lblNewLabel);

        new FileCheckerWorker(null).execute();
\\ if i dont but null in the '()' then it comes up with an error, 
\\and this just makes it execute:
    /**
public FileCheckerWorker(MapTest mapTest) {
        this.mapTest = mapTest;
    }
**/
\\ which is not what I want, I have tried to add commands 
\\to run the methods from within:
\\ ' public FileCheckerWorker(MapTest mapTest)' method
\\ but it gives me a null pointer exception,


}

    }

Below is the 'FileCheckerWorker' class hoping I've done it right :p

package window;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import javax.swing.SwingWorker;


public class FileCheckerWorker extends SwingWorker<Object, List<String>> {

    private MapTest mapTest;

    public FileCheckerWorker(MapTest mapTest) {
        this.mapTest = mapTest;
    }


        @Override
        protected void process(List<List<String>> chunks) {
           List<String> values = chunks.get(chunks.size() - 1);

           mapTest.txtCardiff.setText(values.get(0));
           mapTest.txtBristol.setText(values.get(1));
           mapTest.txtSheffield.setText(values.get(2));
           mapTest.txtDerby.setText(values.get(3));
           mapTest.txtGlasgow.setText(values.get(4));
           mapTest.txtFalkirk.setText(values.get(5));
           mapTest.txtNewcastle.setText(values.get(6));
           mapTest.txtLeeds.setText(values.get(7));
           mapTest.txtPreston.setText(values.get(8));
           mapTest.txtManchester.setText(values.get(9));
           mapTest.txtWolverhampton.setText(values.get(10));
           mapTest.txtBirmingham.setText(values.get(11));
           mapTest.txtMiltonKeynes.setText(values.get(12));
           mapTest.txtPeterborough.setText(values.get(13));
           mapTest.txtColindale.setText(values.get(14));
           mapTest.txtStepneyGreen.setText(values.get(15));
           mapTest.txtSlough.setText(values.get(16));
           mapTest.txtFaraday.setText(values.get(17));
           mapTest.txtGuildford.setText(values.get(18));
           mapTest.txtSouthbank.setText(values.get(19));
        }

        @Override
        protected Object doInBackground() throws Exception {
            long lastUpdated = -1;
            File csvFile = new File("C:/Users/606854007/workspace/NetworkAppTest/Jar/window/Cardiff.txt");
            while (true) {
                if (csvFile.lastModified() != lastUpdated) {
                    BufferedReader csvReader = null;
                    List<String> values = new ArrayList<String>(20);
                    try {
                        csvReader = new BufferedReader(new FileReader("C:/Users/606854007/workspace/NetworkAppTest/Jar/window/Cardiff.txt"));
                        for (int index = 0; index < 20; index++) {
                            values.add(mapTest.CSVFile.readLine());
                        }
                        publish(values);
                        lastUpdated = csvFile.lastModified();
                    } finally {
                        try {
                            csvReader.close();
                        } catch (Exception e) {
                        }
                    }

                    Thread.sleep(5000);
                    System.out.println("Check For Change");
                }
            }
        }

}

UPDATED Both codes to what they are now.

Also when I thought I had the 'doInBackground' working if gave me a NullPointerException on line values.add(mapTest.CSVFile.readLine());

解决方案

Wrap the "look up" code in a SwingWorker, use Thread.sleep to allow the background thread to idle for a given period (say 20 seconds).

Load the contents of the file, determine if it's changed and use the pusblish method to send the results to the process method, which allows you to process the results withing the Event Dispatching Thread

Take a look at SwingWorker for more details

Simple Example

public class PeriodicalWorker extends SwingWorker<Object, String> {
    private boolean running = true;

    public Object doInBackground() throws Exception {
        while (running) {
            try {
                Thread.sleep(20000);
            } catch (Exception exp) {}
            // Load the values...
            // Check to see if anything needs to updated...
            publish(values); // List of String array values
        }
        return null;
    }

    public void process(List<String> values) {
        // Assign the values to the UI...
    }
}

I'm sure you can a "stop" method to set running to false ;)

Updated

The basic concept is very simple. You need to keep track of the last time the file was updated, if the value changes between sleeps, you need to reread the file and update the values on the screen.

This process has been made considerably easier in Java 7, but before that, you could use File#lastModified to check the last modified date/time of the file.

Below is simple example.

public class FileCheckerWorker extends SwingWorker<Object, List<String>> {

    @Override
    protected void process(List<List<String>> chunks) {
        List<String> values = chunks.get(chunks.size() - 1);

        txtCardiff.setText(values.get(0));
        txtBristol.setText(values.get(1));
        txtSheffield.setText(values.get(2));
        txtDerby.setText(values.get(3));
        txtGlasgow.setText(values.get(4));
        txtFalkirk.setText(values.get(5));
        txtNewcastle.setText(values.get(6));
        txtLeeds.setText(values.get(7));
        txtPreston.setText(values.get(8));
        txtManchester.setText(values.get(9));
        txtWolverhampton.setText(values.get(10));
        txtBirmingham.setText(values.get(11));
        txtMiltonKeynes.setText(values.get(12));
        txtPeterborough.setText(values.get(13));
        txtColindale.setText(values.get(14));
        txtStepneyGreen.setText(values.get(15));
        txtSlough.setText(values.get(16));
        txtFaraday.setText(values.get(17));
        txtGuildford.setText(values.get(18));
        txtSouthbank.setText(values.get(19));
    }

    @Override
    protected Object doInBackground() throws Exception {
        long lastUpdated = -1;
        File csvFile = new File("...");
        while (true) {
            if (csvFile.lastModified() != lastUpdated) {
                BufferedReader csvReader = null;
                List<String> values = new ArrayList<String>(20);
                try {
                    csvReader = new BufferedReader(new FileReader("..."));
                    for (int index = 0; index < 20; index++) {
                        values.add(CSVFile.readLine());
                    }
                    publish(values);
                    lastUpdated = csvFile.lastModified();
                } finally {
                    try {
                        csvReader.close();
                    } catch (Exception e) {
                    }
                }

                Thread.sleep(20000);

            }
        }
    }
}

You would place this at the end of your initialise method with new FileCheckerWorker.execute().

Updated

Instead of making your fields static, pass a reference to of the MapTest to the worker...

import java.awt.Color;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MapTest {

    private JFrame frame;
    protected JTextField txtCardiff;
    protected JTextField txtBristol;
    protected JTextField txtSheffield;
    protected JTextField txtDerby;
    protected JTextField txtGlasgow;
    protected JTextField txtFalkirk;
    protected JTextField txtNewcastle;
    protected JTextField txtLeeds;
    protected JTextField txtPreston;
    protected JTextField txtManchester;
    protected JTextField txtWolverhampton;
    protected JTextField txtBirmingham;
    protected JTextField txtMiltonKeynes;
    protected JTextField txtPeterborough;
    protected JTextField txtColindale;
    protected JTextField txtStepneyGreen;
    protected JTextField txtSlough;
    protected JTextField txtFaraday;
    protected JTextField txtGuildford;
    protected JTextField txtSouthbank;
    private BufferedReader CSVFile = new BufferedReader(new FileReader("C:/Users/606854007/workspace/NetworkAppTest/jar/window/Cardiff.txt"));

    ArrayList<String> csv = new ArrayList<String>();

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MapTest window = new MapTest();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     *
     * @throws IOException
     */
    public MapTest() throws IOException {
        try {
            initialize();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * Initialise the contents of the frame.
     *
     * @param output
     * @throws IOException
     */
    private void initialize() throws IOException {
        frame = new JFrame();
        frame.setBounds(0, 0, 1000, 1086);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        frame.setBackground(Color.CYAN);

        txtCardiff = new JTextField();
        txtCardiff.setBackground(Color.GREEN);
        txtCardiff.setBounds(305, 699, 102, 20);
        frame.getContentPane().add(txtCardiff);
        txtCardiff.setColumns(10);

        txtBristol = new JTextField();
        txtBristol.setBackground(Color.GREEN);
        txtBristol.setBounds(416, 780, 102, 20);
        frame.getContentPane().add(txtBristol);
        txtBristol.setColumns(10);

        txtSheffield = new JTextField();
        txtSheffield.setBackground(Color.GREEN);
        txtSheffield.setBounds(517, 356, 110, 20);
        frame.getContentPane().add(txtSheffield);
        txtSheffield.setColumns(10);

        txtDerby = new JTextField();
        txtDerby.setBackground(Color.GREEN);
        txtDerby.setBounds(582, 451, 90, 20);
        frame.getContentPane().add(txtDerby);
        txtDerby.setColumns(10);

        txtGlasgow = new JTextField();
        txtGlasgow.setBackground(Color.GREEN);
        txtGlasgow.setBounds(250, 94, 102, 20);
        frame.getContentPane().add(txtGlasgow);
        txtGlasgow.setColumns(10);

        txtFalkirk = new JTextField();
        txtFalkirk.setColumns(10);
        txtFalkirk.setBackground(Color.GREEN);
        txtFalkirk.setBounds(331, 41, 102, 20);
        frame.getContentPane().add(txtFalkirk);

        txtNewcastle = new JTextField();
        txtNewcastle.setColumns(10);
        txtNewcastle.setBackground(Color.GREEN);
        txtNewcastle.setBounds(424, 141, 111, 20);
        frame.getContentPane().add(txtNewcastle);

        txtLeeds = new JTextField();
        txtLeeds.setColumns(10);
        txtLeeds.setBackground(Color.GREEN);
        txtLeeds.setBounds(481, 244, 102, 20);
        frame.getContentPane().add(txtLeeds);

        txtPreston = new JTextField();
        txtPreston.setColumns(10);
        txtPreston.setBackground(Color.GREEN);
        txtPreston.setBounds(356, 221, 102, 20);
        frame.getContentPane().add(txtPreston);

        txtManchester = new JTextField();
        txtManchester.setColumns(10);
        txtManchester.setBackground(Color.GREEN);
        txtManchester.setBounds(394, 394, 124, 20);
        frame.getContentPane().add(txtManchester);

        txtWolverhampton = new JTextField();
        txtWolverhampton.setColumns(10);
        txtWolverhampton.setBackground(Color.GREEN);
        txtWolverhampton.setBounds(360, 486, 153, 20);
        frame.getContentPane().add(txtWolverhampton);

        txtBirmingham = new JTextField();
        txtBirmingham.setColumns(10);
        txtBirmingham.setBackground(Color.GREEN);
        txtBirmingham.setBounds(424, 553, 128, 20);
        frame.getContentPane().add(txtBirmingham);

        txtMiltonKeynes = new JTextField();
        txtMiltonKeynes.setColumns(10);
        txtMiltonKeynes.setBackground(Color.GREEN);
        txtMiltonKeynes.setBounds(474, 604, 135, 20);
        frame.getContentPane().add(txtMiltonKeynes);

        txtPeterborough = new JTextField();
        txtPeterborough.setColumns(10);
        txtPeterborough.setBackground(Color.GREEN);
        txtPeterborough.setBounds(655, 576, 135, 20);
        frame.getContentPane().add(txtPeterborough);

        txtColindale = new JTextField();
        txtColindale.setColumns(10);
        txtColindale.setBackground(Color.GREEN);
        txtColindale.setBounds(541, 699, 107, 20);
        frame.getContentPane().add(txtColindale);

        txtStepneyGreen = new JTextField();
        txtStepneyGreen.setColumns(10);
        txtStepneyGreen.setBackground(Color.GREEN);
        txtStepneyGreen.setBounds(667, 699, 137, 20);
        frame.getContentPane().add(txtStepneyGreen);

        txtSlough = new JTextField();
        txtSlough.setColumns(10);
        txtSlough.setBackground(Color.GREEN);
        txtSlough.setBounds(525, 756, 102, 20);
        frame.getContentPane().add(txtSlough);

        txtFaraday = new JTextField();
        txtFaraday.setColumns(10);
        txtFaraday.setBackground(Color.GREEN);
        txtFaraday.setBounds(650, 756, 102, 20);
        frame.getContentPane().add(txtFaraday);

        txtGuildford = new JTextField();
        txtGuildford.setColumns(10);
        txtGuildford.setBackground(Color.GREEN);
        txtGuildford.setBounds(525, 822, 102, 20);
        frame.getContentPane().add(txtGuildford);

        txtSouthbank = new JTextField();
        txtSouthbank.setColumns(10);
        txtSouthbank.setBackground(Color.GREEN);
        txtSouthbank.setBounds(655, 807, 115, 20);
        frame.getContentPane().add(txtSouthbank);

        JLabel lblNewLabel = new JLabel("New label");
        lblNewLabel.setForeground(Color.BLACK);
        lblNewLabel.setBackground(Color.BLACK);
        lblNewLabel.setIcon(new ImageIcon(
                        "C:\\Users\\606854007\\workspace\\NetworkAppTest\\Test\\window\\England_Wales.gif"));
        lblNewLabel.setBounds(33, 11, 890, 1086);
        frame.getContentPane().add(lblNewLabel);

        new FileCheckerWorker(this).execute();

    }

    public class FileCheckerWorker extends SwingWorker<Object, List<String>> {

        private MapTest mapTest;

        public FileCheckerWorker(MapTest mapTest) {
            this.mapTest = mapTest;
        }

        @Override
        protected void process(List<List<String>> chunks) {
            List<String> values = chunks.get(chunks.size() - 1);

            mapTest.txtCardiff.setText(values.get(0));
            mapTest.txtBristol.setText(values.get(1));
            mapTest.txtSheffield.setText(values.get(2));
            mapTest.txtDerby.setText(values.get(3));
            mapTest.txtGlasgow.setText(values.get(4));
            mapTest.txtFalkirk.setText(values.get(5));
            mapTest.txtNewcastle.setText(values.get(6));
            mapTest.txtLeeds.setText(values.get(7));
            mapTest.txtPreston.setText(values.get(8));
            mapTest.txtManchester.setText(values.get(9));
            mapTest.txtWolverhampton.setText(values.get(10));
            mapTest.txtBirmingham.setText(values.get(11));
            mapTest.txtMiltonKeynes.setText(values.get(12));
            mapTest.txtPeterborough.setText(values.get(13));
            mapTest.txtColindale.setText(values.get(14));
            mapTest.txtStepneyGreen.setText(values.get(15));
            mapTest.txtSlough.setText(values.get(16));
            mapTest.txtFaraday.setText(values.get(17));
            mapTest.txtGuildford.setText(values.get(18));
            mapTest.txtSouthbank.setText(values.get(19));
        }

        @Override
        protected Object doInBackground() throws Exception {
            long lastUpdated = -1;
            File csvFile = new File("C:/Users/606854007/workspace/NetworkAppTest/Jar/window/Cardiff.txt");
            while (true) {
                if (csvFile.lastModified() != lastUpdated) {
                    BufferedReader csvReader = null;
                    List<String> values = new ArrayList<String>(20);
                    try {
                        csvReader = new BufferedReader(new FileReader("C:/Users/606854007/workspace/NetworkAppTest/Jar/window/Cardiff.txt"));
                        for (int index = 0; index < 20; index++) {
                            values.add(mapTest.CSVFile.readLine());
                        }
                        publish(values);
                        lastUpdated = csvFile.lastModified();
                    } finally {
                        try {
                            csvReader.close();
                        } catch (Exception e) {
                        }
                    }

                    Thread.sleep(5000);
                    System.out.println("Check For Change");
                }
            }
        }
    }
}

这篇关于Java-我需要在Swing GUI程序中更新JTextFields的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆