0

I am looking for a way to put a console output window in a runnable jar file. I looked up a way that would work for me but I also would need to know how to get that into my main file as this one has its own package etc.

package newbuttonthing;

import java.util.Date;

public class clock {
   public static void main(String args[]) {
       // Instantiate a Date object
       Date date = new Date();

       // display time and date using toString()
       System.out.println(date.toString());
   }
}

The window should just have to show up in a little bar (1 line?) at the bottom of the window i will clarify if needed (post my code and indicate where in the window it needs to be).

3
  • Are you asking how to get the characters written to System.out? Commented Jan 14, 2015 at 23:15
  • Anything would work as long as it prints it out somewhere that someone can see it. Commented Jan 14, 2015 at 23:19
  • For SO questions, you need to ask something quite specific (like, how to get access to the data written to System.out), rather than open ended like design me a GUI application with a log window. We're here to help, but you've got to meet us half-way. :) Commented Jan 14, 2015 at 23:23

2 Answers 2

1

You can do it like this:

public static void setOutput(final JLabel label) {
    System.setOut(new PrintStream(new OutputStream() {
        @Override
        public void write(int b) throws IOException {
            char c = (char) b;
            if (c == '\n' || c == '\r') {
                label.setText("");
            } else {
                label.setText(label.getText() + c);
            }
        }
    }));
}

Then every time you call System.out.print or System.out.println it will display the latest line on your label.

Sign up to request clarification or add additional context in comments.

Comments

0

I believe that he is asking a GUI window that displays the date and time. For this to work you will need a thread to periodically update the GUI window for the current date and time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.