Display a message on the screen
I would like to display a message onto the screen upon the button press. The message should not have any window and should be displayed at the center of the screen over any content that would be there. Kind of like here. How would I go about achieving such an effect? I have tried searching everywhere, but the nature of the question would not yield results. Thanks to anyone for their help in pointing me to right direction. UPDATE: For those wondering. This is the project I am working on, thanks MadProgrammer for your help. You saved my eyes.
Create a window using JavaFX SceneBuilder or some other editor and set the initStyle of the stage to StageStyle.UNDECORATED before you show it. Picture an error dialog or similar in any windows program that’s missing the border and the title bar/buttons. That’s what you’ll see if you go this route.
Probably because you haven’t shown that You’ve made any effort to find a solution yourself (this is speculation, since I didn’t downvote; but there’s nothing in your question to indicate this).
3 Answers 3
I can think of at least three ways to do this in Swing.
GlassPane
If you simply want to show content over the current frame, then you can use the current frames glass pane.
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Font; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class OverlayTest < public static void main(String[] args) < new OverlayTest(); >public OverlayTest() < EventQueue.invokeLater(new Runnable() < @Override public void run() < try < UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); >catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) < ex.printStackTrace(); >final OverlayPane overlay = new OverlayPane(); JButton show = new JButton("Show"); show.addActionListener(new ActionListener() < @Override public void actionPerformed(ActionEvent e) < overlay.setVisible(true); >>); JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setGlassPane(overlay); frame.setLayout(new GridBagLayout()); frame.add(show); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); > >); > public class OverlayPane extends JPanel < private JLabel label; public OverlayPane() < setLayout(new GridBagLayout()); label = new JLabel("1"); label.setFont(label.getFont().deriveFont(Font.BOLD, 96f)); add(label); setOpaque(false); >@Override public Dimension getPreferredSize() < return new Dimension(200, 200); >> >
JLayedPane
If you want to display the content over a particular component within the current frame, then you could take advantage of the JLayeredPane , which acts a lot like a glass pane for components.
Undecorated Frame
import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Font; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class OverlayTest2 < public static void main(String[] args) < new OverlayTest2(); >public OverlayTest2() < EventQueue.invokeLater(new Runnable() < @Override public void run() < try < UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); >catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) < ex.printStackTrace(); >final JFrame masterFrame = new JFrame("Testing"); final OverlayPane overlay = new OverlayPane(); JButton show = new JButton("Show"); show.addActionListener(new ActionListener() < @Override public void actionPerformed(ActionEvent e) < JFrame frame = new JFrame(); frame.setUndecorated(true); frame.setBackground(new Color(0, 0, 0, 0)); frame.add(new OverlayPane()); frame.pack(); frame.setLocationRelativeTo(masterFrame); frame.setVisible(true); >>); masterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); masterFrame.setGlassPane(overlay); masterFrame.setLayout(new GridBagLayout()); masterFrame.add(show); masterFrame.pack(); masterFrame.setLocationRelativeTo(null); masterFrame.setVisible(true); > >); > public class OverlayPane extends JPanel < private JLabel label; public OverlayPane() < setLayout(new GridBagLayout()); label = new JLabel("1"); label.setFont(label.getFont().deriveFont(Font.BOLD, 96f)); add(label); setOpaque(false); >@Override public Dimension getPreferredSize() < return new Dimension(200, 200); >> >
Now, if you want the window be filled, then remove the line frame.setBackground(new Color(0, 0, 0, 0)); , this is what makes the frame transparent.
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
- Display Image in Java
- Show Coordinates
- What is Java Swing?
- Creating a Frame
- Setting an Icon for a Frame in Java
- Show Dialog Box in Java
- Show message and confirm dialog box
- Show input dialog box
- Changing the Label of a JButton Component in Java
- Setting Multi-Line label on the Button
- Setting icon on the button in Java
- Making a Frame Non Resizable in Java
- Remove Minimize and Maximize Button of Frame in Java
- Removing the Title Bar of a Frame
- Setting Bounds for a maximized frame
- Iconifying and Maximizing a frame in Java
- Making a component drag gable in java
- Create a ToolBar in Java
- Animating Images in Java Application
- Drawing with Color in Java
- Drawing with Gradient Color in Java
- Adding a Rollover and Pressed Icon to a JButton Component in Java
- Creating Check Box in Java Swing
- Customize the Icon in a JCheckBox Component of Java Swing
- Create a JComboBox Component in Java
- Combo Box operation in Java Swing
- Create a JRadioButton Component in Java
- Selecting a Radio Button component in Java
- Create a JList Component in Java
- Setting Tool Tip Text for items in a JList Component
- Setting the Dimensions of an Item in a JList Component in Java
- Create a JSpinner Component in Java
- Show Time in JSpinner Component
- Disabling Keyboard Editing in a JSpinner Component
- Limiting the Values in a Number JSpinner Component
- JSlider Component of Java Swing
- Progress Bar in Java Swing
- Create menus and submenus in Java
- Crate a Popup Menu in Java
- Create a Popup Menus with Nested Menus in Java
How do I get a MessageBox like information window to appear in Java?
I’m learning Java and I have no idea how to do this. I dragged a button on the form in Netbeans, double clicked it and it created this event:
@Action public void HelloClickMethod()
@Action public void HelloClickMethod()
However the IDE is saying I have an error in the word ‘this’. «Cannot find symbol». I don’t understand. Why is it so dificult and why are the errors so esoteric. 😛
Compiler messages always look esoteric if you are new to a language. The problem is that no compiler can know what you thought you were trying to write.
3 Answers 3
I can think of the following cause: you might not be «importing» the package containing JOptionPane. Try:
On top of your source file. Or, use
javax.swing.JOptionPane.showMessageDialog(this, "The message!", "This is supposed to be the MessageBox title.", JOptionPane.ERROR_MESSAGE);
Other cause is the location of the method, if you are in an static context, you can’t use this .
Looking back at the retardedness of my question is very humbling. Tip! Never look at code/questions you’ve written in the past, you’ll only blush. Hahahaha.
The showMessageDialog method does not take 3 parameters. Try this:
JOptionPane.showMessageDialog(this, "The message!", "This is supposed to be the MessageBox title.", JOptionPane.ERROR_MESSAGE);
There are 3 methods named showMessageDialog, one with 2 parameters (component and message), 4 parameters (component, message, title, message type) and 5 parameters (component, message, title, message type, icon).
I don’t have javac in this computer but I am almost certain that it can take 3 arguments.java.sun.com/docs/books/tutorial/uiswing/components/dialog.html
I am looking at the API docs java.sun.com/javase/7/docs/api/javax/swing/JOptionPane.html and there are only 3 methods? I see the tutorial that shows code with 3 parameters but. I have to check.
OK. I checked. I have javase6 installed and it does not allow the 3 parameter version given in the tutorial.
JavaFX: what is the best way to display a simple message?
In my application I need to display a warning/info message, but I don’t know a simple way to do that because there is no JOptionPane or similar component on JavaFX. There is a Popup class, but you have to set many parameters to get a decent layout/position/background color/etc for a simple message. So I want to know if there is a simple, already implemented component to display messages, maybe in a third party library if JavaFX does not offer anything decent. Thanks in advance for any help/hints.
4 Answers 4
ControlsFX is JavaFX 8 so I can’t use it, and the other alternatives are almost the same level of complexity of what I was going to do. So I implemented my own methods to display an info/warning message using a Popup.
Here is the source code of what I implemented. Just some tweaks to show the Popup centered on the window, use CSS and hide when the user clicks on it.
public static Popup createPopup(final String message) < final Popup popup = new Popup(); popup.setAutoFix(true); popup.setAutoHide(true); popup.setHideOnEscape(true); Label label = new Label(message); label.setOnMouseReleased(new EventHandler() < @Override public void handle(MouseEvent e) < popup.hide(); >>); label.getStylesheets().add("/css/styles.css"); label.getStyleClass().add("popup"); popup.getContent().add(label); return popup; > public static void showPopupMessage(final String message, final Stage stage) < final Popup popup = createPopup(message); popup.setOnShown(new EventHandler() < @Override public void handle(WindowEvent e) < popup.setX(stage.getX() + stage.getWidth()/2 - popup.getWidth()/2); popup.setY(stage.getY() + stage.getHeight()/2 - popup.getHeight()/2); >>); popup.show(stage); >