I'm using Corretto Java 11 and attempting to
- remove the focus around a JButton on a Toolbar when the mouse moves over it
- remove the border around the JButton
- avoid any borders when the JButton is clicked
Based on other threads related to removing the border, these are the various fixes.
//Attempts to remove Border
// b2.setBorderPainted(false);
// b2.setBorder(null);
// Border emptyBorder = BorderFactory.createEmptyBorder();
// b2.setBorder(emptyBorder);
Based on other threads related to removing the focus, these are the various fixes.
//Attempt to remove Focus
b2.setFocusPainted(false);
b2.setFocusable(false);
b2.setRolloverEnabled(false);
Unfortunately, none of these worked and I'm not sure if either the implementation was wrong or the newer version of Java does things different or if the behavior is different when within a Toolbar.
Here is the code currently being used.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
public class ToolBarSample {
public static void main(final String args[]) {
JFrame frame = new JFrame("JToolBar Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JToolBar toolbar = new JToolBar();
toolbar.setRollover(true);
JButton b2 = new JButton();
b2.setText("Big Button");
//Attempts to remove Border
// b2.setBorderPainted(false);
// b2.setBorder(null);
// Border emptyBorder = BorderFactory.createEmptyBorder();
// b2.setBorder(emptyBorder);
//Attempt to remove Focus
b2.setFocusPainted(false);
b2.setFocusable(false);
b2.setRolloverEnabled(false);
b2.setOpaque(true);
Dimension buttonSize = new Dimension(340,27);
b2.setPreferredSize(buttonSize);
b2.setMinimumSize(buttonSize);
b2.setMaximumSize(buttonSize);
b2.setIcon(new EmptyIcon(Color.RED));
b2.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
b2.setVerticalAlignment(SwingConstants.CENTER);
b2.setHorizontalTextPosition(JLabel.CENTER);
b2.setVerticalTextPosition(JLabel.CENTER);
toolbar.add(b2);
Container contentPane = frame.getContentPane();
contentPane.add(toolbar, BorderLayout.NORTH);
JTextArea textArea = new JTextArea();
JScrollPane pane = new JScrollPane(textArea);
contentPane.add(pane, BorderLayout.CENTER);
frame.setSize(550, 350);
frame.setVisible(true);
}
public final static class EmptyIcon implements Icon {
Color color ;
public EmptyIcon() {
this(Color.GREEN);
}
public EmptyIcon(Color color) {
this.color = color;
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y)
{
System.out.println("Inside paintIcon");
if(c != null)
{
if (this.color != null)
{
g.setColor(this.color);
}
// g.fillRoundRect(0, 0, c.getWidth() , c.getHeight(), 5, 5);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fillRoundRect(0 + 1, 3, c.getWidth() - 3 , c.getHeight() - 6, 3, 3);
}
}
@Override
public int getIconWidth() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getIconHeight() {
// TODO Auto-generated method stub
return 0;
}
}
}
Although the outline appears light here, it is more obvious based on the colors used.
The following shows the focus appearing around the button when the mouse is moved over it.