How do I change the position of the icon in JOptionpane?

I'm wondering if it is possible to change the position of the icon in the JOptionPane from the left side to the right side?

public void popupMessage(){
    JCheckBox checkbox = new JCheckBox("Do not show this message again.");
        String message = "Attempt to set icon to right side is successfully approached.";
        Object[] params = {message, checkbox};
        int n = JOptionPane.showConfirmDialog(null, params, "Icon to right side",JOptionPane.YES_NO_CANCEL_OPTION);
        BasicOptionPaneUI.getIcon().paintIcon(  );
}

      

+3


source to share


1 answer


JPanel can be used. You just create a JPanel, add your own icon or an existing icon to the JLabel. Then add some text to another JLabel and add those JLabels to the JPanel. Using BorderLayout, you can control the position of the JLabel text and the JLabel icon.

Example (Ran and tested, works great):



public static void main(String[] args) 
{   
    Icon icon = UIManager.getIcon("OptionPane.errorIcon");
    JLabel iconLabel = new JLabel(icon);
    JLabel textLabel = new JLabel("Some text");

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(iconLabel,BorderLayout.EAST);
    panel.add(textLabel,BorderLayout.CENTER);

    JOptionPane.showMessageDialog(
            null,
            panel,
            "Hello", JOptionPane.PLAIN_MESSAGE);

}

      

0


source







All Articles