Console.java 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package ui.view;
  2. import java.awt.BorderLayout;
  3. import javax.swing.Box;
  4. import javax.swing.ImageIcon;
  5. import javax.swing.JButton;
  6. import javax.swing.JPanel;
  7. import javax.swing.JScrollPane;
  8. import javax.swing.JTextArea;
  9. import javax.swing.JToolBar;
  10. import javax.swing.text.DefaultCaret;
  11. import util.ImageImport;
  12. /**
  13. * Little new swing object to print data to a console.
  14. * @author tom
  15. *
  16. */
  17. public class Console extends JPanel {
  18. private JTextArea textArea = new JTextArea();
  19. private JScrollPane scrollPane;
  20. public Console() {
  21. super();
  22. this.setLayout(new BorderLayout());
  23. textArea.setEditable(false);
  24. DefaultCaret caret = (DefaultCaret)textArea.getCaret();
  25. caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
  26. scrollPane = new JScrollPane(textArea);
  27. this.add(scrollPane, BorderLayout.CENTER);
  28. JToolBar toolBar = new JToolBar();
  29. toolBar.setFloatable(false);
  30. JButton clearButton = new JButton("", new ImageIcon(ImageImport.loadImage("/Button_Images/Clear.png", 24, 24)));
  31. clearButton.setToolTipText("Clear Console");
  32. clearButton.addActionListener(actionEvent -> clear());
  33. toolBar.add(clearButton);
  34. toolBar.add(Box.createHorizontalGlue());
  35. JButton topButton = new JButton("", new ImageIcon(ImageImport.loadImage("/Button_Images/Top.png", 24, 24)));
  36. topButton.setToolTipText("Scroll to top");
  37. topButton.addActionListener(actionEvent -> scrollToTop());
  38. toolBar.add(topButton);
  39. JButton botButton = new JButton("", new ImageIcon(ImageImport.loadImage("/Button_Images/Bottom.png", 24, 24)));
  40. botButton.setToolTipText("Scroll to bottom");
  41. botButton.addActionListener(actionEvent -> scrollToBottom());
  42. toolBar.add(botButton);
  43. scrollPane.setColumnHeaderView(toolBar);
  44. }
  45. private void scrollToTop() {
  46. textArea.setCaretPosition(0);
  47. }
  48. private void scrollToBottom() {
  49. textArea.setCaretPosition(textArea.getDocument().getLength());
  50. }
  51. public void clear() {
  52. textArea.setText("");
  53. }
  54. public void print(String message) {
  55. textArea.append(message);
  56. }
  57. public void println(String message) {
  58. textArea.append(message + "\n");
  59. }
  60. }