JButton with auto resizing icon

Hi,
I want to extend the JButon in order to make it auto resize the icon image to the button size itself.
I tried somethink like the following code and it work but there is some recursive side effect I cannot understand becouse I get a 100% CPU usage and from the print() I see the paint() method is called continuosly.
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
public class IconButton extends JButton {
private static final long serialVersionUID = 4049919355141829686L;
int cntr;
public IconButton(ImageIcon icon) {
super(icon);
public void paintComponent(Graphics g) {
System.out.println(cntr++);
Icon icn = getIcon();
if (icn instanceof ImageIcon) {
ImageIcon ii = (ImageIcon)icn;
Image img = ii.getImage();
img = img.getScaledInstance(getWidth(), getHeight(), Image.SCALE_AREA_AVERAGING);
icn = new ImageIcon(img);
super.setIcon(icn);
super.paintComponent(g);
Maybe there's a better way... for example by changing the icon only when the button size change: but how?
Thank in advance,
Carlo.

ok,
the question is:
how to have a Button with the icon automatically resized to the button size? Is it right to extends JButton or is better extends AbstractButton?
thanks.

Similar Messages

  • Dynamic flv player with auto resize based on content

    Hi,
    I would like to build a .flv player which can accommodate 7
    different video sizes.
    This will probably be made using php and xml to dynamically
    control content.
    What I would like to know is if and how it is possible to
    have a holder.swf (just looks for variables to display the flv)
    that can automatically resize based on the flv dimensions. I dont
    want any kind of browserscreen resolution resizing of content, just
    an swf that will change its size from say 320x240 to 450x253
    depending on the dynamically loaded flv.

    The same thing has been reported for the T410. You can see how to fix it here:
    http://forums.lenovo.com/t5/T400-T500-and-newer-T-series/T410-unstable-screen-brightness/m-p/212492#...
    It appears to occur anytime you are on battery and the intel graphics power plan is set to anything other than 'maximum performance.' It's pretty subtle, but really annoying when it happens.

  • How to put an icon to Jbutton with Jbuilder 5?

    I have tried to make a jbutton with icon on it,
    i create an image icon object and pass it to the jbutton.seticon method...
    When create the image icon, it need the path of the icon file, what is the relative path of image in jbuilder?
    (not absolute e.g file:///)
    Thanks

    What you need to do is
    if your current class is Test and you image file is in the same directory called cat.gif then do the following
    button.setIcon(new ImageIcon(Test.class.getResource("cat.gif")));
    this should work
    if you image file is in the previous directory you just type in "../cat.gif" and if you have a image directory reltaive then you put in "images/cat.gif"
    I hope this helps

  • JButton with Icon question

    In the Toolbar I have a JButtons. I created the JButtons with Icons.
    When I click on a button it is putting a dotted line right around the Icon. How can I get rid of that? Thanks.

    You're talking about the focus rectangle. If memory serves in the SwingSet demo there's an entire panel devoted to buttons and such. You can find something there to help you. The toggle in the example is labeled "Paint Focus", and it does what you want.

  • JButton with Icon greyed out

    Hi,
    I am writing a minesweeper program and I have a problem with the icons on the JButtons. Initially the button is set with a grey icon to cover the text normally visible on top of it. When the button is pressed two things happen:
    - its icon changes
    - it is disabled by setEnabled(false).
    My problem is that when when I use the setEnabled(false) method the icon is 'greyed' out. Does anyone know of a way round this?
    Thanks,
    Jolyon

    just set the disabledIcon to the desired Icon:
    button.setDisabledIcon(Icon icon );
    the grayed out one is a default Icon calculated by the button if no disabledIcon is provided.
    regards,
    stefan

  • JButton with icons

    I need to create JButton with icon but have not much idea. Is there any code sample that may give me some insight? Any help is appreciated in advance.Thanks.

    Icon play = new ImageIcon("images/Play24.gif");
    JButton PlayButton = new JButton(play);

  • Can i paint a JButton with icon?

    i have jbutton with icon and when i push on him i want him to look like when he doesnt Enabled with gray color but i want him to be enable...
    How i do this?

    i dont want to add icon i want to paint the button in gray color
    like you take a jbutton with icon and you do setEnabled(False)
    Thanks

  • PINCH no longer works in FINDER with Magic Trackpad when resizing icons such as photos. Help.

    I am a photographer and I find it VERY convenient to be able to use my Trackpad to PINCH resize icons of photos for quick review. This no longer works in LION. This needs to be restored or can someone tell me how to restore the setting. Thanks
    - Shane

    Not the same symptoms, but I noticed that after a few days of running great, odd things started happening on my iMac. The first thing was that when I would boot the Mac up for the first time in the morning, it would boot into single user mode. I have the Apple Bluetooth keyboard, so I know there's no way keys are being held down.
    The other symptom was that Microsoft Outlook 2011 would hang on launch. It would get stuck on the "Setting Up Identities..." phase. Rebuilding the Outlook database would help until the next time I shut the iMac down.
    Also a couple of times it got to the Desktop after a boot, but not everything was working correctly and some startup items didn't load.
    The iMac also kept booting into single user mode on the first boot.
    I tried reinstalling Mountain Lion, and I thought that fixed both problems, until the next morning. Now I just don't shut the Mac down. I can close Outlook, and it launches in a couple of seconds.
    So it would appear that something is becoming corrupt when OS X is shut down. If I don't shut it down, it's fine.

  • Something is wrong with Firefox's image auto resize

    In forums, when I post an image, it's seems to be doing an incorrect image auto resize, for the image is still large to be fit in the websites' pages, and when I CTRL+Scroll, the browser makes a proper resize though making the letters and numbers smaller.

    Resizing images automatically only works if you open an image in a tab and there is only that image to be shown. In all other cases it is the website that determines how large an image is displayed.<br />
    If you post an image on a forum and the forum doesn't resize the image automatically then use an upload host that automatically generates a thumbnail that you can show and that opens the full sized image if you click the thumbnail.

  • JButton with Icon - when pressed moves Icon

    When I press on a JButton which contains a icon then the icon is moved a little bit each time I press. Can I disable that?
    I need to place clickable images. Is a JButton the best method for this?

    URL url = this.getClass().getResource("weiher_original.jpg");
    ImageIcon icon = new ImageIcon(url);
    btnIcon = new JButton(icon);
    btnIcon.setSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
    btnIcon.setBorderPainted(false);
    btnIcon.setFocusPainted(false);
    getContentPane().add(btnIcon);Now if you press on the icon the image moves a few pixels until the mouse is released.

  • JTable in JScrollPane auto resize refresh problem

    Hello,
    I have a JTable in a JScrollPane. Number of rows is changing.
    I'm using the following to auto-resize JScrollPane.
    public Dimension getPreferredSize() {
                  Dimension size = super.getPreferredSize();
                  size.height -= getViewport().getPreferredSize().height;
                  Component view = getViewport().getView();
                  if(view != null)
                  size.height += view.getPreferredSize().height;
                  return size;
             }There is a JButton, which adds an empty row to the JTable. It all works fine, except the auto-resize when a new row is added. I want all rows of JTable to be visible, with no scrollbars present. I tried repaint(), revalidate(), addNotify(). What should I do?
    Thanks.

    Sure
    DefaultTableModel dtm = new DefaultTableModel(vec, header);
              jt0 = new JTable(dtm);
              jt0.getTableHeader().setReorderingAllowed(false);
              jt0.setFont(new Font("Tahoma", Font.PLAIN, 12));
              jt0.getTableHeader().setFont(new java.awt.Font("Tahoma", java.awt.Font.BOLD, 12));
              jt0.setRowHeight(18);
            jt0.setPreferredScrollableViewportSize(new Dimension(500, jt0.getRowCount() * jt0.getRowHeight()));
            jt0.setFillsViewportHeight(false);
              jsp0 = new JScrollPane(jt0);
    GridBagConstraints gbc = new GridBagConstraints(); 
            gbc.insets = new Insets(2,1,2,1); 
            gbc.weightx = 1.0; 
            gbc.weighty = 1.0; 
            JPanel p0 = new JPanel(new GridBagLayout()); 
            gbc.fill = gbc.HORIZONTAL;
            p0.add(jsp0, gbc);
              JButton jb1 = new JButton("add row");
              jb1.setSize(40, 18);
    jb1.addActionListener(new java.awt.event.ActionListener() {
                   public void actionPerformed(java.awt.event.ActionEvent e) {
                        dtm.addRow(new Object[]{....});
    //                    jt0.scrollRectToVisible(jt0.getCellRect(jt0.getModel().getRowCount()-1, 1, false));
    //                    jt0.setRowSelectionInterval(jt0.getModel().getRowCount()-1, jt0.getModel().getRowCount()-1);
        public class SizeX extends JScrollPane {
             public Dimension getPreferredSize() {
                  Dimension size = super.getPreferredSize();
                  size.height -= getViewport().getPreferredSize().height;
                  Component view = getViewport().getView();
                  if(view != null)
                  size.height += view.getPreferredSize().height;
                  return size;
        }

  • Trouble with auto size text in a single line form field

    Hi.  I'm having an issue with filling out a form in Adobe Reader on my iPad.  The text field is single line.  The font is set to "auto" so that the line of text will not exceed a certain width.  When filling out this form on my laptop using Adobe Reader, it works fine.  As I type additional text, once the field is filled, it shrinks the text font size.  On my iPad, however, it won't let me type any more text in the field once I reach the end.  Does Adobe Reader for iPad not support the "auto" font size in form fields?  And I did check that my version of Adobe Reader is up to date on my iPad, so I do have the latest version.
    Thanks in advance for your help,
    Ashley

    Actually, nevermind.  It must be something wrong with the file.  I tried opening another file and auto resize worked perfectly on it.  Weird though that it works on one file and not another.
    Thanks!
    Ashley

  • Auto resize webpage?

    i have just finished my site which can be seen here www.jbpermanentcosmetics.co.uk The site looks fine on my Macbook pro on Mac and windows but it is way too large for 4:3 monitors. The site width is 1200 which in hindsight is too large really but i don't want to have to resize the whole website. Is there any HTML code which i can add which will auto resize the page to avoid horizontal scrolling?

    rossn49 wrote:
    I found the following code which looks like it might do what i want:
    <SCRIPT language="JavaScript">
    <!--
    if ((screen.width>=1024) && (screen.height>=768))
    window.location="highres.html";
    else
    window.location="lowres.html";
    //-->
    </SCRIPT>
    rossn49, you can not use that javascript. It switches between two pages/sites (I bolded the code), which means you have to add another site with narrow width.... you indicated that you wouldn't like to do that.
    However, you can find the sweet spot for your site design; many people started to design their sites for regular computer and also accommodate mobile devices such as iPhone.
    You may like to do just that, iPhone's resolution is 960px wide, so I would conservatively take it down to 900px~920px wide.
    OT, thanks for the good words... I'd not read the forum today. I'd been spending time today to figure out and mount my GPS on my new motorscooter, maybe I will buy motorscootersaurus.com for my new site. LOL!

  • Macbook Pro Resize icons

    i read this on a article that i can resize my icons by using gestures. How can i do so because right now it isn't working..is their something i can enable. I already know about the finder/view/show view options and i hate resizing icons that way. Please tell me how i can resize my desktop icons with basic gestures.
    Change Icon Size
    If you are on a Macbook Pro, you can use the mousepad gestures (pinch/expand) to change the icon size on the desktop, in a folder or virtuall anywhere.

    You cannot resize Desktop icons individually. You can change the size of icons on the Desktop and in individual Finder windows either using COMMAND-J or using gestures.

  • Auto Resize Text in a Text Box

    How do you make the text inside of a text box auto resize to fit the box I'm typing in?
    I have a text box with a list in it. I change this list everyday and like the text to fit the box. The size of the text doesn't matter. If I have a long list, I don't mind that the font is small. If I have a short list that day, I don't mind that the font size is then big. Hopefully I am making sense. I used to use text boxes like this in MS Publisher and am assuming I just need to find a setting change in Pages.
    I do this often enough that it would save time not having to keep guessing the correct font size until it fits properly.
    Thanks!
    Message was edited by: mnbinth

    mnbinth wrote:
    I used to use text boxes like this in MS Publisher and am assuming I just need to find a setting change in Pages.
    Assuming that application B will behave the same than application A is always a bad idea.
    There is no reason for such a guess.
    Happily, all engineers aren't thinking the same way and remember Apple's statement :
    *_Think different !_*
    Yvan KOENIG (VALLAURIS, France) samedi 25 septembre 2010 10:55:56

Maybe you are looking for

  • GRE slow in one direction

    Hi folks I need some help understanding a problem I have. I have to networks connected over GRE using Cisco 1921 routers. One router is connected to the Internet and the other one to a 4G provider. 4G as I know it should be slower on the upload than

  • Moved house, being billed for broadband and equipm...

    We were approached by BT to upgrade our Broadband to Infinity. I explained that we were likely to move in the near future and the advisor said that we could transfer our account to the new house. I was suspicious that this would not be the case and m

  • IPod won't recognize on my new computer

    I got a new computer I installed Ipod software cd and iTunes and I can't get my Ipod to show up with my music on iTunes or in Windows. Any suggestions?

  • Need to execute a sub-VI continuously, while others are executed on demand.

    I'm running into a problem while trying to develop a set of controls for a scanning probe microscope. I have a PID control which governs the height of the probe above the sample (feedback input is through a PCI DAQ card), and which needs to run at al

  • JavaScript Adobe Live Cycle Checkbox aktivate Textbox

    Hello Community I have created a pdf form, and I have a checkbox is checked by default. Checked if this is not intended to be 3 edit fields. If this if it is not enabled Should all three fields to be editable In this exampele , RAL /Pantone / Andere