Java Swing terminal emulator?

Asked

Viewed 654 times

0

Hello! Everyone who has ever worked with Java at least once must have seen codes intended for terminal (console) use, such as the method println(), located in System.out.

I wonder if there is a terminal emulator for Java Swing. Just out of curiosity! For example: I have a Swing window, and inside it I need a component that "rotates" instructions to the terminal (console). Here the little block:

Thread.sleep(1000);
System.out.println("Preparando-se para explodir...");
Thread.sleep(1000);
System.out.println("3");
Thread.sleep(1000);
System.out.println("2");
Thread.sleep(1000);
System.out.println("1");
Thread.sleep(1000);
System.out.println("KABUUUUUUUUUUUUUUM!!!!");
Thread.sleep(1000);

If you have not understood, think about those program installers (the famous Next, next, Finish). In them there is a component that shows you the course of the installation (creation of folders, settings, etc.). This would be the System.out.println().

So you could help me?

  • 1

    Actually the println method, is not associated with a console, is a Printstream which is an Outputstream, it may be associated with a file.

  • Think about it: Eclipse shows a console in programs, right? There’s a way I can put this "console" in the Swing window?

  • I missed giving this reference: http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#out

  • I think so, yeah. For example: if you use a swing inputText, you can write commands to the terminal and read the results of them wherever you want using the Runtime class

1 answer

0


Gabriel, this is easy to do using a Jtextpane. I created a small example using a file just for you to test there.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.StyledDocument;
import javax.swing.JTextPane;

import java.awt.Color;

public class TerminalFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private StyledDocument doc;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TerminalFrame terminal = new TerminalFrame();
                    terminal.setVisible(true);
                    terminal.print("Hello bloody world!"); // Using print() to print to the "Swing Terminal".
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public TerminalFrame() {
        setTitle("Terminal Simulation");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JTextPane textPane = new JTextPane();
        textPane.setForeground(Color.GREEN);
        textPane.setBackground(Color.BLACK);
        textPane.setEditable(false);
        contentPane.add(textPane, BorderLayout.CENTER);
        doc = textPane.getStyledDocument();
    }

    public void print(String s) {
        try {
            doc.insertString(0, s+"\n", null);
        }
        catch(Exception e) { System.out.println(e); }
    }

}

As you can see, I created a method in the Terminalframe class (which extends Jframe) called print(), which takes a String, and it adds that String to the Styleddocument associated with Jtextframe. With this code template you can customize the terminal at will (I left with the black background and with the green foreground already). You can put a custom Carret that flashes like the old terminals, You can change the source to be more oldschool yet, and anyway, it’s simple.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.