0

I put an imageIcon on JButton but it is just half of button. So, how can I make icon to be over whole JButton. Here is my code:

public static ImageIcon image;
...

try{
           BufferedImage img = null;
           img = ImageIO.read(new File("image.jpg"));
           //Image img1 = img.getScaledInstance(jButton.getWidth(), jButton.getHeight(), Image.SCALE_SMOOTH);
           Image img1 = img.getScaledInstance(40, 30, Image.SCALE_SMOOTH);
           image= new ImageIcon(img1);
         
        }
        catch(Exception ex){
          ex.printStackTrace();
        }
jButton.setIcon(image);

camickr
  • 321,443
  • 19
  • 166
  • 288

1 Answers1

1

Try this:

  try 
  {
    JButton button = new JButton();
    Image img = ImageIO.read(getClass().getResource("image.jpg"));
    button.setIcon(new ImageIcon(img));
    button.setMargin(new Insets(0, 0, 0, 0));
    button.setBorder(null);
    //...

  } 
  catch (Exception ex) {
    logger.error(ex);
  }

All of the code taken from the answers on this question. May be helpful.

aran
  • 10,978
  • 5
  • 39
  • 69