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.
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.
Here's my solution which looks ..pretty neat!
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.
Comments