Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a macOS styled blue colored default button in Java?

emphasized textI currently write Java applications on Mac OS X Mountain Lion and I want to get a button in Java which is Mac OS X style blue colored.

How do I make my button look like the Mac OS X style blue colored default button which is displayed in JOptionPane.showMessageDialog()?

like image 985
Asrar Sunge Avatar asked May 14 '26 01:05

Asrar Sunge


1 Answers

I guess, you are looking for a default button.

JRootPane rootPane = SwingUtilities.getRootPane(button);
rootPane.setDefaultButton(button);

enter image description here

If you want to take a quick look at the result

import java.awt.*;
import javax.swing.*;

class ButtonFrame extends JFrame
{
  JButton button;

  ButtonFrame(String title)
  {
    super(title);
    setLayout(new FlowLayout());

    button = new JButton("OK");
    add(button);

    JRootPane rootPane = SwingUtilities.getRootPane(button);
    rootPane.setDefaultButton(button);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

public class Sample
{
  public static void main ( String[] args )
  {
    ButtonFrame frame = new ButtonFrame("Demo");

    frame.setSize(200,80);
    frame.setVisible(true);
  }
}

and then

javac Sample.java
java -cp . Sample
like image 59
Oo.oO Avatar answered May 15 '26 15:05

Oo.oO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!