Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How do you set an Icon into a JButton?

807599Mar 22 2007 — edited Mar 23 2007
I thought my method would handle this dynamically, but I was way off:
       /**
         * Handle {@link javax.swing.JButton}
         * @param button {@link javax.swing.JButton}
         */
        private void handleButton(JButton button) {
            try {
                String text = button.getText();
                ImageIcon icon = new ImageIcon(
                        System.getProperty("com.ppowell.applications.globals.rockpaperscissorsglobals.SRC_PATH") +
                        FileFunctionality.buildFilePath(RockPaperScissorsGlobals.IMAGE_PATH_ARRAY, true) /* EXCLUDE 1 BACKSLASH */ +
                        File.separator +
                        button.getText().toLowerCase().trim() + ".ico");
                button = new JButton(text, icon);
                Dimension dim = new Dimension(icon.getIconWidth(), icon.getIconHeight());
                button.setSize(dim);
                button.setPreferredSize(dim);
                button.setMaximumSize(dim);
            } catch (Exception e) {
                e.printStackTrace();
            }
            button.setFocusable(false);
            button.setSelected(false);
            button.addActionListener(RockPaperScissors.this);
            button.setMnemonic(button.getText().charAt(0));
            button.setFont(RockPaperScissorsGlobals.FONT);
        }
Instead I see JButtons with no icons, looking dead normal, and worse, setFocusable(false) is ignored; the JButtons look like they've been clicked!

What did I do so wrong this time?
Thanx
Phil
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 20 2007
Added on Mar 22 2007
30 comments
1,839 views