All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

Similar documents
Sri Vidya College of Engineering & Technology

Introduction to the JAVA UI classes Advanced HCI IAT351

Graphical User Interfaces (GUIs)

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Java: Graphical User Interfaces (GUI)

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

What is Widget Layout? Laying Out Components. Resizing a Window. Hierarchical Widget Layout. Interior Design for GUIs

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Laying Out Components. What is Widget Layout?

CS 251 Intermediate Programming GUIs: Components and Layout

GUI in Java TalentHome Solutions

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Java. GUI building with the AWT

Table of Contents. Chapter 1 Getting Started with Java SE 7 1. Chapter 2 Exploring Class Members in Java 15. iii. Introduction of Java SE 7...

SD Module-1 Advanced JAVA

SD Module-1 Advanced JAVA. Assignment No. 4

BASICS OF GRAPHICAL APPS

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

Widget. Widget is a generic name for parts of an interface that have their own behaviour. e.g., buttons, progress bars, sliders, drop-down

User interfaces and Swing

Graphic User Interfaces. - GUI concepts - Swing - AWT

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Graphical User Interfaces. Comp 152

Object Oriented Design & Patterns. Part 1

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place.

Java continued. Chapter 8 ALERT ALERT. Last week. MCQ test next week. This time. This place. Closed book. Assignment #2 is for groups of 3

Layout. Dynamic layout, Swing and general layout strategies

CS11 Java. Fall Lecture 4

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

GUI Programming. Chapter. A Fresh Graduate s Guide to Software Development Tools and Technologies

Basicsof. JavaGUI and SWING

Widgets. Overview. Widget. Widgets Widget toolkits Lightweight vs. heavyweight widgets Swing Widget Demo

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Lecture 18 Java Graphics and GUIs

Graphical User Interface (GUI)

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Systems Programming Graphical User Interfaces

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

GUI Components: Part 1

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

Graphical User Interfaces in Java - SWING

China Jiliang University Java. Programming in Java. Java Swing Programming. Java Web Applications, Helmut Dispert

CSC207H: Software Design Lecture 11

Containers and Components

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

Part I: Learn Common Graphics Components

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Graphical User Interface (GUI)

CS11 Java. Fall Lecture 3

Introduction... xv SECTION 1: DEVELOPING DESKTOP APPLICATIONS USING JAVA Chapter 1: Getting Started with Java... 1

CSE 331 Software Design & Implementation

Swing from A to Z Some Simple Components. Preface

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

Packages: Putting Classes Together

Chapter 6: Graphical User Interfaces

Swing from A to Z Using Focus in Swing, Part 2. Preface

Charlie Garrod Bogdan Vasilescu

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Resizing a Window. COSC 3461: Module 5B. What is Widget Layout? Size State Transitions. What is Widget Layout? Hierarchical Widget Layout.

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

What is Widget Layout? COSC 3461 User Interfaces. Hierarchical Widget Layout. Resizing a Window. Module 5 Laying Out Components

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Tool Kits, Swing. Overview. SMD158 Interactive Systems Spring Tool Kits in the Abstract. An overview of Swing/AWT

CSE 331 Software Design & Implementation

Agenda. Container and Component

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

Graphical User Interfaces

Graphical User Interfaces. Swing. Jose Jesus García Rueda

PIC 20A GUI with swing

Lecture 19 GUI Events

Chapter 12 Advanced GUIs and Graphics

Jonathan Aldrich Charlie Garrod

Layout. Dynamic layout Layout design pattern Layout strategies

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Introduction to concurrency and GUIs

EPITA Première Année Cycle Ingénieur. Atelier Java - J5

Layout. Dynamic layout Layout design pattern Layout strategies. 2.6 Layout 2

Dr. Hikmat A. M. AbdelJaber

CHAPTER 2. Java Overview

Programming graphics

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

Graphical User Interface (GUI)

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

Window Interfaces Using Swing Objects

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

Lecture 9. Lecture

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Graphical interfaces & event-driven programming

No SVN checkout today. Object-Oriented Design

CS 349 Midterm Exam Spring 2014

CS 2113 Software Engineering

Introduction p. 1 JFC Architecture p. 5 Introduction to JFC p. 7 The JFC 1.2 Extension p. 8 Swing p. 9 Drag and Drop p. 16 Accessibility p.

CS 4300 Computer Graphics

Announcements. Introduction. Lecture 18 Java Graphics and GUIs. Announcements. CSE 331 Software Design and Implementation

Transcription:

Q1. If you add a component to the CENTER of a border layout, which directions will the component stretch? A1. The component will stretch both horizontally and vertically. It will occupy the whole space in the middle. Q2. What is the base class for all Swing components? What design pattern does this follow? A2. Design pattern: As you can see from the diagram below, containers collect components. Sometimes you want to add a container to another container. So, a container should be a component. For example container.getpreferredsize() invokes getpreferredsize() of all contained components. Composite design pattern is used in GUI components to achieve this. A composite object is an object, which contains other objects. Composite design pattern manipulates composite objects just like you manipulate individual components. Composite design pattern All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

Swing components Q3. Explain the Swing event dispatcher mechanism? A3. Swing components can be accessed by the Swing event dispatching thread. A few operations are guaranteed to be thread-safe but most are not. Generally the Swing components should be accessed through this event-dispatching thread. The event-dispatching thread is a thread that executes drawing of components and event-handling code. For example the paint() and actionperformed() methods are automatically executed in the eventdispatching thread. Another way to execute code in the event-dispatching thread from outside event-handling or drawing code, is using SwingUtilitiesinvokeLater() or invokeandwait() method. Swing lengthy initialization

tasks (e.g. I/O bound and computationally expensive tasks), should not occur in the event-dispatching thread because this will hold up the dispatcher thread. If you need to create a new thread for example, to handle a job that s computationally expensive or I/O bound then you can use the thread utility classes such as SwingWorker or Timer without locking up the event-dispatching thread. SwingWorker creates a background thread to execute time consuming operations. Timer creates a thread that executes at certain intervals. However after the lengthy initialization the GUI update should occur in the event dispatching thread, for thread safety reasons. We can use invokelater() to execute the GUI update in the event-dispatching thread. The other scenario where invokelater() will be useful is that the GUI must be updated as a result of non-awt event. Q4. What do you understand by MVC as used in a JTable? A4. MVC stands for Model View Controller architecture. Swing J components (e.g. JTable, JList, JTree etc) use a modified version of MVC. MVC separates a model (or data source) from a presentation and the logic that manages it. Component (e.g. JTable, JTree, and JList): coordinates actions of model and the UI delegate. Each generic component class handles its own individual view-andcontroller responsibilities. Model (e.g. TableModel): charged with storing the data. UIDelegate: responsible for getting the data from model and rendering it to screen. It delegates any look-and-feel aspect of the component to the UI Manager.

Q5. Explain layout managers? What design pattern does it use? A5. Layout managers are used for arranging GUI components in windows. The standard layout managers are: FlowLayout: Default layout for Applet and Panel. Lays out components from left to right, starting new rows if necessary. BorderLayout: Default layout for Frame and Dialog. Lays out components in north, south, east, west and center. All extra space is placed on the center. CardLayout: stack of same size components arranged inside each other. Only one is visible at any time. Used in TABs. GridLayout: Makes a bunch of components equal in size and displays them in the requested number of rows and columns. GridBagLayout: Most complicated but the most flexible. It aligns components by placing them within a grid of cells, allowing some components to span more than one cell. The rows in the grid aren t necessarily all the same height, similarly, grid columns can have different widths as well. BoxLayout: is a full-featured version of FlowLayout. It stacks the components on top of each other or places them in a row. Complex layouts can be simplified by using nested containers for example having panels within panels and each panel can use its own LayoutManager. It is also possible to write your own layout manager or use manual positioning of the GUI components. Note: Further reading on each LayoutManagers is recommended for Swing developers. Design pattern: The AWT containers like panels, dialog boxes, windows etc do not perform the actual laying out of the components. They delegate the layout functionality to layout managers. The layout managers make use of thestrategy design pattern, which encapsulates family of algorithms for laying out components in the containers. If a particular layout algorithm is required other than the default algorithm, an appropriate layout manager can be instantiated and plugged into the container. For example, panels by default use the FlowLayout but it can be changed by executing: setlayout(new GridLayout(4,5));

This enables the layout algorithms to vary independently from the containers that use them. This is one of the key benefits of the strategy pattern. Q6. Explain the Swing delegation event model? A6. In this model, the objects that receive user events notify the registered listeners of the user activity. In most cases the event receiver is a component. Event Types: ActionEvent, KeyEvent, MouseEvent, WindowEvent etc. Event Processors: JButton, JList etc. EventListeners: ActionListener, ComponentListener, KeyListener etc. 1) What is Event-Driven-Thread (EDT) in Swing? Event-Driven-Thread or EDT is a special thread in Swing and AWT. Event-Driven Thread is used to draw graphics and listen for events in Swing. You will get a bonus point if you able to highlight that time consuming operations like connecting to database, opening a file or connecting to network should not be done on EDT thread because it could lead to freezing GUI because of blocking and time consuming nature of these operations instead they should be done on separate thread and EDT can just be used to spawn those thread on a button click or mouse click. 2) Does Swing is thread safe? What do you mean by swing is not thread-safe? This swing interview questions is getting very popular now days. Though it s pretty basic many developer doesn't understand thread-safety issue in Swing. Since Swing components are not thread-safe it means you can not update these components in any thread other than Event-Driven-Thread. If you do so you will get unexpected behavior. Some time interviewer will also ask what are thread-safe methods in swing which can be safely called from any thread only few like repaint() and revalidate().

3) What are differences between Swing and AWT? One of the classic java swing interview questions and mostly asked on phone interviews. There is couple of differences between swing and AWT: 1) AWT component are considered to be heavyweight while Swing component are lightweights 2) Swing has plug-gable look and feel. 3) AWT is platform depended same GUI will look different on different platform while Swing is developed in Java and is platform dependent. 4) Why Swing components are called lightweight component? Another popular java swing interview question, I guess the oldest that is generally comes as followup of earlier question based on your answer provided. AWT components are associated with native screen resource and called heavyweight component while Swing components is uses the screen resource of an ancestor instead of having there own and that's why called lightweight or lighter component. 5) What is difference between invokeandwait and invokelater? This swing interview question is asked differently at different point. some time interviewer ask how do you update swing component from a thread other than EDT, for such kind of scenario we use SwingUtilities.invokeAndWait(Runnable r) and SwingUtilities.invokeLetter(Runnable r) though there are quite a few differences between these two, major one is invokeandwait is a blocking call and wait until GUI updates while invokelater is a non blocking asynchronous call. In my opinion these question has its own value and every swing developer should be familiar with these questions or concept not just for interview point of view but on application perspective. you can read more on my post How InvokeAndWait and InvokeLater works in Swing 6) Write code for JTable with custom cell editor and custom cell renderer? Now comes harder part of swing interviews, questions asked in this part of Swing interview is mostly about writing code and checking developer s capability of API familiarity, key concept of swing code etc. Programming Questions: 1. Find the component that best fits each of the following needs. Write down both the component s common name (such as frame ) and find the component's how-to page online. a. A component that lets the user pick a color. b. A component that displays an icon, but that doesn t react to user clicks. c. A component that looks like a button and that, when pressed, brings up a menu of items for the user to choose from. d. A container that looks like a frame, but that appears (usually with other, similar containers) within a real frame.

e. A container that lets the user determine how two components share a limited amount of space. 2. Which method do you use to add a menu bar to a top-level container such as a JFrame? 3. Which method do you use to specify the default button for a top-level container such as a JFrame or JDialog? 4. Which method do you use to enable and disable components such as JButtons? What class is it defined in? 5. a. Which Swing components use ListSelectionModel? [Hint: The Use link at the top of the specification for each interface and class takes you to a page showing where in the API that interface or class is referenced.] b. Do those components use any other models to handle other aspects of the components state? If so, list the other models types. 6. Which type of model holds a text component s content? 1. Find the component that best fits each of the following needs. Write down both the component s common name (such as frame ) and find the component's how-to page online. a. A component that lets the user pick a color. b. A component that displays an icon, but that doesn t react to user clicks. c. A component that looks like a button and that, when pressed, brings up a menu of items for the user to choose from. d. A container that looks like a frame, but that appears (usually with other, similar containers) within a real frame. e. A container that lets the user determine how two components share a limited amount of space. 2. Which method do you use to add a menu bar to a top-level container such as a JFrame? 3. Which method do you use to specify the default button for a top-level container such as a JFrame or JDialog? 4. Which method do you use to enable and disable components such as JButtons? What class is it defined in?

5. a. Which Swing components use ListSelectionModel? [Hint: The Use link at the top of the specification for each interface and class takes you to a page showing where in the API that interface or class is referenced.] b. Do those components use any other models to handle other aspects of the components state? If so, list the other models types. 6. Which type of model holds a text component s content?