15
Introduction JFrame JPanel Event Listeners Summary References T HE AWT AND S WING Muhammad Adil Raja Roaming Researchers, Inc. cbna April 22, 2015

The AWT and Swing

Embed Size (px)

Citation preview

Page 1: The AWT and Swing

Introduction JFrame JPanel Event Listeners Summary References

THE AWT AND SWING

Muhammad Adil Raja

Roaming Researchers, Inc.

cbna

April 22, 2015

Page 2: The AWT and Swing

Introduction JFrame JPanel Event Listeners Summary References

OUTLINE I

1 INTRODUCTION

2 JFRAME

3 JPANEL

4 EVENT LISTENERS

5 SUMMARY

6 REFERENCES

Page 3: The AWT and Swing

Introduction JFrame JPanel Event Listeners Summary References

INTRODUCTION

In this chapter we will examine the Java AWT library forgraphical user interfaces, noting in particular how it usesthe framework concepts developed in the last chapter.The class Frame for the main windows.The Layout Manager classes.Event Listeners.User Interface Components.

Page 4: The AWT and Swing

Introduction JFrame JPanel Event Listeners Summary References

TENSION BETWEEN REUSE AND SPECIALIZATION

A truly general purpose tool cannot contain featuresspecific to any one application.Solving most problems requires application specificfeatures.How do you bridge the gap between general purpose andapplication independent tools and an application that willsolve a real problem?

Page 5: The AWT and Swing

Introduction JFrame JPanel Event Listeners Summary References

CLASSES FRAME AND JFRAME

The place to begin is the class Frame (in AWT) or JFrame(in Swing).These give the overall window for the application, and holdfeatures such as the title bar and menu bar.Many foundation methods that provide a huge amount ofstandard behavior.A few methods that are sometimes overriden, such aspaint.This is a straightforward implementation of the frameworkideas discussed in the previous chapter.he Swing library was introduced in Java 1.3, and hasslowly replaced the older AWT library.

Page 6: The AWT and Swing

Introduction JFrame JPanel Event Listeners Summary References

PANEL AND JPANEL

The window is combined with an instance of class Panel(JPanel in Swing).This class is the screen on to which drawing operations aredirected.JPanel can include components (buttons and the like) andcan refine the method paint.(The overridden method must invoke the parent method, inorder to render features of the window not included in theuser constructed portions).

EXAMPLE

class MyPanel extends JPanel {. . .public void pa in t ( Graphics g ) {

super . pa i n t ( g ) ;. . .

}}

Page 7: The AWT and Swing

Introduction JFrame JPanel Event Listeners Summary References

LAYOUT MANAGER

Here is a problem.The algorithm used to lay out various graphical elements(buttons and the like) can only be determined by the endprogrammer.But writing layout algorithms is complex, probably notsomething most programmers can do.So, how to provide the necessarily flexibility within a simpleframework?Solution – provide a variety of different layout managers(border layout, grid layout, so on), allow the endprogrammer to select one of the many choices, and give itback to the framework.

Page 8: The AWT and Swing

Introduction JFrame JPanel Event Listeners Summary References

SHOWING THE USE OF A LAYOUT MANAGER

EXAMPLE

class MyPanel extends JPanel {public MyPanel ( ) {

. . .setLayoutManger (new BorderLayout ( ) ) ;

}}

Both JPanel and BorderLayout are part of the framework,not written by the end programmer.The end programmer selects the appropriate type of layoutmanager.In this fashion the framework provides a flexible solution,without making the frame class overly large.(As it would be if it needed to hold all layout managers).

Page 9: The AWT and Swing

Introduction JFrame JPanel Event Listeners Summary References

EVENT LISTENERS

Event listeners are another good example of the use ofinheritance and overriding in the Java AWT framework.Every component (window, button, scroll bar) maintains alist of “listeners”; objects that are interested in beingnotified when an event has taken place.Each type of event is defined by an interface, such as thefollowing:

EXAMPLE

public inter face MouseListener extends EventL is tener {public void mouseClicked ( MouseEvent e ) ;public void mouseEntered ( MouseEvent e ) ;public void mouseExited ( MouseEvent e ) ;public void mousePressed ( MouseEvent e ) ;public void mouseReleased ( MouseEvent e ) ;

}

Page 10: The AWT and Swing

Introduction JFrame JPanel Event Listeners Summary References

ADAPTERS REDUCE CODE SIZE

EXAMPLE

public class MouseAdapter implements MouseListener {public void mouseClicked ( MouseEvent e ) { }public void mouseEntered ( MouseEvent e ) { }public void mouseExited ( MouseEvent e ) { }public void mousePressed ( MouseEvent e ) { }public void mouseReleased ( MouseEvent e ) { }

}

Page 11: The AWT and Swing

Introduction JFrame JPanel Event Listeners Summary References

CREATING A LISTENER

The programmer then creates a new class that subclasses theadapter and overrides the methods of interest, then registers aninstance of the class with the component.

EXAMPLE

class MyWindow extends JFrame {public MyWindow ( ) {

. . .addMouseListener (new MouseKeeper ( ) ) ;

}. . .private class MouseKeeper extends MouseAdapter {

public void mousePressed ( MouseEvent e ) {. . .

}}

}

Inner classes are particularly usefor for this idiom.

Page 12: The AWT and Swing

Introduction JFrame JPanel Event Listeners Summary References

BUTTONS, SLIDERS, TEXT BOXES, AND SO ON

Graphical components (Buttons, Sliders, Text Boxes, andmany many more) are provided in the standard library.The user places an instance of a class into the window.Most use the listener event model to notify when they arechanged.

EXAMPLE

Button butn = new Button ( " do i t ! " ) ;add ( " North " , butn ) ; / / p lace a t top o f screenbutn . addAct ionL is tener (new d o I t ( ) ) ; / / add l i s t e n e r. .private class d o I t implements Ac t i onL i s t ene r {

public void act ionPerformed ( Act ionEvent e ) {/ / what ever do i t does. . .

}}

Page 13: The AWT and Swing

Introduction JFrame JPanel Event Listeners Summary References

COMBINING COMPONENT AND LISTENER IN ONE CLASS

Sometimes, a better encapsulation results from combininginheritance from the component class with implementationof the listener.The component then becomes its own listener.

EXAMPLE

class ColorBut ton extends Button implements Ac t i onL i s t ene r {private Color ourColor ;

public ColorBut ton ( Color c , S t r i n g name) {super (name ) ; / / c reate the but tonourColor = c ; / / save the co lo r valueaddAct ionL is tener ( th is ) ; / / add ourse lves as l i s t e n e r

}

public void act ionPerformed ( Act ionEvent e ) {/ / se t c o l o r f o r middle panel

setFromColor ( ourColor ) ;}

}

Page 14: The AWT and Swing

Introduction JFrame JPanel Event Listeners Summary References

SUMMARY

The AWT illustrates the application of the frameworkconcept at many different levels.In this chapter we have examined.The window class, inheriting from Frame.The panel class, inheriting from JPanel.The layout manager, the user selecting one of severalalternatives.The event model, using the concept of listeners.The standard components.

Page 15: The AWT and Swing

Introduction JFrame JPanel Event Listeners Summary References

REFERENCES

Images and content for developing these slides have beentaken from the follwoing book with the permission of theauthor.An Introduction to Object Oriented Programming, TimothyA. Budd.This presentation is developed with Beamer:

Darmstadt, monarca.