fubarable

External Listener

Apr 19th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.GridLayout;
  4. import java.awt.Insets;
  5. import java.awt.event.ActionEvent;
  6. import javax.swing.*;
  7.  
  8. public class MainGUI2 {
  9.     private static final int ROWS = 24;
  10.     private static final int COLS = 48;
  11.     private static final Insets LIST_MARGINS = new Insets(2, 4, 0, 0);
  12.     private JPanel mainPanel = new JPanel();
  13.     private JTextArea listText = new JTextArea(ROWS, COLS);
  14.    
  15.     public MainGUI2() {
  16.         listText.setEditable(false);
  17.         listText.setFocusable(false);
  18.         listText.setWrapStyleWord(true);
  19.         listText.setLineWrap(true);
  20.         listText.setMargin(LIST_MARGINS);
  21.         JScrollPane scrollPane = new JScrollPane(listText);
  22.         scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  23.        
  24.         JPanel buttonPanel = new JPanel();
  25.         buttonPanel.setLayout(new GridLayout(0, 1, 0, 1));
  26.         buttonPanel.add(new JButton(new RefreshAction("Refresh", this)));
  27.        
  28.         JPanel eastPanel = new JPanel(new BorderLayout());
  29.         eastPanel.add(buttonPanel, BorderLayout.PAGE_START);
  30.         eastPanel.setBackground(Color.DARK_GRAY);
  31.        
  32.         mainPanel.setLayout(new BorderLayout());
  33.         mainPanel.add(scrollPane, BorderLayout.CENTER);
  34.         mainPanel.add(eastPanel, BorderLayout.LINE_END);
  35.     }
  36.  
  37.     public JPanel getMainPanel() {
  38.         return mainPanel;
  39.     }
  40.    
  41.     public void appendText(String text) {
  42.         listText.append(text);
  43.     }
  44.  
  45.     private static void createAndShowGui() {
  46.         MainGUI2 mainGui = new MainGUI2();
  47.  
  48.         JFrame frame = new JFrame("GUI");
  49.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50.         frame.getContentPane().add(mainGui.getMainPanel());
  51.         frame.pack();
  52.         frame.setLocationRelativeTo(null);
  53.         frame.setVisible(true);
  54.     }
  55.  
  56.     public static void main(String[] args) {
  57.         SwingUtilities.invokeLater(() -> createAndShowGui());
  58.     }
  59. }
  60.  
  61. @SuppressWarnings("serial")
  62. class RefreshAction extends AbstractAction {
  63.     private MainGUI2 main;
  64.  
  65.     public RefreshAction(String name, MainGUI2 main) {
  66.         super(name);
  67.         int mnemonic = (int) name.charAt(0);
  68.         putValue(MNEMONIC_KEY, mnemonic);
  69.         this.main = main;
  70.     }
  71.  
  72.     @Override
  73.     public void actionPerformed(ActionEvent e) {
  74.         main.appendText("From Refresh Action!\n");
  75.     }
  76. }
Add Comment
Please, Sign In to add comment