SlideShare a Scribd company logo
1 of 66
Download to read offline
Poelman & Associates, Inc. (c) 2003
1
Swing
A Quick Tutorial on Programming
Swing Applications
Poelman & Associates, Inc. (c) 2003
2
MVC – Model View Controller
• Swing is based on this design pattern
• It means separating the implementation of
an application into layers or components:
– The Model - the data structure that
represents something (like a customer info
rec)
– The Controller - the user interface logic for
manipulating it
– The View - the display of that data structure to
the user.
Poelman & Associates, Inc. (c) 2003
3
What is Swing?
• A set of classes (part of JFC) that support
platform independent GUI (Graphical User
Interface)
• Successor to the original Java GUI
classes (AWT) which didn’t work very well
(they had platform dependencies that
really made it a difficult API to use)
• AWT wasn’t very “sexy”
Poelman & Associates, Inc. (c) 2003
4
Swing
• Visible “widgets” - windows, buttons,
combo boxes, trees, tables, checkboxes,
text fields, menus, …
• Containers of components – applets,
dialogs, windows and frames
• Supporting classes and utility methods
Poelman & Associates, Inc. (c) 2003
5
Some important Swing visible
component classes
• JApplet **
• JButton
• JCheckBox
• JColorChooser
• JComboBox
• JDialog **
• JFileChooser
• JFormattedTextField
• JFrame **
• JLabel
• JList
• JMenu
• JMenuBar
• JMenuItem
• JPanel
• JPasswordField
• JPopupMenu
• JProgressBar
• JRadioButton
• JScrollBar
• JSlider
• JSpinner
• JTable
• JTextArea
• JTextField
• JToggleButton
• JToolBar
• JTree
• JWindow **
• ** means a top level containers
Poelman & Associates, Inc. (c) 2003
6
Using netbeans to create a
JDialog
Poelman & Associates, Inc. (c) 2003
7
adding fields
Poelman & Associates, Inc. (c) 2003
8
Poelman & Associates, Inc. (c) 2003
9
my empty
CustomerInfoDialog:JDialog
Poelman & Associates, Inc. (c) 2003
10
code created
Poelman & Associates, Inc. (c) 2003
11
To kill a zombie or running process in
netbeans right click and choose: ”terminate”
Poelman & Associates, Inc. (c) 2003
12
executing the class displays:
Poelman & Associates, Inc. (c) 2003
13
Editing a dialog
• 1St select a layout manager for the dialog
Poelman & Associates, Inc. (c) 2003
14
select component
edit properties
Poelman & Associates, Inc. (c) 2003
15
changing the layout manager
Poelman & Associates, Inc. (c) 2003
16
what layout manager should I
use?
• Start with the absolute and then
experiment when you feel comfortable (or
hire a graphic artist and let them worry
about it ;-).
Poelman & Associates, Inc. (c) 2003
17
Adding other components to the
view - JTextFields
Poelman & Associates, Inc. (c) 2003
18
execute the class
Poelman & Associates, Inc. (c) 2003
19
Adding a combo box
Poelman & Associates, Inc. (c) 2003
20
edit the model property for the
combo box
type in state
abbreviations
separated
by commas
Poelman & Associates, Inc. (c) 2003
21
Poelman & Associates, Inc. (c) 2003
22
preferred size property
hor , vert
Poelman & Associates, Inc. (c) 2003
23
MVC
Model – View – Controller Design
Pattern
Poelman & Associates, Inc. (c) 2003
24
Design Patterns
• A design pattern is a way of designing code that benefits from experience of
other developers – see GoF (Gang of Four) on Patterns
• Design patterns are “rules of thumb” & best practices
• A GUI is based on many design patterns
– 3D Pliancy
– Feedback
– Icons
– Menus
– Pointing
– Mnemonics & Accelerators
– Many more …
• A pattern usually has a name (and several aliases), a context, a problem it
addresses, a description of the solution, hints of when to use it and when
not to.
• See http://www.csc.calpoly.edu/~dbutler/tutorials/winter96/patterns/ ,
http://choices.cs.uiuc.edu/sane/dpatterns.html#dp and
http://www.stanford.edu/~borchers/hcipatterns
Poelman & Associates, Inc. (c) 2003
25
MVC – Model View Controller
pattern
• Swing components are designed as MVC
components
– Model = data or object that is the to be
visually represented
– View = one or more visual representations of
that data/object
– Controller = code to manage input to the
model
Poelman & Associates, Inc. (c) 2003
26
MVC
• © Sun 2002
Poelman & Associates, Inc. (c) 2003
27
MVC in Swing Components
• The Swing component class is the view
and controller
• A separate class is the model
• Most components come with a default
model
• You can set the model to your own model
for a control
• Several controls could share a model!
Poelman & Associates, Inc. (c) 2003
28
Poelman & Associates, Inc. (c) 2003
29
Creating icons using the blank icon
to start with
Poelman & Associates, Inc. (c) 2003
30
Change the properties of the button
to use you icon
Poelman & Associates, Inc. (c) 2003
31
Pushing the buttons changes the
displayed prices.
Poelman & Associates, Inc. (c) 2003
32
A different and better layout
Poelman & Associates, Inc. (c) 2003
33
JTree
JFrame
JFileChooser
JTable
JPasswordField
JSlider
JButton
JProgressBar
Poelman & Associates, Inc. (c) 2003
34
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTree jTree1;
private javax.swing.JSlider jSlider1;
private javax.swing.JProgressBar jProgressBar1;
private javax.swing.JTable jTable1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton1;
private javax.swing.JFileChooser jFileChooser1;
private javax.swing.JLabel jLabel1;
Poelman & Associates, Inc. (c) 2003
35
Swing based MenuLookDemo
Poelman & Associates, Inc. (c) 2003
36
TopLevelWindows.java
Poelman & Associates, Inc. (c) 2003
37
TopLevelWindows.java
package SwingSamples;
import javax.swing.*;
public class TopLevelWindows
{
public static void main(String args[])
{
JFrame myJFrame = new JFrame("The JFrame");
myJFrame.setSize(300,300);
myJFrame.setLocation(100,100);
JWindow myJWindow = new JWindow();
myJWindow.setSize(300,300);
myJWindow.setLocation(500, 100);
myJFrame.setVisible(true);
myJWindow.setVisible(true);
}
}
Poelman & Associates, Inc. (c) 2003
38
Top Level Containers
• Must have a top level container in Swing
• You must add components to the
associated content pane
Poelman & Associates, Inc. (c) 2003
39
ContentPaneExample.java
package SwingSamples;
import java.awt.*;
import javax.swing.*;
public class ContentPaneExample
{
public static void main(String args[])
{
JFrame myJFrame = new JFrame("JFrame");
myJFrame.setLocation(100,100);
Container myContentPane = myJFrame.getContentPane();
myContentPane.setLayout(new FlowLayout());
myContentPane.add(new JLabel("One"));
myContentPane.add(new JLabel("Two"));
myJFrame.pack(); //reformats the layout to the minimum size to fit
everything
myJFrame.setVisible(true);
}
}
Without the pack()
With the pack()
Poelman & Associates, Inc. (c) 2003
40
ContentPaneExample2.java
package SwingSamples;
import java.awt.*;
import javax.swing.*;
public class ContentPaneExample2
{
public static void main(String args[])
{
JFrame myJFrame = new JFrame("JFrame");
myJFrame.setLocation(100,100);
Container myContentPane = new JPanel();
myContentPane.add(new JLabel("One"));
myContentPane.add(new JLabel("Two"));
myJFrame.setContentPane(myContentPane);
myJFrame.pack();
myJFrame.setVisible(true);
}
}
Poelman & Associates, Inc. (c) 2003
41
Events
• Swing uses them to communicate
between swing components.
• An event is just a method call on the
receiving object by the sending object. The
method passes the event object.
addActionListener(ActionListener listener);
removeActionListener(ActionListener listener);
• An object registers to receive events. The
method that gets called is:
actionPerformed(ActionEvent e);
Poelman & Associates, Inc. (c) 2003
42
Events
• In Swing they are multicast – 1 to many
possible. Manes multiple method calls by
the send basically.
• Order isn’t defined, though.
• Events are immutable to the receiver.
• Events may be queued as in the keyboard
event queue.
• Multiple events maybe compressed into
one as in mouse movements.
Poelman & Associates, Inc. (c) 2003
43
Event Modifier Flags
• SHIFT_MASK
• CTRL_MASK
• META_MASK
• ALT_MASK
• BUTTON1_MASK
• BUTTON2_MASK
• BUTTON3_MASK
• Detect when certain keys are also pressed.
int modifierFlags = myEvent.getModifiers();
if ((modifierFlags & InputEvent.CRTL_MASK)!=0)
System.println.out(“Pressing the contrl key”);
Poelman & Associates, Inc. (c) 2003
44
Event Types
• ComponentEvent //resized,moved, shown, hidden
• FocusEvent //gained, lost
• KeyEvent //typed, pressed, released
• MouseEvent //clicked, pressed, released,
//entered, exited
• ContainerEvent //componentAdded componentRemoved
• ActionEvent //fired by: JButton, JChekBox, …
• AdjustmentEvent //fired by: JScrollBar
• Many more ….
Poelman & Associates, Inc. (c) 2003
45
Event Adapter Classes
• Map incoming events to a method to invoke on the
model to achieve the function.
• Separates the View & Controller from the Model (MVC)
• Prebuilt adapter has stubbed out methods for events.
You only implement the ones you are interested. You do
this by extending the adapter and overiding the methods
you need.
• Follows a general design pattern of called “adapter”.
• MouseAdapter, MouseInputAdapter,
MouseMotionAdapter, KeyAdapter, ComponentAdapter,
ContainerAdapter, DragSourceAdapter,
DropTargetAdapter, FocusAdapter, WindowAdapter, …
Poelman & Associates, Inc. (c) 2003
46
AWT Robot!
• Used to simulate keyboard and mouse
programmatically.
• It places events in the native system
queues for the platform you are on (not
just the java queue).
• Used for recording and replaying activities
in regression testing and other uses.
Poelman & Associates, Inc. (c) 2003
47
Multithreading and Swing
• Swing components always execute on a
single thread within your application. Not
the main thread of your application, either.
• Swing components are NOT multithread
safe!
• This is done for speed but influences how
you must design for them.
• We can ignore this for protoyping UIs but
not for design of applications.
Poelman & Associates, Inc. (c) 2003
48
Swing Components
Poelman & Associates, Inc. (c) 2003
49
Sample dialog with a few controls.
MySampleOfSwingControls1.java
Poelman & Associates, Inc. (c) 2003
50
JButton
java.lang.Object
|
+--java.awt.Component
|
+--java.awt.Container
|
+--javax.swing.JComponent
|
+--javax.swing.AbstractButton
|
+--javax.swing.JButton
Poelman & Associates, Inc. (c) 2003
51
JButton
• Used for a command
• Push and shows a state change visually (pliancy)
• Has a name, label text,
Poelman & Associates, Inc. (c) 2003
52
Adding items to the List
Poelman & Associates, Inc. (c) 2003
53
Add items to the model for the list
Poelman & Associates, Inc. (c) 2003
54
Changing the border of a list box
Poelman & Associates, Inc. (c) 2003
55
A Titled Border for a List Box
Poelman & Associates, Inc. (c) 2003
56
List Box Selection Modes
• Single
• Multiple_Interval
• Single_Interval
Poelman & Associates, Inc. (c) 2003
57
Setting the Button group on a radio
button
Poelman & Associates, Inc. (c) 2003
58
Setting the Mnemonics
Poelman & Associates, Inc. (c) 2003
59
Final Dialog Version
Poelman & Associates, Inc. (c) 2003
60
JComboBox
• Two styles in the app –
non-editable and editable
• If you use the editable
type you should check the
input of the user to make
sure it is acceptable.
• You can change the style
by changing the
editable property.
Poelman & Associates, Inc. (c) 2003
61
Creating a
JavaGUI -> SampleForms -> Application
Poelman & Associates, Inc. (c) 2003
62
Menu and Menu item hierarchy that
defines the menus for the app
Poelman & Associates, Inc. (c) 2003
63
The default menus
Poelman & Associates, Inc. (c) 2003
64
Poelman & Associates, Inc. (c) 2003
65
Copy and paste a menu into the
hierarchy
Poelman & Associates, Inc. (c) 2003
66
This shows 2 Edit menus

More Related Content

What's hot (20)

Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 
java2 swing
java2 swingjava2 swing
java2 swing
 
Java- GUI- Mazenet solution
Java- GUI- Mazenet solutionJava- GUI- Mazenet solution
Java- GUI- Mazenet solution
 
Basic using of Swing in Java
Basic using of Swing in JavaBasic using of Swing in Java
Basic using of Swing in Java
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
The AWT and Swing
The AWT and SwingThe AWT and Swing
The AWT and Swing
 
JAVA GUI PART I
JAVA GUI PART IJAVA GUI PART I
JAVA GUI PART I
 
java swing
java swingjava swing
java swing
 
Swings in java
Swings in javaSwings in java
Swings in java
 
Gui
GuiGui
Gui
 
Java GUI PART II
Java GUI PART IIJava GUI PART II
Java GUI PART II
 
Java Swing
Java SwingJava Swing
Java Swing
 
Awt and swing in java
Awt and swing in javaAwt and swing in java
Awt and swing in java
 
Swings
SwingsSwings
Swings
 
Chap1 1 1
Chap1 1 1Chap1 1 1
Chap1 1 1
 
Chap1 1.1
Chap1 1.1Chap1 1.1
Chap1 1.1
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892
 
GUI programming
GUI programmingGUI programming
GUI programming
 
Java swings
Java swingsJava swings
Java swings
 
Java Swing
Java SwingJava Swing
Java Swing
 

Similar to Sample Swing Dialog with Controls

Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docxBroncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docxcurwenmichaela
 
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docxBroncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docxhartrobert670
 
Inversion Of Control
Inversion Of ControlInversion Of Control
Inversion Of Controlbhochhi
 
Getting start Java EE Action-Based MVC with Thymeleaf
Getting start Java EE Action-Based MVC with ThymeleafGetting start Java EE Action-Based MVC with Thymeleaf
Getting start Java EE Action-Based MVC with ThymeleafMasatoshi Tada
 
IBM Cognos 10 Framework Manager Metadata Modeling: Tips and Tricks
IBM Cognos 10 Framework Manager Metadata Modeling: Tips and TricksIBM Cognos 10 Framework Manager Metadata Modeling: Tips and Tricks
IBM Cognos 10 Framework Manager Metadata Modeling: Tips and TricksSenturus
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event HandlingWebStackAcademy
 
JavaScript Framework Smackdown
JavaScript Framework SmackdownJavaScript Framework Smackdown
JavaScript Framework Smackdownmeghantaylor
 
01 startoff angularjs
01 startoff angularjs01 startoff angularjs
01 startoff angularjsErhwen Kuo
 
Building strong foundations apex enterprise patterns
Building strong foundations apex enterprise patternsBuilding strong foundations apex enterprise patterns
Building strong foundations apex enterprise patternsandyinthecloud
 
Lightning salesforce
Lightning salesforceLightning salesforce
Lightning salesforcemounikadv
 
object oriented testing with types of testing
object oriented testing with types of testingobject oriented testing with types of testing
object oriented testing with types of testingMani Deepak Choudhry
 
Ctools presentation
Ctools presentationCtools presentation
Ctools presentationDigitaria
 
Web sphere application server performance tuning workshop
Web sphere application server performance tuning workshopWeb sphere application server performance tuning workshop
Web sphere application server performance tuning workshopRohit Kelapure
 
Better User Experience with .NET
Better User Experience with .NETBetter User Experience with .NET
Better User Experience with .NETPeter Gfader
 
Sas® Macro Design Patterns
Sas® Macro Design PatternsSas® Macro Design Patterns
Sas® Macro Design PatternsMark Tabladillo
 
MVC Pattern. Flex implementation of MVC
MVC Pattern. Flex implementation of MVCMVC Pattern. Flex implementation of MVC
MVC Pattern. Flex implementation of MVCAnton Krasnoshchok
 
Javascript-heavy Salesforce Applications
Javascript-heavy Salesforce ApplicationsJavascript-heavy Salesforce Applications
Javascript-heavy Salesforce ApplicationsSalesforce Developers
 
Developing maintainable Cordova applications
Developing maintainable Cordova applicationsDeveloping maintainable Cordova applications
Developing maintainable Cordova applicationsIvano Malavolta
 
P Training Presentation
P Training PresentationP Training Presentation
P Training PresentationGaurav Tyagi
 

Similar to Sample Swing Dialog with Controls (20)

Test
TestTest
Test
 
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docxBroncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
 
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docxBroncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
 
Inversion Of Control
Inversion Of ControlInversion Of Control
Inversion Of Control
 
Getting start Java EE Action-Based MVC with Thymeleaf
Getting start Java EE Action-Based MVC with ThymeleafGetting start Java EE Action-Based MVC with Thymeleaf
Getting start Java EE Action-Based MVC with Thymeleaf
 
IBM Cognos 10 Framework Manager Metadata Modeling: Tips and Tricks
IBM Cognos 10 Framework Manager Metadata Modeling: Tips and TricksIBM Cognos 10 Framework Manager Metadata Modeling: Tips and Tricks
IBM Cognos 10 Framework Manager Metadata Modeling: Tips and Tricks
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
 
JavaScript Framework Smackdown
JavaScript Framework SmackdownJavaScript Framework Smackdown
JavaScript Framework Smackdown
 
01 startoff angularjs
01 startoff angularjs01 startoff angularjs
01 startoff angularjs
 
Building strong foundations apex enterprise patterns
Building strong foundations apex enterprise patternsBuilding strong foundations apex enterprise patterns
Building strong foundations apex enterprise patterns
 
Lightning salesforce
Lightning salesforceLightning salesforce
Lightning salesforce
 
object oriented testing with types of testing
object oriented testing with types of testingobject oriented testing with types of testing
object oriented testing with types of testing
 
Ctools presentation
Ctools presentationCtools presentation
Ctools presentation
 
Web sphere application server performance tuning workshop
Web sphere application server performance tuning workshopWeb sphere application server performance tuning workshop
Web sphere application server performance tuning workshop
 
Better User Experience with .NET
Better User Experience with .NETBetter User Experience with .NET
Better User Experience with .NET
 
Sas® Macro Design Patterns
Sas® Macro Design PatternsSas® Macro Design Patterns
Sas® Macro Design Patterns
 
MVC Pattern. Flex implementation of MVC
MVC Pattern. Flex implementation of MVCMVC Pattern. Flex implementation of MVC
MVC Pattern. Flex implementation of MVC
 
Javascript-heavy Salesforce Applications
Javascript-heavy Salesforce ApplicationsJavascript-heavy Salesforce Applications
Javascript-heavy Salesforce Applications
 
Developing maintainable Cordova applications
Developing maintainable Cordova applicationsDeveloping maintainable Cordova applications
Developing maintainable Cordova applications
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
 

More from Daroko blog(www.professionalbloggertricks.com)

More from Daroko blog(www.professionalbloggertricks.com) (20)

Small Business ideas you can start in Nigeria 2014(best Business ideas Nigeri...
Small Business ideas you can start in Nigeria 2014(best Business ideas Nigeri...Small Business ideas you can start in Nigeria 2014(best Business ideas Nigeri...
Small Business ideas you can start in Nigeria 2014(best Business ideas Nigeri...
 
Agriculture business ideas for 2014(Business ideas Kenya,Business ideas Niger...
Agriculture business ideas for 2014(Business ideas Kenya,Business ideas Niger...Agriculture business ideas for 2014(Business ideas Kenya,Business ideas Niger...
Agriculture business ideas for 2014(Business ideas Kenya,Business ideas Niger...
 
An Introduction to Project management(project management tutorials)
An Introduction to Project management(project management tutorials)An Introduction to Project management(project management tutorials)
An Introduction to Project management(project management tutorials)
 
java arlow jdbc tutorial(java programming tutorials)
java arlow jdbc tutorial(java programming tutorials)java arlow jdbc tutorial(java programming tutorials)
java arlow jdbc tutorial(java programming tutorials)
 
The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)The java rogramming swing _tutorial for beinners(java programming language)
The java rogramming swing _tutorial for beinners(java programming language)
 
Java programming basics notes for beginners(java programming tutorials)
Java programming basics notes for beginners(java programming tutorials)Java programming basics notes for beginners(java programming tutorials)
Java programming basics notes for beginners(java programming tutorials)
 
advanced java programming(java programming tutorials)
 advanced java programming(java programming tutorials) advanced java programming(java programming tutorials)
advanced java programming(java programming tutorials)
 
An introduction to java programming language forbeginners(java programming tu...
An introduction to java programming language forbeginners(java programming tu...An introduction to java programming language forbeginners(java programming tu...
An introduction to java programming language forbeginners(java programming tu...
 
fundamentals of Computer graphics(Computer graphics tutorials)
 fundamentals of Computer graphics(Computer graphics tutorials) fundamentals of Computer graphics(Computer graphics tutorials)
fundamentals of Computer graphics(Computer graphics tutorials)
 
bresenham circles and polygons in computer graphics(Computer graphics tutorials)
bresenham circles and polygons in computer graphics(Computer graphics tutorials)bresenham circles and polygons in computer graphics(Computer graphics tutorials)
bresenham circles and polygons in computer graphics(Computer graphics tutorials)
 
Computer Graphics display technologies(Computer graphics tutorials and tips)
Computer Graphics display technologies(Computer graphics tutorials and tips)Computer Graphics display technologies(Computer graphics tutorials and tips)
Computer Graphics display technologies(Computer graphics tutorials and tips)
 
Computer Graphics display technologies(Computer graphics tutorials)
Computer Graphics display technologies(Computer graphics tutorials)Computer Graphics display technologies(Computer graphics tutorials)
Computer Graphics display technologies(Computer graphics tutorials)
 
Displays and color system in computer graphics(Computer graphics tutorials)
Displays and color system in computer graphics(Computer graphics tutorials)Displays and color system in computer graphics(Computer graphics tutorials)
Displays and color system in computer graphics(Computer graphics tutorials)
 
Data structures graphics library in computer graphics.
Data structures  graphics library in computer graphics.Data structures  graphics library in computer graphics.
Data structures graphics library in computer graphics.
 
lecture8 clipping
lecture8 clippinglecture8 clipping
lecture8 clipping
 
Csc406 lecture7 device independence and normalization in Computer graphics(Co...
Csc406 lecture7 device independence and normalization in Computer graphics(Co...Csc406 lecture7 device independence and normalization in Computer graphics(Co...
Csc406 lecture7 device independence and normalization in Computer graphics(Co...
 
lecture4 raster details in computer graphics(Computer graphics tutorials)
lecture4 raster details in computer graphics(Computer graphics tutorials)lecture4 raster details in computer graphics(Computer graphics tutorials)
lecture4 raster details in computer graphics(Computer graphics tutorials)
 
lecture3 color representation in computer graphics(Computer graphics tutorials)
lecture3 color representation in computer graphics(Computer graphics tutorials)lecture3 color representation in computer graphics(Computer graphics tutorials)
lecture3 color representation in computer graphics(Computer graphics tutorials)
 
lecture2 computer graphics graphics hardware(Computer graphics tutorials)
 lecture2  computer graphics graphics hardware(Computer graphics tutorials) lecture2  computer graphics graphics hardware(Computer graphics tutorials)
lecture2 computer graphics graphics hardware(Computer graphics tutorials)
 
lecture1 introduction to computer graphics(Computer graphics tutorials)
lecture1 introduction to computer graphics(Computer graphics tutorials)lecture1 introduction to computer graphics(Computer graphics tutorials)
lecture1 introduction to computer graphics(Computer graphics tutorials)
 

Recently uploaded

Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxJanEmmanBrigoli
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 

Recently uploaded (20)

Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 

Sample Swing Dialog with Controls

  • 1. Poelman & Associates, Inc. (c) 2003 1 Swing A Quick Tutorial on Programming Swing Applications
  • 2. Poelman & Associates, Inc. (c) 2003 2 MVC – Model View Controller • Swing is based on this design pattern • It means separating the implementation of an application into layers or components: – The Model - the data structure that represents something (like a customer info rec) – The Controller - the user interface logic for manipulating it – The View - the display of that data structure to the user.
  • 3. Poelman & Associates, Inc. (c) 2003 3 What is Swing? • A set of classes (part of JFC) that support platform independent GUI (Graphical User Interface) • Successor to the original Java GUI classes (AWT) which didn’t work very well (they had platform dependencies that really made it a difficult API to use) • AWT wasn’t very “sexy”
  • 4. Poelman & Associates, Inc. (c) 2003 4 Swing • Visible “widgets” - windows, buttons, combo boxes, trees, tables, checkboxes, text fields, menus, … • Containers of components – applets, dialogs, windows and frames • Supporting classes and utility methods
  • 5. Poelman & Associates, Inc. (c) 2003 5 Some important Swing visible component classes • JApplet ** • JButton • JCheckBox • JColorChooser • JComboBox • JDialog ** • JFileChooser • JFormattedTextField • JFrame ** • JLabel • JList • JMenu • JMenuBar • JMenuItem • JPanel • JPasswordField • JPopupMenu • JProgressBar • JRadioButton • JScrollBar • JSlider • JSpinner • JTable • JTextArea • JTextField • JToggleButton • JToolBar • JTree • JWindow ** • ** means a top level containers
  • 6. Poelman & Associates, Inc. (c) 2003 6 Using netbeans to create a JDialog
  • 7. Poelman & Associates, Inc. (c) 2003 7 adding fields
  • 8. Poelman & Associates, Inc. (c) 2003 8
  • 9. Poelman & Associates, Inc. (c) 2003 9 my empty CustomerInfoDialog:JDialog
  • 10. Poelman & Associates, Inc. (c) 2003 10 code created
  • 11. Poelman & Associates, Inc. (c) 2003 11 To kill a zombie or running process in netbeans right click and choose: ”terminate”
  • 12. Poelman & Associates, Inc. (c) 2003 12 executing the class displays:
  • 13. Poelman & Associates, Inc. (c) 2003 13 Editing a dialog • 1St select a layout manager for the dialog
  • 14. Poelman & Associates, Inc. (c) 2003 14 select component edit properties
  • 15. Poelman & Associates, Inc. (c) 2003 15 changing the layout manager
  • 16. Poelman & Associates, Inc. (c) 2003 16 what layout manager should I use? • Start with the absolute and then experiment when you feel comfortable (or hire a graphic artist and let them worry about it ;-).
  • 17. Poelman & Associates, Inc. (c) 2003 17 Adding other components to the view - JTextFields
  • 18. Poelman & Associates, Inc. (c) 2003 18 execute the class
  • 19. Poelman & Associates, Inc. (c) 2003 19 Adding a combo box
  • 20. Poelman & Associates, Inc. (c) 2003 20 edit the model property for the combo box type in state abbreviations separated by commas
  • 21. Poelman & Associates, Inc. (c) 2003 21
  • 22. Poelman & Associates, Inc. (c) 2003 22 preferred size property hor , vert
  • 23. Poelman & Associates, Inc. (c) 2003 23 MVC Model – View – Controller Design Pattern
  • 24. Poelman & Associates, Inc. (c) 2003 24 Design Patterns • A design pattern is a way of designing code that benefits from experience of other developers – see GoF (Gang of Four) on Patterns • Design patterns are “rules of thumb” & best practices • A GUI is based on many design patterns – 3D Pliancy – Feedback – Icons – Menus – Pointing – Mnemonics & Accelerators – Many more … • A pattern usually has a name (and several aliases), a context, a problem it addresses, a description of the solution, hints of when to use it and when not to. • See http://www.csc.calpoly.edu/~dbutler/tutorials/winter96/patterns/ , http://choices.cs.uiuc.edu/sane/dpatterns.html#dp and http://www.stanford.edu/~borchers/hcipatterns
  • 25. Poelman & Associates, Inc. (c) 2003 25 MVC – Model View Controller pattern • Swing components are designed as MVC components – Model = data or object that is the to be visually represented – View = one or more visual representations of that data/object – Controller = code to manage input to the model
  • 26. Poelman & Associates, Inc. (c) 2003 26 MVC • © Sun 2002
  • 27. Poelman & Associates, Inc. (c) 2003 27 MVC in Swing Components • The Swing component class is the view and controller • A separate class is the model • Most components come with a default model • You can set the model to your own model for a control • Several controls could share a model!
  • 28. Poelman & Associates, Inc. (c) 2003 28
  • 29. Poelman & Associates, Inc. (c) 2003 29 Creating icons using the blank icon to start with
  • 30. Poelman & Associates, Inc. (c) 2003 30 Change the properties of the button to use you icon
  • 31. Poelman & Associates, Inc. (c) 2003 31 Pushing the buttons changes the displayed prices.
  • 32. Poelman & Associates, Inc. (c) 2003 32 A different and better layout
  • 33. Poelman & Associates, Inc. (c) 2003 33 JTree JFrame JFileChooser JTable JPasswordField JSlider JButton JProgressBar
  • 34. Poelman & Associates, Inc. (c) 2003 34 private javax.swing.JPasswordField jPasswordField1; private javax.swing.JTree jTree1; private javax.swing.JSlider jSlider1; private javax.swing.JProgressBar jProgressBar1; private javax.swing.JTable jTable1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton1; private javax.swing.JFileChooser jFileChooser1; private javax.swing.JLabel jLabel1;
  • 35. Poelman & Associates, Inc. (c) 2003 35 Swing based MenuLookDemo
  • 36. Poelman & Associates, Inc. (c) 2003 36 TopLevelWindows.java
  • 37. Poelman & Associates, Inc. (c) 2003 37 TopLevelWindows.java package SwingSamples; import javax.swing.*; public class TopLevelWindows { public static void main(String args[]) { JFrame myJFrame = new JFrame("The JFrame"); myJFrame.setSize(300,300); myJFrame.setLocation(100,100); JWindow myJWindow = new JWindow(); myJWindow.setSize(300,300); myJWindow.setLocation(500, 100); myJFrame.setVisible(true); myJWindow.setVisible(true); } }
  • 38. Poelman & Associates, Inc. (c) 2003 38 Top Level Containers • Must have a top level container in Swing • You must add components to the associated content pane
  • 39. Poelman & Associates, Inc. (c) 2003 39 ContentPaneExample.java package SwingSamples; import java.awt.*; import javax.swing.*; public class ContentPaneExample { public static void main(String args[]) { JFrame myJFrame = new JFrame("JFrame"); myJFrame.setLocation(100,100); Container myContentPane = myJFrame.getContentPane(); myContentPane.setLayout(new FlowLayout()); myContentPane.add(new JLabel("One")); myContentPane.add(new JLabel("Two")); myJFrame.pack(); //reformats the layout to the minimum size to fit everything myJFrame.setVisible(true); } } Without the pack() With the pack()
  • 40. Poelman & Associates, Inc. (c) 2003 40 ContentPaneExample2.java package SwingSamples; import java.awt.*; import javax.swing.*; public class ContentPaneExample2 { public static void main(String args[]) { JFrame myJFrame = new JFrame("JFrame"); myJFrame.setLocation(100,100); Container myContentPane = new JPanel(); myContentPane.add(new JLabel("One")); myContentPane.add(new JLabel("Two")); myJFrame.setContentPane(myContentPane); myJFrame.pack(); myJFrame.setVisible(true); } }
  • 41. Poelman & Associates, Inc. (c) 2003 41 Events • Swing uses them to communicate between swing components. • An event is just a method call on the receiving object by the sending object. The method passes the event object. addActionListener(ActionListener listener); removeActionListener(ActionListener listener); • An object registers to receive events. The method that gets called is: actionPerformed(ActionEvent e);
  • 42. Poelman & Associates, Inc. (c) 2003 42 Events • In Swing they are multicast – 1 to many possible. Manes multiple method calls by the send basically. • Order isn’t defined, though. • Events are immutable to the receiver. • Events may be queued as in the keyboard event queue. • Multiple events maybe compressed into one as in mouse movements.
  • 43. Poelman & Associates, Inc. (c) 2003 43 Event Modifier Flags • SHIFT_MASK • CTRL_MASK • META_MASK • ALT_MASK • BUTTON1_MASK • BUTTON2_MASK • BUTTON3_MASK • Detect when certain keys are also pressed. int modifierFlags = myEvent.getModifiers(); if ((modifierFlags & InputEvent.CRTL_MASK)!=0) System.println.out(“Pressing the contrl key”);
  • 44. Poelman & Associates, Inc. (c) 2003 44 Event Types • ComponentEvent //resized,moved, shown, hidden • FocusEvent //gained, lost • KeyEvent //typed, pressed, released • MouseEvent //clicked, pressed, released, //entered, exited • ContainerEvent //componentAdded componentRemoved • ActionEvent //fired by: JButton, JChekBox, … • AdjustmentEvent //fired by: JScrollBar • Many more ….
  • 45. Poelman & Associates, Inc. (c) 2003 45 Event Adapter Classes • Map incoming events to a method to invoke on the model to achieve the function. • Separates the View & Controller from the Model (MVC) • Prebuilt adapter has stubbed out methods for events. You only implement the ones you are interested. You do this by extending the adapter and overiding the methods you need. • Follows a general design pattern of called “adapter”. • MouseAdapter, MouseInputAdapter, MouseMotionAdapter, KeyAdapter, ComponentAdapter, ContainerAdapter, DragSourceAdapter, DropTargetAdapter, FocusAdapter, WindowAdapter, …
  • 46. Poelman & Associates, Inc. (c) 2003 46 AWT Robot! • Used to simulate keyboard and mouse programmatically. • It places events in the native system queues for the platform you are on (not just the java queue). • Used for recording and replaying activities in regression testing and other uses.
  • 47. Poelman & Associates, Inc. (c) 2003 47 Multithreading and Swing • Swing components always execute on a single thread within your application. Not the main thread of your application, either. • Swing components are NOT multithread safe! • This is done for speed but influences how you must design for them. • We can ignore this for protoyping UIs but not for design of applications.
  • 48. Poelman & Associates, Inc. (c) 2003 48 Swing Components
  • 49. Poelman & Associates, Inc. (c) 2003 49 Sample dialog with a few controls. MySampleOfSwingControls1.java
  • 50. Poelman & Associates, Inc. (c) 2003 50 JButton java.lang.Object | +--java.awt.Component | +--java.awt.Container | +--javax.swing.JComponent | +--javax.swing.AbstractButton | +--javax.swing.JButton
  • 51. Poelman & Associates, Inc. (c) 2003 51 JButton • Used for a command • Push and shows a state change visually (pliancy) • Has a name, label text,
  • 52. Poelman & Associates, Inc. (c) 2003 52 Adding items to the List
  • 53. Poelman & Associates, Inc. (c) 2003 53 Add items to the model for the list
  • 54. Poelman & Associates, Inc. (c) 2003 54 Changing the border of a list box
  • 55. Poelman & Associates, Inc. (c) 2003 55 A Titled Border for a List Box
  • 56. Poelman & Associates, Inc. (c) 2003 56 List Box Selection Modes • Single • Multiple_Interval • Single_Interval
  • 57. Poelman & Associates, Inc. (c) 2003 57 Setting the Button group on a radio button
  • 58. Poelman & Associates, Inc. (c) 2003 58 Setting the Mnemonics
  • 59. Poelman & Associates, Inc. (c) 2003 59 Final Dialog Version
  • 60. Poelman & Associates, Inc. (c) 2003 60 JComboBox • Two styles in the app – non-editable and editable • If you use the editable type you should check the input of the user to make sure it is acceptable. • You can change the style by changing the editable property.
  • 61. Poelman & Associates, Inc. (c) 2003 61 Creating a JavaGUI -> SampleForms -> Application
  • 62. Poelman & Associates, Inc. (c) 2003 62 Menu and Menu item hierarchy that defines the menus for the app
  • 63. Poelman & Associates, Inc. (c) 2003 63 The default menus
  • 64. Poelman & Associates, Inc. (c) 2003 64
  • 65. Poelman & Associates, Inc. (c) 2003 65 Copy and paste a menu into the hierarchy
  • 66. Poelman & Associates, Inc. (c) 2003 66 This shows 2 Edit menus