PUBLIC OBJECT

An IntelliJ live template for Swing developers

Whenever I'm prototyping Swing code, there's lots to remember about getting a component on screen:

  • invokeLater() for thread safety
  • setDefaultCloseOperation() to clean up after myself
  • pack() and setLocationRelativeTo() for a convenient frame

    Live TemplatesTo make this easier, I've created a Live Template for IDEA. Add this to your collection of live templates, then simply type swing[tab] and remove a little more boilerplate from your life!

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() { public void run() {
            JPanel panel = new JPanel(new BorderLayout());
            $FOCUS$
    
            JFrame frame = new JFrame();
            frame.getContentPane().add(panel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }});
    }