Custom Button Not Working on MAC (Buttonui)

SwiftUI Button Size/Shape/Color Not Changing

You have to use .buttonStyle(PlainButtonStyle()) to get rid of the default button UI.

Button(action: {
print("Delete button tapped!")
}) {
HStack {
Image(systemName: "trash")
.resizable()
.scaledToFit()
.imageScale(.large)
Text("Delete")
}
.padding()
.background(
Capsule().strokeBorder(Color.white, lineWidth: 1.25)
)
}
.accentColor(Color.white)
.buttonStyle(PlainButtonStyle())

Java/swing font resize not working across platforms

The component's default UI delegate on your platform may be pre-empting your setting. In the alternative, consider one of the following:

  • Use deriveFont() on the existing font, illustrated here.

    component.getFont().deriveFont(16f)
  • Use an available sizeVariant, illustrated here.

size variant

  • Use a custom UI delegate, illustrated here and here.

custom UI delegate

Creating a custom button in Java with JButton

You will have to extend JButton class not AbstractButton. Try the following things and you will get idea.

The first move is to subclass JButton.

Then, in your subclass, start by redefining the paintComponent(Graphics) method. If you want any changes.

Then, override paintBorder(Graphics) to give it a shape of hexagon.

Create GridBagLayout fixed size buttons

As an alternative, consider using an available sizeVariant, illustrated here.

image

If you pursue a custom ButtonUI approach, shown in the last row, please ensure that the resulting buttons are no smaller than their preferred size. Doing so in your implementation of getPreferredSize() will help avoid the pitfall seen here.

JButton not resizing with setMaximumSize()?

It's because the frame('s contentPane) has a BorderLayout by default. You add the buttons to BorderLayout.CENTER, so the layout manager ignores the minimum-, preferred- and maximumSize.

I just want them to come up small and side by side

For that you could use a simple FlowLayout. (And if you want them to be centered on the frame, a parent JPanel with a GridBagLayout)

If you want a custom width & height for the buttons, override their getPreferredSize method. Overriding this method is safer than calling setPreferredSize.


Example:

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Example {

public Example() {

JButton button1 = new JButton("Hello button1");
JButton button2 = new JButton("Hello button2") {
@Override
public Dimension getPreferredSize() {
int width = super.getPreferredSize().width;
return new Dimension(width, width);
}
};;

JPanel buttonPanel = new JPanel();
buttonPanel.add(button1);
buttonPanel.add(button2);

JPanel contentPanel = new JPanel(new GridBagLayout());
contentPanel.add(buttonPanel);

JFrame frame = new JFrame();
frame.setContentPane(contentPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Example();
}
});
}

}

JButton will not display background color

I would eventually like a JButton to change color when pressed

here are two different ways (note JButton has an arrays of colors in UIManager)

  • override events from ButtonModel (ChangeListener or the same methods are implemented e.g. isPressed, isArmed in JButtons API), accelators are valid for mouse and KeyEvens (selection or focusInWindow)

  • override BasicsButtonUI (for real project)

I am using a mac, and I'm wondering if it has something to do with the
default button settings, but I don't know how to change/override
these.

  • depends of Look and Feel that your Java uses (Quaqua - default on OSX or standards by Oracle Metal, Nimbus ...)

Synthetica JButton Margins

Several possibilities should be considered:

  • Manipulate the button's bound properties: setBorderPainted(false), et al., as suggested here.

  • See if a suitable sizeVariant is available, as shown here.

  • Use a custom UI delegate based on BasicButtonUI, as shown here and here.



Related Topics



Leave a reply



Submit