how to Work with JDialog and JOptionPane on NetBeans IDE
What Will I Learn?
How to use JDialog and JOptionPane(Java graphical user interface programming) in Java Desktop Application through NetBeans IDE.
You can suppose off a dialog as a straightforward pop-up window which will show a warning or maybe asks for a confirmation. eg- A dialog showing confirmation "Do you actually need to delete this information?"
You can additionally use a dialog to request some data from the user.
By reading the on top of that means of dialog you want to have already set that however would you utilize it in your project.
Requirements
Java swing provides four varieties of pre-build Dialogs-
JDialog
JOptionPane
JFileChooser
JColorChooser
Difficulty
- Basic
Tutorial Contents
Using JDialog
JDialog provides a window equipped shut icon. it's the foremost versatile dialogs as you'll be able to produce your own layout or interface. It's rather like another JFrameForm.
Steps-
1 - produce a replacement kind underneath your package and style it per your wants.
2 - within the palette window expand Swing Windows and drag the Dialog management onto the shape.
3 - Nothing appeared on your screen, you are doing right! currently expand 'Other Components' within the inspector window.(If the inspector isn't visible on your space then move to Window> Navigating> Inspector).
4 - Right click on the JDialog and select 'Change Variable Name'. Rename it to something, i'm renaming this as 'simpleDialog'.
5 - currently double click the simpleDialog. NetBeans can take you to the look read of the dialog. Here you'll drag & drop elements as you are doing for your type.
6 - Right click your OK button and choose action>actionPerformed below events.
7 - Here, write the subsequent code to shut the dialog.
simpleDialog.dispose();
// If you have got ever browse my tutorials, you will grasp what totally different colours within the codes mean
8 - Click the dialog and within the properties panel mention its minimumSize otherwise it'll get shrunken to 0x0 pixels.
9 - currently return to your kind by double clicking JFrame underneath the inspector window otherwise you may return by double click anyplace within the JDialog.
10 - Right click the 'Open JDialog' button and attend Events>Action>actionPerformed and write the subsequent code to show the Dialog.
simpleDialog.setVisible(true);
11 - Run your file(Shift + F6) to see the results!
Using JOptionPane-
The JOptionPane may be a swing window management that has in-built Dialog designs. it's a abstract category which means it doesn't need to be initialized before it's use.
Elements of a JOptionPane Dialog -
![6.jpg]
()
You can customise all of the four components in keeping with your desires. A JOptionPane also can hold a textfield if some data is needed from the user.
Steps-
1 - Right click 'Open JOptionPane' and choose action>actionPerformed beneath events.
2 - Write the subsequent code to show an easy message.
JOptionPane.showMessageDialog(this, "This is a simple dialog");
3 - Click on the yellow bulb on the left most line and choose 'Add import for javax.swing.JOptionPane'.
4 - Save and run your file(Shift + F6). Click on 'Open JOptionPane' to ascertain your results.
Unlike JDialog, you do not got to set the minimumSize for JOptionPane. Moreover, you do not got to write any technique to shut the dialog, it'll mechanically get shut once you click the OK button.
In the on top of example, we have a tendency to solely tried the message component of the JOptionPane. So, lets move onto a fancy example.
I have further 2 a lot of elements on my form(Label and TextField). currently we'll show the user a choice dialog, during which he would opt for his most popular alternative.
Write the subsequent for the 'Open JOptionPane' button -
String[] choices= {"NetBeans","Eclipse"};
int response=JOptionPane.showOptionDialog(this, "Which Java IDE do you use?", "Select One", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
if (response == 0){
jTextField1.setText("NetBeans");
}
else if (response == 1){
jTextField1.setText("Eclipse");
}
else
jTextField1.setText("No Choice");
Explanation - initial, we tend to produce a string array with variable name decisions within which we tend to keep our choices. Then we tend to displayed choice Dialog to the user by exploitation the tactic JOptionPane.showOptionDialog. In it's builder we tend to mentioned it's parent window as this(the current window) so typewritten our message between the double-qoutes. when this we tend to mentioned it's title. Then we tend to selected JOptionPane.DEFAULT_OPTION and JOptionPane.QUESTION_MESSAGE as it's optionType and messageType severally. we tend to set icon property to be null as a result of we do not wish show any icon on the toolbar of the window. Finally, we tend to place decisions declared earlier within the choices property. Choices[0] - zero here is that the initial string in our string array i.e NetBeans - simply makes NetBeans because the highlighted price. we tend to store the chosen choice in associate degree number variable response.
Then if the user elect the primary alternative i.e 0-NetBeans then NetBeans can displayed within the text-field and if the user selects second alternative i.e 1-Eclipse then Eclipse is displayed within the text field. However, if none is chosen then "No Choice" are displayed. this could be the case once user clicks on the cross icon.
Other kinds of JOptionPane dialogs -
InputDialog: This provides some way ( textfield, combobox or list) for the user to enter information. It continuously includes 2 buttons- OK and Cancel. It is created by exploitation the strategy showInputDialog().
Confirm Dialog: This asks a user to substantiate some data. It includes buttons like affirmative, No, OK and Cancel. It is created exploitation the strategy showConfirmDialog().
Values attainable for messageType property of JOptionPane Dialogs
Posted on Utopian.io - Rewarding Open Source Contributors
Hey @dany-alves, your contribution was rejected by the supervisor @espoem because he found out that it did not follow the Utopian rules.
Upvote this comment to help Utopian grow its power and help other Open Source contributions like this one. Do you want to chat? Join me on Discord.
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
ready boss thank you for agreeing my post and thanks also his boss advice
Your contribution is rejected because it does not follow the Utopian Rules, and is considered as plagiarism. Plagiarism is not allowed on Utopian, and posts that engage in plagiarism will be flagged and hidden forever.
You can contact us on Discord.
[utopian-moderator]