Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #1273 > unrolled thread

How do you set an Icon in

Started by"Phil Powell" <phil.powell@THRWHITE.remove-dii-this>
First post2011-04-27 15:32 +0000
Last post2011-04-27 15:32 +0000
Articles 3 — 2 participants

Back to article view | Back to comp.lang.java.gui


Contents

  How do you set an Icon in "Phil Powell" <phil.powell@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000
    Re: How do you set an Ico "Ian Wilson" <ian.wilson@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000
      Re: How do you set an Ico "Phil Powell" <phil.powell@THRWHITE.remove-dii-this> - 2011-04-27 15:32 +0000

#1273 — How do you set an Icon in

From"Phil Powell" <phil.powell@THRWHITE.remove-dii-this>
Date2011-04-27 15:32 +0000
SubjectHow do you set an Icon in
Message-ID<1174578176.010750.176730@l77g2000hsb.googlegroups.com>
  To: comp.lang.java.gui
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

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#1275 — Re: How do you set an Ico

From"Ian Wilson" <ian.wilson@THRWHITE.remove-dii-this>
Date2011-04-27 15:32 +0000
SubjectRe: How do you set an Ico
Message-ID<4602ab60$0$27120$db0fefd9@news.zen.co.uk>
In reply to#1273
  To: comp.lang.java.gui
Phil Powell wrote:
> 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);

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());
>                 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
>

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1282 — Re: How do you set an Ico

From"Phil Powell" <phil.powell@THRWHITE.remove-dii-this>
Date2011-04-27 15:32 +0000
SubjectRe: How do you set an Ico
Message-ID<1174658989.463309.19530@n59g2000hsh.googlegroups.com>
In reply to#1275
  To: comp.lang.java.gui
On Mar 22, 12:14 pm, Ian Wilson <scoblo...@infotop.co.uk> wrote:
> Phil Powell wrote:
> > 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);
>
> 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)
>

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());
> >                 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

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web