alynurly
alynurly

Reputation: 1849

Setting different background color for icon of Jbutton

Let's say i have a Jbutton where it has both ImageIcon and text, is it possible to set background color to icon filling it's buttons width?

Jbutton with text and icon

Upvotes: 1

Views: 460

Answers (1)

c0der
c0der

Reputation: 18792

Consider setting the button's background and using an icon with transparent background:

    JButton button = new JButton("Sweety");
    URL url = new URL("https://findicons.com/files/icons/345/summer/128/cake.png");
    button.setIcon(new ImageIcon(url));
    button.setVerticalTextPosition(SwingConstants.BOTTOM);
    button.setHorizontalTextPosition(SwingConstants.CENTER);
    button.setBackground(Color.YELLOW);

enter image description here

Upvotes: 1

Related Questions