1

i have written this code for showing image instead of button following This answer

My code is:

JButton btnCalender;
    try {
        
        BufferedImage calendarIcon = ImageIO.read(new File("Calendar_0.jpg"));
        btnCalender = new JButton("Calendar", new ImageIcon(calendarIcon));
        btnCalender.setBorder(BorderFactory.createEmptyBorder());
        btnCalender.setContentAreaFilled(false);
        btnCalender.setBounds(244, 177, 129, 36);
        frmOptions.getContentPane().add(btnCalender);
        btnCalender.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    

But image is not showing. The button area is blank. All images are in Project folder. Why This is happening? Please give me solution. Thanks.

Edit: Solution found. My image resulation was too big. I've compressed this into 144*144. Now it is working.

Community
  • 1
  • 1
Amir Hossain
  • 184
  • 11

6 Answers6

1

The code you posted looks fine. Without the rest of the code it is hard to tell for sure, but it appears that the issue you are having is related to actually putting the button on the GUI.

Double-check that aren't getting any IOExceptions finding/loading the image.

jzd
  • 23,473
  • 9
  • 54
  • 76
  • There is no I/O exception. I've checked this. Images are in Project folder also. But still now no result. – Amir Hossain Nov 21 '13 at 15:37
  • Please post an SSCCE because there is something else going on that you aren't including then. – jzd Nov 21 '13 at 15:51
0

Have you tried adding:

btnCalender.setContentAreaFilled(false);
Octoshape
  • 1,131
  • 8
  • 26
0

try removing border and content area like this:

BufferedImage calendarIcon = ImageIO.read(new File("Calendar_0.jpg"));
btnCalender = new JButton("Back", new ImageIcon(calendarIcon));
btnCalender.setBorder(BorderFactory.createEmptyBorder());
btnCalender.setContentAreaFilled(false);
jandresrodriguez
  • 794
  • 7
  • 18
0

There is two things you can try, firstly make sure you have specified the layout "exact positioning" for the contentPane
frmOptions.getContentPane().setLayout(null);
and also make sure you repaint your contentPane after you've added the jbutton component like this
frmOptions.getContentPane().repaint();
Remember to put it AFTER you have added your button to the contentPane!

Linus
  • 1,516
  • 17
  • 35
0
JFrame f = new JFrame("This is a test");
    f.setSize(400, 150);
    Container content = f.getContentPane();
    BufferedImage calendarIcon = ImageIO.read(new File("C:\\Documents and Settings\\sugandhan\\Desktop\\suganthan.jpg"));
    content.add(new JButton("Calendar", new ImageIcon(calendarIcon)));
    f.setVisible(true);

And Please check you image path

0

First of all you don't need a bufferedImage to do this, secondly you have to correct the link of the image like this :

JFrame frmOptions = new JFrame();

JButton btnCalender = new JButton("Calendar", new ImageIcon(
        "./src/Calendar_0.jpg"));
btnCalender.setBorder(BorderFactory.createEmptyBorder());
btnCalender.setContentAreaFilled(false);
btnCalender.setBounds(244, 177, 129, 36);

btnCalender.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    }
});
frmOptions.getContentPane().add(btnCalender);
frmOptions.setDefaultCloseOperation(3);
frmOptions.pack();
frmOptions.setLocationRelativeTo(null);
frmOptions.setVisible(true);

I hope that helps, if so make it up please ;) Salam

BilalDja
  • 1,072
  • 1
  • 9
  • 17