snottybugbearSoftware and s/w Development

Nov 3, 2013 (5 years and 5 months ago)


Comp 2014
Java Swing
• What is Swing
• Interface Elements
– Windows (JFrame, JPanel)
– Buttons
– Menus
– ToolBars
• Event Handling
– Adding listeners for specific mouse/keyboard events
• Custom Interface Draw Techniques
– Graphics Context
What is Swing
• The javax.swing package is a collection of
classes and interfaces which allow
developers to quickly produce “industry
standard” interfaces
• It provides interfaces/base
implementations to many standard GUI
coding problems
• In many ways it is the successor to awt
• In order to create a GUI there must be
somewhere everything is held such as
buttons, menus, editors etc..
• The primary containers are
–JFrame ~ an Application
–JPanel~dialogs, editor panes etc..
• There are also
JFrame Usage
1.Create a class that extends JFrame
• public MyFrame extends JFrame
2.Create a constructor
public MyFrame() {
try {
catch(Exception e) {
3.Override the jbInit() method for your JFrame
• This is where you initialise your frames features
• Size, look, dispose behaviour
• Also call this.show() to make the frame visible
• You must set a content pane, which is a JPanel
JPanel usage
• JPanels are created the same way as JFrames
with two key distinctions
• Firstly in the constructor you do not enable
public Frame1() {
try {
catch(Exception e) {
• You do not need to make the Panel visible within
jbInit() i.e. this.show()
• In Swing everything inherits from Component
• Component provides a base interface which allows
components to be added to each other i.e. buttons can
be added to Toolbars.
• Buttons are an extension of Component
– JButton//Standard button i.e. ok
– JRadioButton//selection button
– JToggleButton//on/off buttons
– JCheckBox// choice buttons
JFrame adding components
• Adding Components to a JFrame
• All components are added to the content pane which is
an embedded JPanel within the Frame
• The contentpane acts as a place holder, and can be
accessed from
– myFrame.getContentPane();
• All components can be added as follows:
– myFrame.getContentPane().add(component);
• Adding JMenubars
– this.setJMenuBar(myMenuBar);
JPanel adding components
• Adding Components to a Panel
– myPanel.add(component);
• Once a component is added to a Panel
you must invalidate, then validate the
Panel followed by a call to repaint()
• This updates the Panel to make the newly
added component available
• Frames are the only thing that should have
a menu.
• If you’ve created a new application
JBuilder will automatically add a standard
Menu bar to your app i.e. File, Edit Help
• To edit switch into Design view of your
frame class
• Select the menubar from the bottom left of
the screen
• The designer will now change to edit the menu
• To add a new command click on the menu you wish to add it to
• In the drop down select where you wish to place it and fill in its details
• To add a new menu click in an empty area of the menubar and fill in its
Event Handling
• An event is when a User interacts with a system
– Mouse clicks
– Mouse Motion
– Keyboard Input
– Etc…
• Events are dealt with in Swing by three components
– An Event Handling Stub //method that is invoked by event
– An EventAdapter//Method in the Event listener which invokes
the handling stub
– An EventListener// A class which monitors for the event
• The following example adds an MouseClicklistener to a
Event Handling
User causes an
event i.e. mouse
Event is passed
to Event Handler
class by adaptee
Adapter class
•Event handler
processes event.
•Notifies all
adaptees, it is
attached to, of
•By invoking their
MyFrame.onEvent ()
Event Handling : Example
public MyMouseCliskListener extends Mouse java.awt.event.MouseAdapter{
MyPanel adaptee;
public MyMouseClickedListener(MyPanel adaptee)
this.adaptee = adaptee;
public void mousePressed(MouseEvent e) {
this.adaptee.mouseLeft Pressed();
Event Handling: Example
public MyPanel extends JPanel{

public void jbInit(){

public void mouseleftPressed()

Event Handling
• Types of events
–Window Events: window minimised, closed,
maximised, resized etc..
–Action Events: Menu items clicked, Buttons
–Menu Events: Menu selected,closed etc..
–Mouse Events: Mouse clicked, dragged,
released, moved
–Item Events: Events specific to components,
selected, dropped etc…
Event Handling: JBuilder
• Good News is in JBuildermost of this code is generated
automatically for you
1.Switch to the design view of the element you wish to add an event
handler to.
Event Handling: JBuilder
• You can now select which event you would like to handle
• As you can see from the selection above I’ve chosen the
Mouseclicked event
• You can then give a name to this even i.e. MyFrameMouseClicked
• Hit enter and JBuilder will create the skeleton code you need to
handle this event
• NOTE: You must complete this code yourself
Graphics Context
• All components, JFrames, JPanels etc. have a
Paint method which can be overidden
– public void paint(Graphics g)
• This allows for custom interface drawing e.g.
– Wish to change the look of a button
– Change the background content of a Panel
– Or Draw Shapes on a Panel!!!
• The Graphics object allows you to draw
– Squares (filled or standard)
– Circles(filled or standard)
– Lines(filled or standard)
– Text
– Change colours
Graphics Context: Example
• Sample paint method for a JPanel
public void paint(Graphics g){
Color oldColor = g.getColor();
• Methods worth knowing
– setColor(Color color)
– drawLine(int x,int y,int width,int height)
– drawRect(int x,int y,int width,int height)
– drawOval(int x,int y,int width,int height)
• Don’t use them
–Primarily used for web apps
–A nice tool if you need to quickly design an
–For the project they are a hindrance you need
to be able to freely place elements in your
–Set all layouts to null in the designer
Todays Lab
• Complete the Swing code
–Create your own panel class with a button
located in the centre of the panel
–Setup Listeners so that when you click and
drag the button it should move around with
the mouse
–Add the panel to the frame
• Any questions