swing and joptionpane
TRANSCRIPT
SwingandJOptionPane
Producedby:
DepartmentofComputingandMathematicshttp://www.wit.ie/
UsingGraphicalUserInterface(GUI)Components
Dr.Siobhán DrohanMr.ColmDunphyMr.DiarmuidO’Connor
GraphicalUser
Interface(GUI)
GraphicalUser
Interface(GUI)
GraphicalUser
Interface(GUI)
Topicslist
Swing• JOptionPane– JOptionPane methods• showMessageDialog()• showInputDialog()• showConfirmDialog()
WhatisSwing?
• Swing isasetofprogramcomponentsfor Java programmersthatallowyoutocreategraphicaluserinterface(GUI )components
WhatisSwing?
• Swing isasetofprogramcomponentsfor Java programmersthatallowyoutocreategraphicaluserinterface(GUI )components
Thismodule:
dialog/messageboxesonly
UsingSwing– import thelibrary
• WemakeSwingcomponentsavailabletousbyimportingtheSwingcomponentsatthestartoftheprogram.
import javax.swing.*;
However,* importsall theSwingcomponents(andtherearealot!)
UsingSwing– import specifics
• WemakeSwingcomponentsavailabletousbyimportingtheSwingcomponentsatthestartoftheprogram.
import javax.swing.JOptionPane;
AsweonlyplanonusingJOptionPane,wecanjustimportthatspecificSwingcomponentinsteadoftheentirelibrary.
UsingJOptionPane withSwing
• HavingimportedJOptionPane fromSwing,wecanusethesemethods:
showMessageDialog()• Simplemessageoutput
showInputDialog()• Allowsusertotypein(string)input
showConfirmDialog()• Allowtheusertochooseanoption
JOptionPane.showMessageDialog (null, "Welcome to the 12 days of Christmas”
);
showMessageDialog - SimpleMessageoutput
TextinDialogbox
ParentComponent– forourpurposes,null willworkasfirstparameter.
https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
JOptionPane.showMessageDialog (null, "Welcome to the first day of Christmas”,“Christmas Title”,JOptionPane.PLAIN_MESSAGE);
showMessageDialog - Messageoutputwithlabel
Thismeans‘noicon’https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
Textfortitle ofbox
String day = JOptionPane.showInputDialog("Welcome to Christmas week\n\n " + "Please enter the day: ","Monday");
showInputDialog - Messageinput
Prompttext
https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
Defaultvalue
ReadinginnumbersviaJOptionPane
• Problem:– showInputDialog() returnsaString– Soifyoutype22,itisthestring“22”,thiscan’tbeusedasnumber
• Solution– Useapredefinedmethodtoconverttoanumber.
int number = Integer.parseInt ("22");println (number + 3);
printsthenumber25.
UsingparseInt withinput
ThisconvertstheinputStringtoanIntegerandstoresitinnum.https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
int num = Integer.parseInt ( JOptionPane.showInputDialog(
"Enter a number between 0 and 10", "3" ));
showConfirmDialog - UsingtheYes/No option
int reply = JOptionPane.showConfirmDialog(null, “Did you watch the Late Late Toy Show on Saturday night?","Christmas Question", JOptionPane.YES_NO_OPTION );
https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
JOptionPane.YES_OPTION isreturnedifyoupress‘Yes’.JOptionPane.NO_OPTION isreturnedotherwise.(replywillbeassignedthisvalue)
Summary
Swing• JOptionPane
– import javax.swing.JOptionPane;
– JOptionPane methods• showMessageDialog()• showInputDialog()
– parseInt()
• showConfirmDialog()– JOptionPane.YES_NO_OPTION– JOptionPane.YES_OPTION– JOptionPane.NO_OPTION
Questions?