Search code examples
javaswingjfilechooserthread-sleep

Run Frame and loop at same time?


Im making a simple little program that spams a Text file. But im stuck at a problem. I need to run a while(true) loop and a frame at the same time. I need close the frame too. But i have tried threading and stuff but i cant figure it out for nothing, and im at the point where i need help. This is what ive got

MAIN

    public static void main(String[] args) throws IOException {
        Frame frame = new Frame();
    }

    public static void Spam(){
        try{
            while(true){
                String userName = names[ran.nextInt(names.length)]+ran.nextInt(360);
                String rawMessage = messages[ran.nextInt(names.length)]+ran.nextInt(360);

                String message=userName+": "+rawMessage;

                CustomWriter writer = new CustomWriter();
                CustomWriter.Write(message);

                System.out.println(message);

                Thread.sleep(waitTime);
            }
        }catch(Exception err){}             
    }
}

FRAME

    public class Frame {
    String file;
    Frame()
    {
        final JFrame frame = new JFrame("SuperSpammer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300,100);

        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new FlowLayout());

        //Now lets get the stuff
        JButton bPickFile = new JButton("FILE");
        JButton bStart = new JButton("START");

        final JTextField tfWaitTime = new JTextField(5);
        final JLabel lSpaming = new JLabel("SPAMING");
        final JFileChooser fc = new JFileChooser();

        frame.add(bPickFile);
        frame.add(tfWaitTime);
        frame.add(bStart);

        frame.setVisible(true);

        //ActionListners
        bPickFile.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                int returnVal = fc.showOpenDialog(fc);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    file = fc.getSelectedFile().toString();
                }
            }
        });
        bStart.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                if(file!=null){
                    frame.removeAll();
                    frame.add(lSpaming);
                    Main.Spam();
                }
            }
        });
    }       
}

So how can i run the loop and the frame at the same time so i can close the frame? What am i doing wrong and how can i fix it?


Solution

  • Swing is a single threaded environment, that means that anything that blocks the Event Dispatching Thread will prevent the UI from been updated (or respond to user interaction).

    Have a look at Concurrency in Swing for more details.

    In this case, you want to start a background thread and run you "spam" loop within it.

    Also remember, that Swing is not thread safe, this means that you should never try to interact with or modify any UI component from any thread outside of the EDT.