Messagebox in java swing

Dystopian Code

To create a dialog in Swing, you will need to use the JOptionPane class.

If you want to trigger a message dialog, you will need to use the static method:

showMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon)

Defines the component (usually a JFrame or a JPanel) that will be the parent of the dialog. The coordinates of the parent will be used for the placement of the dialog box (usually the dialog box will to the middle of the component). If this parameter is null the dialog will pop-up in the center of the screen.

package javaexample; import javax.swing.JFrame; import javax.swing.JOptionPane; public class DialogExample < public static void main(String[] args) < JFrame frame = new JFrame("Sample frame"); frame.setSize(400, 400); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JOptionPane.showMessageDialog(frame, "Sample dialog box"); >>
package javaexample; import javax.swing.JFrame; import javax.swing.JOptionPane; public class DialogExample < public static void main(String[] args) < JFrame frame = new JFrame("Sample frame"); frame.setSize(400, 400); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JOptionPane.showMessageDialog(null, "Sample dialog box"); >>

The message is usually a string constant and represents the text contained in the body.

If message is an array, it will be interpreted as a series of messages (the interpretation is recursive — each object will be interpreted according to its type). If message is a component, the component will be displayed in the dialog.

package javaexample; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.UIManager; public class DialogExample < public static void main(String[] args) < Object[] objects = ; JOptionPane.showMessageDialog(null, objects, "This is the title", JOptionPane.PLAIN_MESSAGE); > >

The messageType defines the style of message. The available options are:
ERROR_MESSAGE
INFORMATION_MESSAGE
WARNING_MESSAGE
QUESTION_MESSAGE
PLAIN_MESSAGE

package javaexample; import javax.swing.JOptionPane; public class DialogExample < public static void main(String[] args) < new Thread() < @Override public void run() < JOptionPane.showMessageDialog(null, "Error message", "This is the title", JOptionPane.ERROR_MESSAGE); >>.start(); new Thread() < @Override public void run() < JOptionPane.showMessageDialog(null, "Information message", "This is the title", JOptionPane.INFORMATION_MESSAGE); >>.start(); new Thread() < @Override public void run() < JOptionPane.showMessageDialog(null, "Warning message", "This is the title", JOptionPane.WARNING_MESSAGE); >>.start(); new Thread() < @Override public void run() < JOptionPane.showMessageDialog(null, "Question message", "This is the title", JOptionPane.QUESTION_MESSAGE); >>.start(); new Thread() < @Override public void run() < JOptionPane.showMessageDialog(null, "Plain message", "This is the title", JOptionPane.PLAIN_MESSAGE); >>.start(); > >

You can place a decorative icon in the dialog box. Usually a default value is determined by the messageType parameter.

package javaexample; import javax.swing.JOptionPane; import javax.swing.UIManager; public class DialogExample < public static void main(String[] args) < JOptionPane.showMessageDialog(null, "This is the text body", "This is the title", JOptionPane.PLAIN_MESSAGE, UIManager.getIcon("Tree.collapsedIcon")); >>

Источник

Java Message Box

In this section we will use JOptionPane class to display the message Dialog box.

Java Message Box

Java Message Box

In this tutorials you will learn about how to create Message Box in Java. Java API provide a class to create a message Box in java. By message box we mean a box in which message is displayed or a box from which input provided by the user can be retrieved. A message box is meant to carry a temporary information or message apart from the main swing application. Most of the message box is used for error message. Using JOptionPane class you can create a message box. Here is the code that creates Message box and shows it:

JOptionPane.showMessageDialog(frame, "This is a Message Box.");

This statement will display a message with message as «This is a Message Box.».

There are several feature JOptionPane like customizing icon, customizing the component the dialog display and specify where message box should appear on screen.

JOptionPane icon let you decide which icon you want to display. You can use custom icon, or no icon, or any of the four JOptionPane standard icon. The two most useful method of showxxxdialog method are as follows :

  • showMessageDiaolog : The showMessageDialog method will display the one button dialog method.
  • showOptionDialog : This method display a variety of button with button text. and contain a standard text message.
  • showConfirmDialog : It will ask for confirmation of something with Yes/No.
  • showInputDialog : This will display a message box that accept input from user from text field or combo box.

showMessageDialog : Display a message with one button which is labeled «OK». You can specify the message, title, and icon according to your wish. Here are some example using showMessageDialog :

JOptionPane.showMessageDialog(frame," Message Box Demo.");
JOptionPane.showMessageDialog(frame,"Message Box Demo.","Warning",JOptionPane.WARNING_MESSAGE);
JOptionPane.showMessageDialog(frame,"Message Box Demo","Error Message Box",JOptionPane.ERROR_MESSAGE);

showOptionDialog : Displays a Message with the specified buttons, icons, message, title, and so on. By the help of this message, you can change the text that appears on the buttons of the message Box.

Object[] options = ; int n = JOptionPane.showOptionDialog(frame, "Would you like to save it",null, JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[2]);

JOptionPane () : Creates a JOptionPane with the specified buttons, icons, message, title, and so on.

Example : We create a program which will display the message box using a swing. First creating a swing application in which we created a button. When you click on the button then message box will pop up and display a message.

import java.awt.Component; import javax.swing.JOptionPane; import javax.swing.*; import java.awt.event.*; public class MessageBox < JFrame frame; public static void main(String[] args) < MessageBox db = new MessageBox(); >public MessageBox() < frame = new JFrame("Show Message Box"); JButton button = new JButton("Click"); button.setAlignmentX((float) 100.00); button.addActionListener(new MyAction()); frame.add(button); frame.setSize(300, 300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); >public class MyAction implements ActionListener < public void actionPerformed(ActionEvent e) < JOptionPane.showMessageDialog(frame, "Message Box"); >> >

Output from the program :

When you click on button then a message box will appear as below:

Tutorials

  1. Display Image in Java
  2. Show Coordinates
  3. What is Java Swing?
  4. Creating a Frame
  5. Setting an Icon for a Frame in Java
  6. Show Dialog Box in Java
  7. Show message and confirm dialog box
  8. Show input dialog box
  9. Changing the Label of a JButton Component in Java
  10. Setting Multi-Line label on the Button
  11. Setting icon on the button in Java
  12. Making a Frame Non Resizable in Java
  13. Remove Minimize and Maximize Button of Frame in Java
  14. Removing the Title Bar of a Frame
  15. Setting Bounds for a maximized frame
  16. Iconifying and Maximizing a frame in Java
  17. Making a component drag gable in java
  18. Create a ToolBar in Java
  19. Animating Images in Java Application
  20. Drawing with Color in Java
  21. Drawing with Gradient Color in Java
  22. Adding a Rollover and Pressed Icon to a JButton Component in Java
  23. Creating Check Box in Java Swing
  24. Customize the Icon in a JCheckBox Component of Java Swing
  25. Create a JComboBox Component in Java
  26. Combo Box operation in Java Swing
  27. Create a JRadioButton Component in Java
  28. Selecting a Radio Button component in Java
  29. Create a JList Component in Java
  30. Setting Tool Tip Text for items in a JList Component
  31. Setting the Dimensions of an Item in a JList Component in Java
  32. Create a JSpinner Component in Java
  33. Show Time in JSpinner Component
  34. Disabling Keyboard Editing in a JSpinner Component
  35. Limiting the Values in a Number JSpinner Component
  36. JSlider Component of Java Swing
  37. Progress Bar in Java Swing
  38. Create menus and submenus in Java
  39. Crate a Popup Menu in Java
  40. Create a Popup Menus with Nested Menus in Java

Источник

Message Box in Java

Message Box in Java

The Message Box in Java is the pop-up that appears on the screen for displaying some message and waits for confirmation from the user. The term JOptionPane is the Java-provided class that provides users the privilege to show message dialogue boxes. This class is inherited from the JComponent class and is present in the javax.swing package.

Below is the code block to show how the message box in Java works.

import javax.swing.*;  public class DialogueBoxPopUp   public static void main(String[] args)   JOptionPane.showMessageDialog(null,  "Hi, In the message box",  "PopUp Dialog",  JOptionPane.INFORMATION_MESSAGE);  > > 

In the above simple code block, the JOptionPane class prompts users with message boxes and waits for the response. The class has some static methods that serve as utilities for the user. The method showConfirmDialog asks a question and confirms over options as yes, no, and cancel. The showInputDialog method prompts the user for some input. The showMessageDialog function tells the user about some happenings.

The block above uses an overloaded version of the showMessageDialog method and takes four parameters. Firstly, the parentComponent argument checks for the frame in which the component can get displayed. If the value is a null value, then it uses the default frame. In the previous program, the null frame gets passed, so the code uses the default frame.

Next is the message argument that takes the message Object to be displayed. The title argument takes the title string for the pop-up box. The message in the above block takes the title as the PopUp Dialog that comes on the top of the dialog box.

The messageType is the type of message that executes ERROR_MESSAGE INFORMATION_MESSAGE WARNING_MESSAGE QUESTION_MESSAGE or PLAIN_MESSAGE values. These values are present as static and final values as the type of message in the JOptionPane class. The code uses INFORMATION_MESSAGE as the message type.

Check the previous program’s output here:

Pop up message dialog box

If the message type changes to JOptionPane.ERROR_MESSAGE , the error message dialog is as the image below.

The error dialog box popup

If the message type changes to JOptionPane.WARNING_MESSAGE , the warning message dialog looks like below.

The warning pop-up dialog box

There are some more message types that one can use when needed.

Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.

Related Article — Java GUI

Источник

Читайте также:  Box shadow inner css generator
Оцените статью