Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2007

Tip: Looking for answers? Try searching our database.

How do you set an Icon into a JButton?

Thread view: 
Phil Powell - 22 Mar 2007 16:42 GMT
I thought my method would handle this dynamically, but I was way off:

[code]
      /**
        * 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);
       }
[/code]

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
Ian Wilson - 22 Mar 2007 17:14 GMT
> I thought my method would handle this dynamically, but I was way off:
>
[quoted text clipped - 17 lines]
> ".ico");
>                 button = new JButton(text, icon);

After this statement, button is no longer "pointing at" (a reference to)
the button displayed on your panel. From here on you are manipulating a
completely separate and invisible button (invisible because it hasn't
been added to any visible component).

Replace the above line with
  button.setText(text);
  button.setIcon(icon);
(Untested - I've no idea if these are the appropriate methods)

>                 Dimension dim = new Dimension(icon.getIconWidth(),
> icon.getIconHeight());
[quoted text clipped - 19 lines]
> Thanx
> Phil
Phil Powell - 23 Mar 2007 15:09 GMT
> > I thought my method would handle this dynamically, but I was way off:
>
[quoted text clipped - 27 lines]
>    button.setIcon(icon);
> (Untested - I've no idea if these are the appropriate methods)

Thanx! Here is the fixed method:

      /**
        * Handle {@link javax.swing.JButton}
        * @param button {@link javax.swing.JButton}
        */
       private void handleButton(JButton button) {
           try {
               ImageIcon icon = IconHandler.createImageIcon(

System.getProperty("com.ppowell.applications.globals.rockpaperscissorsglobals.FILE_SRC_PATH")
+

FileFunctionality.buildFilePath(RockPaperScissorsGlobals.IMAGE_PATH_ARRAY) /
* EXCLUDE 1 BACKSLASH */ +
                       File.separator +
                       button.getText().toLowerCase().trim() +
                       "." +

System.getProperty("com.ppowell.applications.globals.rockpaperscissorsglobals."
+
                       button.getText() + "_ICON_EXT"));
               icon = IconHandler.scaleImageIcon(icon, 16, 16);
               button.setIcon(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);
       }

The problem resulted in my attempt to set an .ico file as Icon, and
apparently that is not possible in Java as ".ico" is not recognized as
a legitimate image.  Switching to a JPEG image instead was just fine.

> >                 Dimension dim = new Dimension(icon.getIconWidth(),
> > icon.getIconHeight());
[quoted text clipped - 19 lines]
> > Thanx
> > Phil


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.