How to add mouse listener to JOptionPane button?

Dalibor Krivokapic :

I want to change appearance of Button on JOptionPane.ShowMessageDialog. I have managed to change Button caption with

UIManager.put("OptionPane.okButtonText", "Text I want");

Now, my next goal is to make Button work same as buttons in rest of my app. That is, when hovering mouse over it, it changes background and font color. On rest of my buttons I added mouse listener like this one:

    //setting change color on hover
        private final MouseListener mouseAction = new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                JButton rollOver = (JButton)e.getSource();
                if (rollOver.isEnabled()) {
                    rollOver.setBackground(new Color(163, 184, 204));
                    rollOver.setForeground(Color.WHITE);
                    rollOver.setFont(b);
                }
            };

            @Override
            public void mouseExited(MouseEvent e) {
                JButton rollOver = (JButton)e.getSource();
                if (rollOver.isEnabled()) {
                    rollOver.setBackground(new Color(230, 230, 230));
                    rollOver.setForeground(Color.BLACK);
                    rollOver.setFont(f);
                }
            };
        };

Previously in code I have Font varibles set:

    Font f = new Font("System", Font.PLAIN, 12);
    Font b = new Font("System", Font.BOLD, 12);

I could make new dialogs from scratch and implent this behaviour but that would be overkill.

Is there some way to access Button on JOptionPane and add mouse listener to it?

camickr :
UIManager.put("OptionPane.okButtonText", "Text I want");

The above will change the text for all "Ok" buttons on all JOptionPanes that you create.

If you want to change the text on an individual button on a specific JOptionPane then read the section from the Swing tutorial on Customizing Button Text.

Is there some way to access Button on JOptionPane and add mouse listener to it?

When you use the static showXXX(...) methods a modal JDialog is created so you don't have access to the dialog or its components until the dialog is closed which is too late.

So instead you need to manually create the JOptionPane and add it to a JDialog. The basics of doing this can be found by reading the JOptionPane API and looking at the section titled "Direct Use".

Once you have created the JOptionPane (and before you make the dialog visible) you can then search the option pane for the buttons and add a MouseListener to each button. To help you with this you can use the Swing Utils class. It will do a recursive search of the option pane and return the buttons to you in a List. You can then iterate through the List and add the MouseListener.

The basic code using this helper class would be:

JOptionPane optionPane = new JOptionPane(
    "Are you sure you want to exit the application",
    JOptionPane.QUESTION_MESSAGE,
    JOptionPane.YES_NO_CANCEL_OPTION);

List<JButton> buttons = SwingUtils.getDescendantsOfType(JButton.class, optionPane, true);

for (JButton button: buttons)
{
    System.out.println( button.getText() );
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325421&siteId=1