LooMeenin
LooMeenin

Reputation: 818

Redirecting console output to GUI

I borrowed a design that I found on stackoverflow to redirect console output to a GUI. It worked fine until I started reading from text files in my program. Now when I run the program using the GUI no output is displayed and the GUI freezes and then closes on its own eventually. Here is a slimmed down version of my GUI code:

public class GreenhouseControlsGUI {
    public static class MyGui extends JFrame implements ActionListener {

      // Start button 
      JButton Start = new JButton("Start");

      /..................................../

      /**
       * The constructor.
       */ 
     public MyGui(){     
        super("Greenhouse Controls");

            /............................../

        bottomPanel.setLayout(new FlowLayout());
        bottomPanel.add(Start);

            /............................../

        getContentPane().add(holdAll, BorderLayout.CENTER);

        Start.addActionListener(this);

            /............................../

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
     }

    public void actionPerformed(ActionEvent e){

        if (e.getSource() == Start)
            GreenhouseControls.startMeUp(); // Start program...

            /............................../
        }

    public static void main(String[] args){

        MyGui myApplication = new MyGui();

        // redirect output to GUI
        myApplication.redirectSystemStreams();

        // Specify where will it appear on the screen:
        myApplication.setLocation(10, 10);
        myApplication.setSize(500, 300);

        // Show it!
        myApplication.setVisible(true);
      } 

      private void updateTextArea(final String text) {
            SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                myText.append(text);
              }
            });
          }

      private void redirectSystemStreams() {

          OutputStream out = new OutputStream() {

              @Override
              public void write(int b) throws IOException {
                updateTextArea(String.valueOf((char) b));
              }

              @Override
              public void write(byte[] b, int off, int len) throws IOException {
                updateTextArea(new String(b, off, len));
              }

              @Override
              public void write(byte[] b) throws IOException {
                write(b, 0, b.length);
              }
            };

            System.setOut(new PrintStream(out, true));
            System.setErr(new PrintStream(out, true));
          }
    }
}

I'm pretty sure my problem starts with reading from the 'filename' path in the code below because I don't have this problem when I comment the 'filename' variable declaration out. I thought the methods to redirect console output to my GUI were only redirecting output.... Why is it screwing everything up when I read from a file? I am new to programming and I have probably overlooked something obvious, but I can't figure it out.

Here is the static startMeUp() method invoked inside the GUI class:

public static void startMeUp() {
        try {
            String option   = "-f";                    // (-f) to start program or (-d) to restore program
            filename = "src/greenhouse/examples3.txt"; // read file from here
            dumpFile = "dump.out";                     // restore program from here

            // if option is invalid invoke corresponding print statement
            if ( !(option.equals("-f")) && !(option.equals("-d")) ) {
               System.out.println("Invalid option");
                printUsage();
            }

            GreenhouseControls gc = new GreenhouseControls(); // Create GreenhouseControls object 
            Restart restart = new Restart(0,filename, gc);    // Create Restart object
            Restore restore = new Restore(0,dumpFile);        // Create Restore object

            // if the option value is -f ... 
            if (option.equals("-f"))  {
                gc.addEvent(restart);      // add new Restart object to addEvent() 
            }
            gc.run();                                   

            // if the option value is -d ... 
            if (option.equals("-d"))  {
                gc.addEvent(restore);     // add new Restore object to addEvent()
            }
            gc.run();

            }catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Invalid number of parameters");
            printUsage();
        }
    }

Upvotes: 1

Views: 4753

Answers (1)

Robin Green
Robin Green

Reputation: 33033

You need to invoke startMeUp in a new thread, because your console program is blocking the event dispatch thread. Like this:

new Thread () {
  @Override public void run () {
    GreenhouseControls.startMeUp();
  }
}.start();

instead of just

GreenhouseControls.startMeUp();

Upvotes: 4

Related Questions