开发者

Accessing JOption Pane's button to add MouseListener to them

Right, I thought this wo开发者_StackOverflow社区uld be relatively simple by using .getComponents() on the Component which would return the JPanel of the JOptionPane and them retrieve the JButtons by using that method again with the JPanel however I am facing difficulties.

I want to use a mouse listener on the JOptionPane buttons so that I can change the color of the button on rollover. Is there a simpler way of achieving this?

This is my class so far ..

package rsapp.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;


public class RSJPaneComponent extends JOptionPane {

    /**
     * 
     */
    private static final long serialVersionUID = 13453253L;
    private JOptionPane j=this;
    final Color WHITE = Color.WHITE;


    public RSJPaneComponent(){
        UIManager.put("OptionPane.background",WHITE);
        UIManager.put("Panel.background",WHITE);
        UIManager.put("Button.background",WHITE);
        UIManager.put("Button.foreground",new Color(85,153,187));
        UIManager.put("activeCaption", WHITE);
    }

    protected String initJPaneInput(final JFrame f, final String message){
        return j.showInputDialog(f,message);
    }

    public int generateDialog(int error_code, String title_message, String message, final JFrame f){
        return  JOptionPane.showConfirmDialog(
                f,
                message,
                "Error "+error_code+": "+title_message,
                JOptionPane.YES_NO_OPTION);
    }
}


Is there a simpler way of achieving this?

Use a JDialog. Long experience tells me that while a JOptionPane is a powerful & handy component, once it comes to customizing it, you are better off simply using a JDialog.


Code

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

class CustomDialog {

    public static void main(String[] args) {

        Runnable r = new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Show Custom Dialog");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(400,400);
                frame.setLocationRelativeTo(null);

                final JDialog dialog = new JDialog(frame, "Dialog", true);

                JPanel mainGui = new JPanel(new BorderLayout());
                mainGui.setBorder(new EmptyBorder(20,20,20,20));
                mainGui.add( new JLabel("Contents go here"), BorderLayout.CENTER );

                JPanel buttonPanel = new JPanel(new FlowLayout());
                mainGui.add(buttonPanel, BorderLayout.SOUTH);

                JButton close = new JButton("Close");
                close.addActionListener( new ActionListener(){
                    public void actionPerformed(ActionEvent ae) {
                        dialog.setVisible(false);
                    }
                } );

                buttonPanel.add(close);

                frame.setVisible(true);

                dialog.setContentPane(mainGui);
                dialog.pack();
                dialog.setLocationRelativeTo(frame);
                dialog.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

Screen Shot

Accessing JOption Pane's button to add MouseListener to them


Note that this example does not yet have all the functionality built-in to a JOptionPane.

For example, if a JOptionPane is open and the user presses the escape key, the dialog will be dismissed. You might add that functionality using a KeyListener or ActionMap.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜