Create a program that can display a GUI like the picture bel
Solution
A dialog window is an impartial subwindow meant to carry brief note other than the main Swing application Window. most Dialogs present an blunders message or warning to a consumer, however Dialogs can gift images, directory bushes, or just about some thing well suited with the primary Swing utility that manages them.
For convenience, several Swing factor classes can immediately instantiate and show dialogs. To create simple, widespread dialogs, you operate the JOptionPane class. The ProgressMonitor magnificence can positioned up a dialog that shows the progress of an operation. two other classes, JColorChooser and JFileChooser, additionally supply widespread dialogs. To bring up a print dialog, you could use the Printing API. To create a custom dialog, use the JDialog magnificence without delay.
The code for simple dialogs may be minimum. for instance, here is an informational conversation:
An informational conversation requires minimum code
here is the code that creates and indicates it:
JOptionPane.showMessageDialog(body, \"Eggs aren\'t supposed to be inexperienced.\");
The rest of this section covers the subsequent topics:
a top level view of Dialogs
The DialogDemo example
JOptionPane functions
creating and displaying easy Dialogs
Customizing Button textual content
Getting the person\'s input from a conversation
preventing automatic dialog last
The dialog API
Examples that Use Dialogs
an outline of Dialogs
every dialog is depending on a frame factor. when that frame is destroyed, so are its based Dialogs. whilst the body is iconified, its established Dialogs also disappear from the display. whilst the frame is deiconified, its dependent Dialogs return to the screen. A swing JDialog class inherits this behavior from the AWT conversation magnificence.
A conversation may be modal. while a modal conversation is seen, it blocks person enter to all other windows in the software. JOptionPane creates JDialogs which can be modal. To create a non-modal dialog, you need to use the JDialog class without delay.
starting with JDK 7, you could modify dialog window modality conduct the use of the new Modality API. See the brand new Modality API for info.
The JDialog elegance is a subclass of the AWT java.awt.conversation elegance. It provides a root pane field and support for a default close operation to the conversation object . these are the equal capabilities that JFrame has, and using JDialog without delay may be very much like using JFrame. in case you\'re going to use JDialog directly, then you definately must apprehend the cloth in the use of pinnacle-stage bins and how to Make Frames, in particular Responding to Window-remaining events.
even if you use JOptionPane to put into effect a dialog, you are nevertheless using a JDialog backstage. The cause is that JOptionPane is in reality a box which can routinely create a JDialog and upload itself to the JDialog\'s content material pane.
The DialogDemo instance
here is a picture of an software that displays dialogs.
DialogDemo helps you to convey up many varieties of dialogs
do this::
click the launch button to run the dialog Demo using Java™ internet begin (down load JDK 7 or later). rather, to compile and run the example your self, consult the instance index.Launches the DialogDemo example
click on the display it! button.
A modal dialog will appear. until you shut it, the utility can be unresponsive, although it will repaint itself if necessary. you may close the dialog both via clicking a button within the conversation or explicitly, together with by using the use of the dialog window decorations.
in the greater Dialogs pane, click on the bottom radio button and then the show it! button.
A non-modal conversation will seem. be aware that the DialogDemo window stays fully functional even as the non-modal dialog is up.
at the same time as the non-modal conversation is showing, iconify the DialogDemo window.
The conversation will disappear from the display until you deiconify the DialogDemo window.
JOptionPane capabilities
using JOptionPane, you could speedy create and customize numerous unique forms of dialogs. JOptionPane gives guide for laying out substantial dialogs, supplying icons, specifying the conversation pick out and text, and customizing the button text. unique functions permit you to customise the components the dialog presentations and specify wherein the communique want to seem onscreen. you can even specify that an choice pane placed itself into an internal frame (JInternalFrame) in area of a JDialog.
while you create a JOptionPane, look-and-sense-precise code gives additives to the JOptionPane and determines the layout of those components.
JOptionPane\'s icon assist lets you effortlessly specify which icon the conversation displays. you could use a custom icon, no icon in any respect, or any character of 4 famous JOptionPane icons (question, facts, caution, and mistakes). every appearance and revel in has its very own versions of the 4 fashionable icons. the following figure indicates the icons used inside the Java (and home windows) look and feel.
Icons utilized by JOptionPane
Icon description Java look and experience domestic windows look and sense
question The Java look and enjoy icon for dialogs that ask questions the house windows look and sense icon for dialogs that ask questions
data The Java appearance and experience icon for informational dialogs The home windows appearance and feel icon for informational dialogs
warning The Java appearance and feel icon for caution dialogs the house windows appearance and enjoy icon for warning dialogs
mistakes The Java look and experience icon for error dialogs The home windows appearance and feel icon for errors dialogs
growing and showing clean Dialogs
for max easy modal dialogs, you create and show the communication using one in all JOptionPane\'s showXxxDialog strategies. in case your dialog must be an internal frame, then upload internal after display — as an instance, showMessageDialog changes to showInternalMessageDialog. in case you want to manipulate the dialog window-last conduct or in case you do no longer need the dialog to be modal, then you definitely definately need to right away instantiate JOptionPane and add it to a JDialog instance. Then invoke setVisible(actual) on the JDialog to make it seem.
the 2 most useful showXxxDialog techniques are showMessageDialog and showOptionDialog. The showMessageDialog technique presentations a clean, one-button communique. The showOptionDialog method shows a customized dialog — it could display a selection of buttons with custom designed button text, and may include a widespread text message or a group of additives.
the other showXxxDialog techniques are used less often. The showConfirmDialog method asks the person to confirm some issue, however gives widespread button text (yes/No or the localized identical, for example) in desire to button textual content custom designed to the purchaser situation (begin/Cancel, for example). A fourth method, showInputDialog, is designed to display a modal verbal exchange that receives a string from the character, using both a text discipline, an uneditable blend field or a list.
right here are a few examples, taken from DialogDemo.java, of the usage of showMessageDialog, showOptionDialog, and the JOptionPane constructor. For greater example code, see DialogDemo.java and the opposite applications indexed in Examples that Use Dialogs.
showMessageDialog
shows a modal communication with one button, that\'s categorized \"right enough\" (or the localized equal). you could effortlessly specify the message, icon, and identify that the dialog suggests. right here are some examples of the usage of showMessageDialog:
Informational conversation with default name and icon
//default title and icon
JOptionPane.showMessageDialog(frame,
\"Eggs aren\'t imagined to be green.\");
Informational verbal exchange with custom identify, warning icon
//custom title, caution icon
JOptionPane.showMessageDialog(body,
\"Eggs aren\'t alleged to be inexperienced.\",
\"Inane caution\",
JOptionPane.WARNING_MESSAGE);
Informational communique with custom identify, errors icon
//custom identify, mistakes icon
JOptionPane.showMessageDialog(body,
\"Eggs aren\'t alleged to be green.\",
\"Inane blunders\",
JOptionPane.ERROR_MESSAGE);
Informational verbal exchange with custom call, no icon
//custom name, no icon
JOptionPane.showMessageDialog(body,
\"Eggs are not presupposed to be inexperienced.\",
\"a undeniable message\",
JOptionPane.PLAIN_MESSAGE);
Informational communique with custom name, custom icon
//custom identify, custom icon
JOptionPane.showMessageDialog(body,
\"Eggs are not supposed to be inexperienced.\",
\"Inane custom conversation\",
JOptionPane.INFORMATION_MESSAGE,
icon);
showOptionDialog
displays a modal conversation with the specified buttons, icons, message, name, and so forth. With this method, you can change the text that looks at the buttons of general dialogs. you may moreover perform many different varieties of customization.
sure/No/Cancel (in one-of-a-type phrases); showOptionDialog
//custom button text
item[] alternatives = \"yes, please\",
\"No, thanks\",
\"No eggs, no ham!\";
int n = JOptionPane.showOptionDialog(body,
\"could you want a few green eggs to move \"
+ \"with that ham?\",
\"A stupid question\",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
alternatives,
options[2]);


