Java Swing Toolkit Graphics - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Java Swing Toolkit Graphics

Description:

the basic components of the graphics library. the patterns that are used to ... Containers are the basic enclosing element of all graphical applications. ... – PowerPoint PPT presentation

Number of Views:99
Avg rating:3.0/5.0
Slides: 27
Provided by: cseBu
Category:

less

Transcript and Presenter's Notes

Title: Java Swing Toolkit Graphics


1
Java Swing Toolkit Graphics
  • The key to effectively using graphics in Java is
    understanding
  • the basic components of the graphics library
  • the patterns that are used to combine components

2
Some patterns used
  • OBSERVER
  • STRATEGY
  • COMPOSITE
  • DECORATOR

3
Components
  • All displayable objects in the graphics hierarchy
    extend this base class. You will probably never
    create an instance of this class directly, but
    instead use the pre-defined GUI elements that
    Java provides.

4
Containers
  • Containers are the basic enclosing element of all
    graphical applications. You cant have a
    graphical app without an outer enclosing
    container to put the rest of your graphical
    components in.
  • The top level container for an application is
    usually a Frame (Or JFrame)

5
Frames
  • A frame is a top-level window with a title and a
    border.
  • Frames also support menu bars.
  • Lets look at some code to create a basic frame.
  • FrameTest.java

6
Hello World, Swing Style
  • public class FrameTest
  • JFrame frame new JFrame()
  • public void init()
  • frame.getContentPane().add(new JLabel(Hello
    World))
  • frame.pack()
  • frame.show()
  • public static void main (String args)
  • FrameTest app new FrameTest()
  • app.init()

7
Composite Pattern
8
Composite Pattern
  • The whole graphical framework is a hierarchy of
    components. This is an almost textbook example
    of the composite pattern. A frame contains many
    children, which all comply to the same interface
    (Component), and a call to, for example,
    repaint() on the frame will also call repaint()
    on all its children.

9
Input Elements
  • The whole point of a GUI is to accept user input
    and do something with it. To this end, there are
    a large number of input components in Swing.
  • Lets first examine the JButton.
  • FrameButton.java

10
Button Example
  • JButton button new JButton(Push Me)
  • Frame.getContentPane.add(button)
  • button.addActionListener(new ActionListener()
  • public void actionPerformed(ActionEvent e)
  • label.setText(Clicked)
  • )

11
OBSERVER PATTERN
  • Idea decouple event from event handling

Abstract Observable
Abstract Observable
Abstract Observable
Abstract Observer
attach(Observer) detach(Observer) notifyObservers(
)
update()
0..
Concrete Observable
Concrete Observer
12
JFC use of OBSERVER
  • The observer pattern is used for event
    notification.
  • Observables (classes like JButton) generate
    events.
  • An observable can have many observers.

13
OBSERVER PATTERN
  • Terminology differs slightly in JFC classes

Abstract Observable
Abstract Observable
JButton
ActionListener
addActionListener(ActionListener)
actionPerformed(ActionEvent)
0..
14
Multiple Observers
  • We can add more than one observer to our button.
    All observers are notified when a button event
    occurs. Lets add an observer that will also
    change the color of the label.
  • button.addActionListener(new ActionListener()
  • public void actionPerformed(ActionEvent e)
    setLabelColor()
  • )

15
The Text Field
  • Lets look at another input widget in Javas
    toolkit, the Text Field
  • Text fields are one of the most common data input
    elements for an application. They can also be
    one of the most prone to problems, because they
    allow entry of free-format information, pushing
    validation to the client.
  • For example, if your text field is collecting a
    number from the user, nothing is stopping them
    from entering q, which may cause problems for
    the client.
  • See TextWidget.java

16
The Combo Box
  • Often, the input that you want from the users is
    one of a finite set of choices. In order to keep
    the user from entering an invalid value via a
    text field widget, you can give them a combo box
    (dropdown list) that allows them to pick one of a
    restricted set of values. This allows you to
    guard against invalid input from the user.
  • See Combo.java

17
The List
  • Weve already seen many implementations of the
    List ADT. Now lets take a look at one way in
    which we can represent a list graphically using
    Swing. The class is called (Surprise, surprise)
    JList.
  • See ListWidget.java

18
DECORATOR
  • A decorator adds functionality while maintaining
    an interface.
  • One example
  • InputStreamReader wraps InputStream
  • BufferedReader wraps InputStreamReader
  • Another example
  • JScrollPane wraps Jlist

19
But, some list elements dont show up!
  • Swing makes use of the decorator pattern to
    provide scroll functionality to various
    components (those that implement the scrollable
    interface) via the ScrollPane.
  • See ScrollListWidget.java

20
Why is the list so scrunched up?
  • In our example program so far, we can see that
    the layout of the various components is not
    exactly ideal. The list doesnt show up the
    right size, the label is not centered, maybe we
    would like to move the list to the center, or any
    number of other things. What controls how the
    components appear in the frame? The answer is
    the Layout Manager.

21
STRATEGY
  • A layout manager has responsibility for laying
    out components within a container.
  • Unlike in NGP, JFC containers do not have fixed
    layout managers.
  • Layout managers are treated as strategies.
  • Strategies can be swapped.

22
Layout Managers
  • Swing has several different layout managers.
    Some of the most common are FlowLayout,
    BorderLayout, GridLayout, and GridBag Layout.
    Each has advantages and disadvantages.
  • The JFrame component defaults to BorderLayout.

23
BorderLayout
  • The BorderLayout manager creates an object that
    resembles a picture with a four sided frame, or
    border.

24
FlowLayout
  • FlowLayout is arguably the simplest layout
    manager. It just stacks up components in a row,
    right to left. If it runs out of space, it wraps
    to a new line. This is the default layout
    manager for JPanel. You can see the behavior in
    how the button, text area, and combo box are laid
    out.

25
GridLayout
  • GridLayout arranges its components in an equally
    spaced grid of cells. Will take as much space as
    is available to it.

26
GridBag Layout
  • GridBag is both the most flexible of the layout
    managers, and the most difficult to use. It
    allows you customize the size and growth of all
    the components separately, but setting it up is a
    pain.
Write a Comment
User Comments (0)
About PowerShow.com