- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Customize the JOptionPane layout with updated color and image in Java
Customize the layout by changing the look and feel of the panel in which you added the component −
ImageIcon icon = new ImageIcon(new URL("http −//www.tutorialspoint.com/images/C-PLUS.png")); JLabel label = new JLabel(icon); JPanel panel = new JPanel(new GridBagLayout()); panel.add(label); panel.setOpaque(true); panel.setBackground(Color.ORANGE);
Above, we have added an image and even updated the background color of the panel.
Now, set it for the text panel −
JPanel textPanel = new JPanel(new GridLayout(10, 5)); textPanel.setBackground(Color.Magenta);
The following is an example to customize the JOptionPane layout −
Example
package my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.net.URL; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; public class SwingDemo { public static void main(String[] args) throws Exception { ImageIcon icon = new ImageIcon(new URL("http −//www.tutorialspoint.com/images/C-PLUS.png")); JLabel label = new JLabel(icon); JPanel panel = new JPanel(new GridBagLayout()); panel.add(label); panel.setOpaque(true); panel.setBackground(Color.ORANGE); JPanel textPanel = new JPanel(new GridLayout(10, 5)); textPanel.setBackground(Color.MAGENTA); for (int i = 0; i < 20; i++) { textPanel.add(new JLabel("Learn C++")); } JPanel panel2 = new JPanel(new BorderLayout()); panel2.add(textPanel); panel2.add(panel, BorderLayout.EAST); JOptionPane.showMessageDialog(null, panel2, "Course",JOptionPane.DEFAULT_OPTION); } }
Output
- Related Articles
- Customize the tooltip font, color , background and foreground color in Java
- Java Program to customize MenuBar and change the background color
- Add multiple number input fields with JOptionPane and display the sum in Console with Java
- How to use JOptionPane with Array Elements in Java?
- Can we set JOptionPane with predefined selection in Java?
- How to customize the color and colormaps of a Plot in Matplotlib
- How to how to customize snackBar's layout in Android?
- How to customize a button to set text and color in Android?
- Java Program to set JComboBox in JOptionPane
- What are the different types of JOptionPane dialogs in Java?\n
- How to work with border layout position options in Java?
- How to make JOptionPane to handle Yes, No and Closed buttons in Java?
- Java Program to set an icon for JOptionPane
- How to drop fill color to change the image color using HTML and CSS?
- Mega ios updated with new photosync and passcode lock features

Advertisements