Displaying message in java

How to display a message in a new frame using Java

Following example demonstrates how to display message in a new frame by creating a frame using JFrame() & using JFrames getContentPanel(), setSize() & setVisible() methods to display this frame.

import java.awt.*; import java.awt.font.FontRenderContext; import java.awt.geom.Rectangle2D; import javax.swing.JFrame; import javax.swing.JPanel; public class Main extends JPanel < public void paint(Graphics g) < Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setFont(new Font("Serif", Font.PLAIN, 48)); paintHorizontallyCenteredText(g2, "Java Source", 200, 75); paintHorizontallyCenteredText(g2, "and", 200, 125); paintHorizontallyCenteredText(g2, "Support", 200, 175); >protected void paintHorizontallyCenteredText( Graphics2D g2, String s, float centerX, float baselineY) < FontRenderContext frc = g2.getFontRenderContext(); Rectangle2D bounds = g2.getFont().getStringBounds(s, frc); float width = (float) bounds.getWidth(); g2.drawString(s, centerX - width / 2, baselineY); >public static void main(String[] args) < JFrame f = new JFrame(); f.getContentPane().add(new Main()); f.setSize(450, 350); f.setVisible(true); >>

Result

The above code sample will produce the following result.

JAVA and J2EE displayed in a new Frame.

The following is an example to display a message in a new frame.

import java.awt.BorderLayout; import java.awt.Button; import java.awt.Component; import java.awt.Frame; import java.awt.TextArea; public class Main < public static void main(String[] args) < Frame f = new Frame("Tutorialspoint"); Component text = new TextArea("Sairamkrishna Mamamahe"); Component button = new Button("Button"); f.add(text, BorderLayout.NORTH); f.add(button, BorderLayout.SOUTH); int width = 300; int height = 300; f.setSize(width, height); f.setVisible(true); >>

Источник

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

Источник

How to present a simple alert message in java?

In Java, it is sometimes necessary to display an alert message to the user. This can be used to inform the user of important information or to prompt the user for confirmation before performing an action. There are multiple ways to present a simple alert message in Java, each with its own advantages and disadvantages. In this article, we will discuss the different methods for displaying an alert message in Java.

Method 1: JOptionPane

To present a simple alert message in Java using JOptionPane , follow these steps:

  1. Import the javax.swing.JOptionPane package.
  2. Use the showMessageDialog method to display the alert message.
import javax.swing.JOptionPane; public class AlertMessageExample  public static void main(String[] args)  JOptionPane.showMessageDialog(null, "This is a simple alert message."); > >

In the above example, the showMessageDialog method takes two arguments: the parent component (in this case, null is used to indicate that there is no parent component) and the message to be displayed («This is a simple alert message.»).

You can also customize the dialog box by using additional arguments. For example, you can specify the message type (information, warning, error, question) and the icon to be displayed. Here is an example code:

import javax.swing.JOptionPane; import javax.swing.ImageIcon; public class CustomAlertMessageExample  public static void main(String[] args)  ImageIcon icon = new ImageIcon("path/to/icon.png"); JOptionPane.showMessageDialog(null, "This is a custom alert message.", "Alert", JOptionPane.WARNING_MESSAGE, icon); > >

In the above example, the showMessageDialog method takes five arguments: the parent component, the message to be displayed, the title of the dialog box («Alert»), the message type (warning), and the icon to be displayed.

You can explore more options and variations of JOptionPane by referring to the official Java documentation.

Method 2: Dialog Boxes

To present a simple alert message in Java using Dialog Boxes, follow these steps:

import javax.swing.JOptionPane;
JOptionPane.showMessageDialog(null, "Your message here");
JOptionPane.showMessageDialog(null, "Your message here", "Title here", JOptionPane.INFORMATION_MESSAGE);

Here are some more examples:

JOptionPane.showMessageDialog(null, "Your warning message here", "Warning", JOptionPane.WARNING_MESSAGE);
JOptionPane.showMessageDialog(null, "Your error message here", "Error", JOptionPane.ERROR_MESSAGE);
int answer = JOptionPane.showConfirmDialog(null, "Are you sure?", "Confirmation", JOptionPane.YES_NO_OPTION); if (answer == JOptionPane.YES_OPTION)  // do something > else  // do something else >
String input = JOptionPane.showInputDialog(null, "Enter your name", "Input", JOptionPane.QUESTION_MESSAGE); System.out.println("Your name is " + input);

That’s it! These are some examples of how to present a simple alert message in Java using Dialog Boxes.

Method 3: JavaFX Alert

To present a simple alert message in Java using JavaFX Alert, you can follow these steps:

Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Information Dialog"); alert.setHeaderText("Look, an Information Dialog");
alert.setContentText("I have a great message for you!");

Here is the complete code example:

import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; public class AlertExample  public static void main(String[] args)  Alert alert = new Alert(AlertType.INFORMATION); alert.setTitle("Information Dialog"); alert.setHeaderText("Look, an Information Dialog"); alert.setContentText("I have a great message for you!"); alert.showAndWait(); > >

This code will display an information alert dialog with the specified title, header text, and content text. You can change the alert type by specifying a different AlertType , such as AlertType.WARNING or AlertType.ERROR . You can also customize the alert buttons and add icons to the alert dialog.

Источник

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

Источник

Оцените статью