Amir Hossain
Amir Hossain

Reputation: 184

Code is not working for swing image button

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.

Upvotes: 1

Views: 136

Answers (6)

BilalDja
BilalDja

Reputation: 1092

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

Upvotes: 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

Upvotes: 0

Linus
Linus

Reputation: 1518

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!

Upvotes: 0

jzd
jzd

Reputation: 23629

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.

Upvotes: 1

jandresrodriguez
jandresrodriguez

Reputation: 794

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

Upvotes: 0

Octoshape
Octoshape

Reputation: 1141

Have you tried adding:

btnCalender.setContentAreaFilled(false);

Upvotes: 0

Related Questions