Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Square Buttons in Swing

I would like to create something like the following in Swing:

enter image description here

The top part is relatively easy: I can just create a table and display it. What I'm having trouble with is the square plus and minus buttons at the bottom, which are designed to add a new item or remove the selected item respectively. In particular, I haven't been able to make the square shape because on Mac OS X and some other platforms, JButtons are rectangles with rounded corners and I can't find a way to change that. Also, I'm wanting to make sure it's a perfect square and without any space in between buttons.

How can this be accomplished in a cross-platform way on Swing?

like image 677
Thunderforge Avatar asked Sep 14 '25 10:09

Thunderforge


2 Answers

JButtons are rectangles with rounded corners and I can't find a way to change that.

Change the Border:

button.setBorder( new LineBorder(Color.BLACK) );

Edit.

Another approach is to create your own icon from an existing button. Something like the following:

JButton button = new JButton("+");
Dimension size = button.getPreferredSize();
size.x += 6;
size.y += 6;
button.setPreferredSize(size);
Rectangle rectangle = new Rectangle(3, 3, size.x - 3, size.y - 3);
ScreenImage buttonImage = ScreenImage(button, rectangle);
ImageIcon icon = new ImageIcon(buttonImage);

JButton plus = new JButton(icon);
plus.setBorder( ... );

The above code should create an image of your button on any platform. I increased the preferred size to avoid taking an image of the border.

You will need to use the Screen Image class.

like image 130
camickr Avatar answered Sep 16 '25 00:09

camickr


This is most easily achieved by returning a preferred size that is NxN - where N is the larger of preferred width or height.

Square Buttons

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

class SquareButton extends JButton {

    SquareButton(String s) {
        super(s);
    }

    @Override
    public Dimension getPreferredSize() {
        Dimension d = super.getPreferredSize();
        int s = (int)(d.getWidth()<d.getHeight() ? d.getHeight() : d.getWidth());
        return new Dimension (s,s);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JComponent gui = new JPanel(new FlowLayout());
                for (int ii=0; ii<5; ii++) {
                    gui.add(new SquareButton("" + ii));
                }

                gui.setBorder(new EmptyBorder(4, 8, 4, 8));

                JFrame f = new JFrame("Square Buttons");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };

        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}
like image 45
Andrew Thompson Avatar answered Sep 16 '25 01:09

Andrew Thompson