{"id":57,"date":"2005-03-16T12:53:50","date_gmt":"2005-03-16T17:53:50","guid":{"rendered":"http:\/\/stein.everybody.org\/journal\/posts\/swing-example\/"},"modified":"2013-04-27T14:19:01","modified_gmt":"2013-04-27T18:19:01","slug":"swing-example","status":"publish","type":"post","link":"https:\/\/jeremystein.com\/journal\/swing-example\/","title":{"rendered":"Swing Example"},"content":{"rendered":"<p>Nearly a year ago, I posted some ideas for <a href=\"http:\/\/stein.everybody.org\/journal\/posts\/best-practices-for-swing\/\">best practices for swing<\/a>  I neglected to add any examples, but today someone finally asked for one.  So I threw together an example of a dialog window that uses composition rather than inheritance.  Whew &#8212; there sure is a lot of boilerplate code just to get a simple dialog!<\/p>\n<pre>import java.awt.*;\nimport java.awt.event.*;\nimport javax.swing.*;\n\npublic class SampleDialog {\n    private JFrame parentFrame;\n    private JDialog dialog;\n    private JTextField field;\n      \n    private JButton okButton;\n    private JButton cancelButton;\n\n    public SampleDialog(JFrame parentFrame) {\n        this.parentFrame = parentFrame;\n    }\n\n    private void init() {\n        this.field = new JTextField();\n    \n        this.okButton = new JButton(\"OK\");\n        this.okButton.addActionListener(new OkButtonListener());\n\n        this.cancelButton = new JButton(\"Cancel\");\n        this.cancelButton.addActionListener(new ActionListener() {\n            public void actionPerformed(ActionEvent event) {\n                close();\n            }\n        });\n\n        this.dialog = new JDialog(this.parentFrame, \"Sample\", true);\n        this.dialog.getContentPane().add(createPane());\n        this.dialog.getRootPane().setDefaultButton(this.okButton);\n        setEscapeKeyMap();\n        this.dialog.pack();\n        this.dialog.setLocationRelativeTo(this.parentFrame);\n    }\n\n    private Container createPane() {\n        JPanel topPanel = new JPanel(new FlowLayout());\n        topPanel.add(new Label(\"Something\"));\n        topPanel.add(this.field);\n\n        JPanel bottomPanel = new JPanel();\n        bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));\n        bottomPanel.add(Box.createHorizontalGlue());\n        bottomPanel.add(this.okButton);\n        bottomPanel.add(Box.createRigidArea(new Dimension(10, 0)));\n        bottomPanel.add(this.cancelButton);\n\n        JPanel mainPanel = new JPanel(new BorderLayout());\n        mainPanel.setBorder(BorderFactory.createEmptyBorder(9,9,9,9));\n        mainPanel.add(topPanel, BorderLayout.CENTER);\n        mainPanel.add(bottomPanel, BorderLayout.PAGE_END);\n        return mainPanel;\n    }\n\n    private void setEscapeKeyMap(){\n        String CANCEL_ACTION_KEY = \"CANCEL_ACTION_KEY\";\n        int noModifiers = 0;\n        KeyStroke escapeKey = KeyStroke.getKeyStroke(\n            KeyEvent.VK_ESCAPE, noModifiers, false);\n        InputMap inputMap = this.dialog.getRootPane().getInputMap(\n            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);\n        inputMap.put(escapeKey, CANCEL_ACTION_KEY);\n        AbstractAction cancelAction = new AbstractAction(){\n            public void actionPerformed(ActionEvent e){\n                close();\n            }\n        }; \n        this.dialog.getRootPane().getActionMap().put(\n            CANCEL_ACTION_KEY, cancelAction);\n    }\n\n    public void show() {\n        if (this.dialog == null) {\n            init();\n        }\n\n        this.field.setText(\"Initial value\");\n\n        this.dialog.show();\n    }\n\n    private void close() {\n        this.dialog.hide();\n    }\n  \n    private class OkButtonListener implements ActionListener {\n        public void actionPerformed(ActionEvent event) {\n            \/\/ Do work\n            close();\n        }\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Nearly a year ago, I posted some ideas for best practices for swing I neglected to add any examples, but today someone finally asked for one. So I threw together an example of a dialog window that uses composition rather than inheritance. Whew &#8212; there sure is a lot of boilerplate code just to get [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-57","post","type-post","status-publish","format-standard","hentry","category-geek"],"_links":{"self":[{"href":"https:\/\/jeremystein.com\/journal\/wp-json\/wp\/v2\/posts\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jeremystein.com\/journal\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jeremystein.com\/journal\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jeremystein.com\/journal\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jeremystein.com\/journal\/wp-json\/wp\/v2\/comments?post=57"}],"version-history":[{"count":1,"href":"https:\/\/jeremystein.com\/journal\/wp-json\/wp\/v2\/posts\/57\/revisions"}],"predecessor-version":[{"id":328,"href":"https:\/\/jeremystein.com\/journal\/wp-json\/wp\/v2\/posts\/57\/revisions\/328"}],"wp:attachment":[{"href":"https:\/\/jeremystein.com\/journal\/wp-json\/wp\/v2\/media?parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jeremystein.com\/journal\/wp-json\/wp\/v2\/categories?post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jeremystein.com\/journal\/wp-json\/wp\/v2\/tags?post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}