JOptionPane is pop up window to show any information, confirm any action or user input in Java. We can use JOptionPane class simply to show dialogue boxes for users. But it has default size, text and icons and some time we feel that, can we customise the properties of this dialogue box, like its window size, its text size, colour and type and many more?
So, obviously answer is yes, we can customise the properties of JOptionPane. Let see how?
To create dialogue box using JOptionPane, we simply use -
JOptionPane.showConfirmDialog(null,"Text Message", "Window Title",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
So, obviously answer is yes, we can customise the properties of JOptionPane. Let see how?
To create dialogue box using JOptionPane, we simply use -
JOptionPane.showConfirmDialog(null,"Text Message", "Window Title",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
Output looks like -
Now If you want to customise the message text, we require to use label(JLabel), because we can customize the JLabel text and can apply css and HTML on it. See this post.
JLabel label = new JLabel("<html><p><h1>Text Message</h1></p><html>");
JOptionPane.showConfirmDialog(null,"Text Message", "Window Title",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
Output looks like -
above we have changed the text colour and size.
You can also customise JLabel text by using code like -
label.setFont(new Font("Arial", Font.BOLD, 18));
To change default image you can use code like -
ImageIcon icon = new ImageIcon(Pane.class.getResource("ss.jpg"));
JOptionPane.showConfirmDialog(null,"Text Message", "Window Title",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, icon);
Output will look like -
To change the window size, you can use code like -
UIManager.put("OptionPane.minimumSize",new Dimension(500,500));
JLabel label = new JLabel("<html><p><h1 style=color:green>Text Message</h1></p><html>");
ImageIcon icon = new ImageIcon(Pane.class.getResource("ss.jpg"));
JOptionPane.showConfirmDialog(null,label, "Window Title",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,icon);
Output looks like -
You can also do many more experience with JOptionPane customisation.
No comments:
Post a Comment