How can I make flush JButtons in swing?

matt

I am doing a layout, and I have a pair of buttons that I would like to be flush together. I can think of a couple ways to accomplish this, but I am wondering if there is a good way.

Here is some template code:

package helloworld;

import javax.swing.BoxLayout;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.EventQueue;
/**
 * Created by matt on 22/07/16.
 */
public class FlushButtons {

    private void showGui(){
        JFrame frame = new JFrame();

        JPanel panel = new JPanel();
        JButton plus = new JButton("+");
        JButton minus = new JButton("-");

        panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
        panel.add(plus);
        panel.add(minus);

        frame.setContentPane(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args){

        EventQueue.invokeLater(()->{
            new FlushButtons().showGui();
        });

    }
}

That produces the image on the left, but I would like it to look more like the image on the right.default layout desired layout

I tried using JButton#setMargin, but that did not have any effect. I have used JButton#setPreferredSize which I can get the desired result, but I need to know the size of the button beforehand.

Andrew Thompson

Here's my solution which looks ..pretty neat!

enter image description here

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class FlushButtons {

    private JComponent ui = null;

    FlushButtons() {
        initUI();
    }

    private JButton getFlushButton(String text) {
        JButton b = new JButton();

        b.setBorderPainted(false);
        b.setContentAreaFilled(false);
        b.setMargin(new Insets(0,0,0,0));
        b.setBorder(null);
        b.setIcon(new ImageIcon(getImageOfText(text, Color.GREEN.darker())));
        b.setRolloverIcon(new ImageIcon(getImageOfText(text, Color.ORANGE)));
        b.setPressedIcon(new ImageIcon(getImageOfText(text, Color.RED)));

        return b;
    }

    private BufferedImage getImageOfText(String text, Color color) {
        int s = 24;
        BufferedImage bi = new BufferedImage(s, s, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();

        g.setColor(color);
        g.fillRect(0, 0, s, s);
        g.setColor(Color.BLACK);
        g.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16));
        // use better ways to position text..
        g.drawString(text, 8, 16);

        g.dispose();
        return bi;
    }

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

        ui = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        ui.add(getFlushButton("+"));
        ui.add(getFlushButton("-"));
    }

    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) {
                }
                FlushButtons o = new FlushButtons();

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to make array of 15000 jbuttons in swing java jframe and set action against each row with button?

From Dev

How to make an event in a matrix of JButtons?

From Dev

How to make Round JButtons in Java

From Dev

How can I flush the DNS on Ubuntu 17.04?

From Dev

How can I force AmazonSQSBufferedAsyncClient to flush messages?

From Dev

How can I manually flush a boost log?

From Dev

How can I flush the DNS on Ubuntu 17.04?

From Dev

Windows - How can I flush modified memory?

From Dev

How can I get the img flush in the corner?

From Dev

How can i add an image with swing and how do i make it the size of my window?

From Dev

Java Swing - How can I make next and previous buttons to display variables of objects in an array?

From Dev

How to make JButtons visible on a JPanel with an image as background

From Dev

Swing - positioning some JButtons

From Dev

Java 8 + Swing: How to Draw Flush Polygons

From Dev

Java 8 + Swing: How to Draw Flush Polygons

From Dev

Java Swing: How to remove the default-spacing for JButtons in a JPanel

From Dev

In bash, How can I force a flush of an incomplete line printed to the terminal

From Dev

how can I flush all redis nodes through predis?

From Dev

JAVA : SWING : How can i build a .exe for Swing application

From Dev

How can I create this component in Swing?

From Dev

How to make browser auto flush?

From Dev

Can I 'flush' UIGestureRecognizer events?

From Dev

How do I create spacing in between JButtons?

From Dev

Dinamic of JButtons Swing with action listener

From Dev

Cant figure out how to make my JButtons show up

From Dev

Can I use JButtons added to a JToolBar to scroll up and down a JPanel?

From Dev

How can I make this repeat?

From Dev

How can I make slope

From Dev

How can I make this recursive?

Related Related

  1. 1

    how to make array of 15000 jbuttons in swing java jframe and set action against each row with button?

  2. 2

    How to make an event in a matrix of JButtons?

  3. 3

    How to make Round JButtons in Java

  4. 4

    How can I flush the DNS on Ubuntu 17.04?

  5. 5

    How can I force AmazonSQSBufferedAsyncClient to flush messages?

  6. 6

    How can I manually flush a boost log?

  7. 7

    How can I flush the DNS on Ubuntu 17.04?

  8. 8

    Windows - How can I flush modified memory?

  9. 9

    How can I get the img flush in the corner?

  10. 10

    How can i add an image with swing and how do i make it the size of my window?

  11. 11

    Java Swing - How can I make next and previous buttons to display variables of objects in an array?

  12. 12

    How to make JButtons visible on a JPanel with an image as background

  13. 13

    Swing - positioning some JButtons

  14. 14

    Java 8 + Swing: How to Draw Flush Polygons

  15. 15

    Java 8 + Swing: How to Draw Flush Polygons

  16. 16

    Java Swing: How to remove the default-spacing for JButtons in a JPanel

  17. 17

    In bash, How can I force a flush of an incomplete line printed to the terminal

  18. 18

    how can I flush all redis nodes through predis?

  19. 19

    JAVA : SWING : How can i build a .exe for Swing application

  20. 20

    How can I create this component in Swing?

  21. 21

    How to make browser auto flush?

  22. 22

    Can I 'flush' UIGestureRecognizer events?

  23. 23

    How do I create spacing in between JButtons?

  24. 24

    Dinamic of JButtons Swing with action listener

  25. 25

    Cant figure out how to make my JButtons show up

  26. 26

    Can I use JButtons added to a JToolBar to scroll up and down a JPanel?

  27. 27

    How can I make this repeat?

  28. 28

    How can I make slope

  29. 29

    How can I make this recursive?

HotTag

Archive