0

I'm using Corretto Java 11 and attempting to

  1. remove the focus around a JButton on a Toolbar when the mouse moves over it
  2. remove the border around the JButton
  3. 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.

enter image description here

The following shows the focus appearing around the button when the mouse is moved over it.

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Unhandled Exception
  • 1,427
  • 14
  • 30
  • *Unfortunately, none of these worked* - did you try them on a regular button without custom painting? I think you will find they do work. So the issue is your custom painting. When I use your commented out fillRoundRect(...) statement I don't notice a problem. Also, when I changed the arc width/height to 6 I got better rounded corners. Don't set the 3 sizes of the button. Instead you should pass the width/height as a parameter to your EmptyIcon and then return those values for the icon width/height. – camickr Nov 18 '21 at 17:18
  • You might be able to create a BufferedImage and paint the image with rounded borders the size you want. This should allow anti aliasing to work better. Then use the image to create your Icon. – camickr Nov 18 '21 at 17:31
  • Thanks for the feedback but not sure I fully followed your first comment. Are you saying to not make these 3 calls b2.setPreferredSize(buttonSize); b2.setMinimumSize(buttonSize); b2.setMaximumSize(buttonSize);? How does passing in the width/height to the EmptyIcon resize it? I even checked the IconImage class instead of the Interface Icon and neither provide methods for the sizing. – Unhandled Exception Nov 18 '21 at 17:50
  • The getIconWidth/Height methods should NOT return 0. They should return the size of the Icon. Then the label will be able to calculate the 3 size automatically. – camickr Nov 18 '21 at 22:12

0 Answers0