Terminal emulator in Java Swing?

0

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

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

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 of those program installers (the famous Next, next, finish). In them there is a component that shows you the progress of the installation (creation of folders, configurations, etc.). This would be System.out.println() .

So, could you help me?

    
asked by anonymous 30.06.2015 / 18:33

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 (JFrame) class called print (), which takes a String, and it adds this String to the StyledDocument associated with the 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 in the old terminals, you can change the font to be more oldschool still, and finally, it's simple.

    
30.06.2015 / 19:05