Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
F27SB2 Programming Languages
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Corresponds with Chapter 12
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Understanding SWING Architecture CS 4170 UI Design Hrvoje Benko Oct. 9, 2001.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
Advanced Java Class GUI – part 1. Intro to GUI GUI = Graphical User Interface -- “Gooey” Just because it’s “gooey” does not mean you may write messy code.
Chapter 121 Window Interfaces Using Swing Chapter 12.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
© The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Adding Graphics to a Frame Application Applets: Can generate drawings by overriding paint Frame: Do not draw directly on a frame. Draw graphics on a JPanel.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
Session 27 Swing vs. AWT. AWT (Abstract Window ToolKit) It is a portable GUI library for stand-alone applications and/or applets. The Abstract Window.
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
CompSci 100E 35.1 Graphical User Interfaces: GUIs  Components  Flat Layouts  Hierarchical Layouts  Designing a GUI  Coding a GUI.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Layout Managers Arranges and lays out the GUI components on a container.
Object Oriented Programming Engr. M. Fahad Khan Lecturer, Software Engineering Department University of Engineering & Technology, Taxila.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
University of Limerick1 Software Architecture Java Layout Managers.
CS3340: Swing and Event handling L. Grewe. Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel,
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
Review_6 AWT, Swing, ActionListener, and Graphics.
Basics of GUI Programming Chapter 11 and Chapter 22.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
GUI.1 Graphical User Interfaces GUIs. GUI.2 The Plan Components Flat Layouts Hierarchical Layouts Designing a GUI Coding a GUI.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
A First Look at GUI Applications
Graphical User Interface (pronounced "gooey")
Tim McKenna Layout Mangers in Java Tim McKenna
Ellen Walker Hiram College
Chap 7. Building Java Graphical User Interfaces
Chapter 13: Advanced GUIs and Graphics
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

Swing

Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc.. Most Swing Components are “Lightweight” Use Java code rather than native code to draw in the underlying window. The Exceptions are JFrame, JApplet, JWindow, and JDialog Use Content Pane for adding Components to “Heavyweight” Containers To add Components to a JApplet (in method init( )) use Container cp = new getContentPane( ); cp.add(new JButton(“Start”);

Swing Differences between Swing and AWT Components Use of paintComponent for Drawing public void paintComponent(Graphics g) { super.paintComponent(g); //always call super.paintComponent( ) before performing custom drawing // draw Shape on the Swing Component } Double Buffering In Swing a JPanel is used instead of a Canvas to be the target for drawing graphics objects. When drawing to a JPanel using paintComponent double buffering is automatically employed. There is an option to perform drawing directly to the screen using the paint method by using panel.getGraphics( );

Swing Additional features of Swing Components All of the “lightweight” Swing classes inherit from JComponent. The four “heavyweight” components – JFrame, JApplet, JWindow, and JDialog – inherit from their AWT counterparts. Unlike their AWT counterparts, Swing uses setXXX( ) and getXXX( ) to access private attributes. This designation is a bit easier for users to remember, and lends itself to the documentation conventions used by Java Beans and class Introspector( ) to display information about a bean. The default layout manager for a JApplet is BorderLayout.

Swing In many ways Swing is an improvement upon the original Java design and the AWT… BUT Many browsers (none before Netscape 6.0) cannot run the most current versions of the JRE (Java Runtime Environment) and will not run applets using swing components. If you want your applet to be viewable by the widest audience use AWT components.

Swing The Java Plug-In A plug-in is available from Sun that, once installed, allows browsers to run the most current version of the JRE. This allows you to deliver applets that use the more recent JRE classes (like Swing and 2D- graphics) that can run over Netscape and IE browsers This capability comes at a price! The plug-in is 5 MB, and downloading the plug-in is not a viable solution for an internet client. The plug-in may be obtained at:

Swing Example 1 – an applet with 3 buttons and 2 text fields Step1 – Design the interface ReadWriteClear Enter text Correct Entry? Applet The JApplet will consist of 2 JPanels – The top one containing labels and text fields The bottom one containing three JButtons The browser will add a title bar when the applet is running

Swing ReadWriteClear Enter text Correct Entry? Applet The top Panel will use a GridLayout with 2 rows and 2 columns (with a separation of 10 pixels)

Swing //(global) attributes of the class private MyJTextField source = new MyJTextField(25); private MyJTextField target = new MyJTextField(25); private JLabel edit = new JLabel("Enter text"); private JLabel verify = new JLabel("Correct Entry?"); private String inStr = new String(""); //method to construct the top panel private JPanel makeJTextPanel( ) { JPanel theText = new JPanel( ); theText.setLayout(new GridLayout(2,2, 10, 10)); theText.add(edit); theText.add(source); theText.add(verify); theText.add(target); return theText; } Construct the top panel – write a method makeJTextPanel( ) Add components to the grid – left to right, top to bottom

Swing ReadWriteClear Enter text Correct Entry? Applet Construct the bottom Panel – Use FlowLayout (center adjusted) Top JPanel Bottom JPanel

Swing Construct the bottom panel – with a method private MyJButton read; private MyJButton write; private MyJButton clear; //build the bottom panel with method makeJButtonPanel( ) private JPanel makeJButtonPanel() { read = new MyJButton ("Read", source); write = new MyJButton("Write", target); clear = new MyJButton("Clear", target); JPanel theButtons = new JPanel( ); theButtons.setLayout(new FlowLayout(FlowLayout.CENTER)); theButtons.add(read); theButtons.add(write); theButtons.add(clear); theButtons.setBackground(Color.blue); return theButtons; } Create the bottom panel Set its layout and color, and add the buttons to it (left to right)

Swing Place the panels into the applet in method init( ) public void init( ) { Container cp = getContentPane( ); theButtons = makeJButtonPanel( ); cp.add(BorderLayout.SOUTH, theButtons); theText = makeJTextPanel( ); cp.add(BorderLayout.CENTER, theText); } For heavyweight component like JApplet components must be added to a ContentPane //add theButtons (a JPanel) to the ContentPane //the default LayoutManager of a JApplet is BorderLayout

Swing Design of the System A button event will cause a read, write, or clear of a text field. JPanelJAppletMyJTextField MyJTextField(int size) doButtonAct(MyJButton) String response MyJButtonJLabel MyJButton(String, MyJTextField) actionPerformed(ActionEvent) MyJTextField target ActionListener activates The applet is an aggregate of 2 JPanels 2 32 describes JPanels contain JLabels and MyJTextField objects OR MyJButtons MyJButton objects interact with MyJTextField objects

Swing class MyJButton extends JButton implements ActionListener { private MyJTextField target; public MyJButton(String name, MyJTextField targ) { Font buttonFont = new Font ("Times Roman", Font.BOLD, 12); setFont(buttonFont); setText(name); setBackground(Color.cyan); target = targ; addActionListener(this); } public void actionPerformed(ActionEvent e) { target.doButtonAct(this); } class MyJButton Constructor receives handle to text field Hold handle to target text field Event causes message to be sent to the target to perform a read or write

Swing class MyJTextField extends JTextField { private String response = new String( ); public MyJTextField(int size) { super(size); } public void doButtonAct(MyJButton source) { response = source.getText( ); if (response.equals("Read") ) inStr = super.getText( ); else if (response.equals("Write") ) setText(inStr); else if (response.equals("Clear") ) setText(""); } A MyJButton object notifies a MyJTextField of a button event and elicits an action

Swing The Applet JButtons -- implementation of this design JButtons.html JButtons.html

Swing Adding scroll bars to a component such as a JTextArea Just wrap a JTextArea object inside a JScrollPane. JTextArea ta = new JTextArea(10, 25); JScrollPane sp = new JScrollPane(ta); The programmer can control which scrollbars are allowed – vertical, horizontal, both, or neither JScrollPane sp = new JScrollPane(ta, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

Swing Example 2. Drawing Shapes on a JPanel

Swing ArcLineEllipseRectangle Rounded Rectangle Circle RGB Colors R G B JApplet MainPanel Add a JButtonPanel to the North Add a JPanel to the East that….Contains a JLabel Panel and… A JScrollBar PanelFinally add a JPanel to the Center (whatever is left with no West and South) to serve as a canvas on which you will draw the shapes. Remember! You must get a ContentPane from the JApplet on which you will add these panels.

Swing The applet will contain the following classes: MyCanvas – a JPanel that will be the object where the shapes will be placed. This class will need to hold the shape objects in a container, implement the method paintComponent( ) for painting the contents of this container, and discover when and which JButton and JScrollBar events occur and handle the repainting commands that they require. MyJScrollBar – Three scroll bars with adjustment listeners handle adjustment events and notify the MyCanvas object to change its background color. MyJButton – Butttons with action listeners respond to an event by notifying the MyCanvas object to add a new shape to its collection and indicate the specific shape that has been requested. A class Shape that is extended by such classes as Line, Ellipse, Rectangle, etc. – These classes hold color and size information and provide a draw method for locating and painting the shape on the canvas.

Swing Class Shape uses a BoundsBox object, that is located and adjusted by pressing and dragging a mouse on the MyCanvas object, to determine the location and size parameters of each shape that is drawn. The class MyCanvas object must be notified of and respond to button and scroll bar events. It must also implement the MouseListener and MouseMotionListener interfaces to respond to mousePressed( ) and mouseDragged( ) events that determine the location and extent of the BoundsBox object. Classes MyJButton and MyJScrollBar must maintain a permanent handle to the MyCanvas object that they must notify when one of their events occur.

Swing Here is the applet that implements the previous design Draw Shapes