title: How to create a custom JButton in java with an image base?
body: <p>I recently read this thread (<a href="https://stackoverflow.com/questions/2158/creating-a-custom-button-in-java">Creating a custom button in Java</a>) on creating custom buttons in java by extending the JButton class, however all the solutions on this thread use graphics drawn in java.</p>
  
  <p>I wanted to have my button based on a button image I had drawn in photoshop. So I tried to apply what I read in that thread with this result:</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  
  public class nextButton extends JButton {
      @Override
          protected void paintComponent(Graphics g) {
          Image image = new ImageIcon("nextButton.png").getImage();
          g.drawImage(image,0,0,this);
  }
  
      @Override
      public Dimension getPreferredSize() {
          Dimension size = super.getPreferredSize();
          size.setSize(75, 150);
          return size;
      }
  }
  </code></pre>
  
  <p>When I run the main program having added this button to a JPanel it doesn't display. I am assuming it could be one of several reasons:</p>
  
  <p>a) The size of the JButton doesn't match the image?
  b) I haven't loaded the image properly. In the notes my lecturer gave me he writes out the display image code with just "imageName.png" with no file path so I have no idea if this is the correct way to do it, or how the program will know to load the image.
  c) Something else which is beyond my knowledge so far.</p>
  
  <p>If anyone knows how to solve this I'd be very grateful.</p>
  
  <p>Thanks so much!</p>