1

I have modified the Font of JOptionPane using UIManager settings, but I am not able to find a property to change button attributes. For eg. I want to remove the border around the text as shown below. Also I want to remove the underline under Y and N as shown below.

enter image description here

Moreover, can I change the background color of JOptionPane from this Off-White color to something else ?

Gagan93
  • 1,826
  • 2
  • 25
  • 38
  • Realistically, this can be very easily, you could take a closer look at [`JOptionPane.showOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue)`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html#showOptionDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int,%20int,%20javax.swing.Icon,%20java.lang.Object[],%20java.lang.Object)), which would allow you to pass you custom buttons through the `options` parameter – MadProgrammer Sep 26 '14 at 12:13
  • But then you would become responsible for managing the state of the dialog, which is no easy task – MadProgrammer Sep 26 '14 at 12:14
  • As an [example](http://stackoverflow.com/questions/14591089/joptionpane-passing-custom-buttons/14591165#14591165) – MadProgrammer Sep 26 '14 at 12:15

1 Answers1

0

You can change individual dialogs:

You need a customJDialog and a custom JOptionPane. Buttons underline the first letter that matches the mnemonic. Change it to something that is not used. Rectangle is about button focus.

JDialog dialog = new JDialog();

JOptionPane pane = new JOptionPane("Some Message",
JOptionPane.PLAIN_MESSAGE, JOptionPane.YES_NO_OPTION);

List<JButton> list = SwingUtils.getDescendantsOfType(JButton.class, pane);
//this method is part of the SwingUtils. It is free to use. You can download it from the link.

pane.setBackground(Color.green); //this does not completely change the background. I am not sure how you can change it completely

for (JButton b : list) {
    b.setMnemonic(0); //call it with an int or char that does not exist in your button.
    b.setFocusable(false); //removes the rectangle
    b.setBackground(Color.red);
    //you can do more since you have direct access to the button
}

If you have no experience with java custom JOptionPane check this out for button actions: Pressing Yes/No on embedded JOptionPane has no effect

SwingUtil link:http://tips4java.wordpress.com/2008/11/13/swing-utils/

If you want to turn the code into library read: http://www.programcreek.com/2011/07/build-a-java-library-for-yourself/

Community
  • 1
  • 1
WVrock
  • 1,725
  • 3
  • 22
  • 30