DarthDonut
DarthDonut

Reputation: 123

JButton Icon a little bit off

I tried to program a small TicTacToe and gave the buttons I used for the game an icon depending on which player marked that button (you know, the traditional cross and circle).

Now, when I check my buttons "in game", the icons are a little bit off; there is a small (maybe 10 px big) gap between the icon and the button border.

I already tried this but it didn't work:

button.setHorizontalAlignement(SwingConstants.RIGHT)

Example Code:

JButton button = new JButton();
button.setPreferredSize(new Dimension(175,175));   //Note: Image is also 175x175
button.addActionListener(new MyOnClickListener());

...

class MyOnClickListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e){
        JButton button = (JButton) e.getSource();
        ImageIcon myIcon = new ImageIcon("source");
        button.setEnabled(false);
        button.setIcon(myIcon);
        button.setDisabledIcon(myIcon);
    }
}

Screenshot of button

See that little white margin to the right? This is what i don't want. I want the icon to fill the button completely. Here's the icon: Icon

Upvotes: 2

Views: 404

Answers (1)

Andrew Thompson
Andrew Thompson

Reputation: 168825

To remove extraneous space, set the border of the button to null. This might require (in some PLAFs) changing the look of the icon itself to indicate focus, hover, pressed etc.

In this screenshot, the middle button in the right hand column is focused, while the mouse is hovering over the middle button in the bottom row.

enter image description here

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.imageio.ImageIO;
import java.net.URL;

public class TicTacToeButtons {

    private JComponent ui = null;
    private String path = "https://i.sstatic.net/sAU9n.png";
    private BufferedImage image;
    Image transparentImage;

    private JButton getButton(int i) {
        Image img = (i%2==0 ? image : transparentImage);
        JButton b = new JButton(new ImageIcon(img));
        b.setBorder(null);
        return b;
    }

    TicTacToeButtons() {
        try {
            initUI();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void initUI() throws Exception {
        if (ui!=null) return;

        image = ImageIO.read(new URL(path));
        transparentImage = new BufferedImage(
                image.getWidth(),image.getHeight(),BufferedImage.TYPE_INT_ARGB);

        ui = new JPanel(new GridLayout(3,3));
        ui.setBorder(new EmptyBorder(4,4,4,4));
        for (int ii=0; ii<9; ii++) {
            ui.add(getButton(ii));
        }
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                TicTacToeButtons o = new TicTacToeButtons();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

Upvotes: 1

Related Questions