1

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();
        }
    }
LooMeenin
  • 818
  • 3
  • 17
  • 33
  • my goodness what a horrible idea, redirecting stderr. Of course that will make it difficult to debug this problem. Comment out the line `System.setErr(new PrintStream(out, true));` and try again. Do you see anything on the console / terminal window? – Robin Green Jan 05 '14 at 19:52
  • Tried your suggestion and I'm still having the same problem. No output to the GUI – LooMeenin Jan 05 '14 at 19:54
  • what about on the *console* or *terminal window*? I'm not talking about the GUI. You do have a console or terminal window open don't you? – Robin Green Jan 05 '14 at 19:55
  • The program runs fine when I output to the console in my IDE. It's only when I run it from inside the GUI that I have a problem. – LooMeenin Jan 05 '14 at 19:57
  • After you have commented out that line I told you to comment out, any errors (like exceptions) should appear on the console. If you don't have a console visible you won't be able to see the errors. How are you running your GUI? – Robin Green Jan 05 '14 at 20:00
  • I do have the console open but when I run the program from the GUI nothing appears in the console. I have a main method inside the GUI class that fires up the GUI.. not sure if that's what your asking? – LooMeenin Jan 05 '14 at 20:05
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44571/discussion-between-robin-green-and-loomeenin) – Robin Green Jan 05 '14 at 20:06

1 Answers1

4

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();
Robin Green
  • 32,079
  • 16
  • 104
  • 187