Reputation: 67
I'm wondering if it is possible to change location of icon in JOptionPane from 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( );
}
Upvotes: 4
Views: 731
Reputation: 76
It's possible using a JPanel. You simply create a JPanel,add your own icon or existing icon to a JLabel. Then add your text to another JLabel and add these JLabels to the JPanel. Using BorderLayout, you can control the position of the text JLabel and icon JLabel.
Example (Ran and tested it, works fine):
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);
}
Upvotes: 1