KEMBAR78
Cs315 Advance Java Reviewer | PDF | Window (Computing) | Software
0% found this document useful (0 votes)
8 views9 pages

Cs315 Advance Java Reviewer

The document outlines the key aspects of user interface design, including UI elements, layouts, and behaviors, as well as various graphical user interface components in Java Swing, such as JFrame, JButton, and JTextField. It details the constructors and functionalities of these components, along with layout managers and event listeners necessary for handling user interactions. Additionally, it explains how to set up components and associate them with event listeners to respond to user actions effectively.

Uploaded by

actualwizardd666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

Cs315 Advance Java Reviewer

The document outlines the key aspects of user interface design, including UI elements, layouts, and behaviors, as well as various graphical user interface components in Java Swing, such as JFrame, JButton, and JTextField. It details the constructors and functionalities of these components, along with layout managers and event listeners necessary for handling user interactions. Additionally, it explains how to set up components and associate them with event listeners to respond to user actions effectively.

Uploaded by

actualwizardd666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

*Every user interface considers the following three main aspects:

1. UI elements : These are the core visual elements the user eventually sees and interacts with.

2. Layouts: They define how UI elements should be organized on the screen and provide a final look and
feel to the GUI GraphicalUserInterface.

3. Behavior: These are events which occur when the user interacts with UI elements.

*Graphical user interface components:

1. Frames—Windows that can include a title bar; menu bar; and Maximize, Minimize, and Close buttons

The constructor of the class should handle the following tasks:

-Call a superclass constructor to give the frame a title and handle other setup procedures.

-Set the size of the frame’s window, either by specifying the width and height in pixels or by
letting Swing choose the right size.

-Decide what to do if a user closes the window.

-Display the frame.

*The JFrame class has two constructors: JFrame() and JFrame(String). One sets the

frame’s title bar to the specified text, and the other leaves the title bar empty. You also

can set the title by calling the frame’s setTitle(String) method.

*The size of a frame can be established by calling the setSize(int, int) method with the

width and height as arguments.

*Another way to set the size of a frame is to fill the frame with the components it will

contain and then call the frame’s pack() method. This resizes the frame based on the

size of the components inside it.

*Frames are invisible when they are created. You can make them visible by calling the

frame’s setVisible(boolean) method with the literal true as an argument.

*When a frame is displayed, the default behavior is for it to be positioned in the upper-left

corner of the computer’s desktop. You can specify a different location by calling the
setBounds(int, int, int, int) method. The first two arguments to this method are

the (x,y) position of the frame’s upper-left corner on the desktop. The last two arguments

set the width and height of the frame.

*To change this, you must call a frame’s setDefaultCloseOperation() method with one

of four static variables of the JFrame class as an argument:

-EXIT_ON_CLOSE—Exit the application when the frame is closed.

-DISPOSE_ON_CLOSE—Close the frame, remove the frame object from memory, and

keep running the application.

-DO_NOTHING_ON_CLOSE—Keep the frame open and continue running.

-HIDE_ON_CLOSE—Close the frame and continue running.

Component

A Container is the abstract base class for the non menu user-interface controls of SWING.

Component represents an object with graphical representation

Container - Interface elements that can hold other components

A Container is a component that can contain other SWING components.

JComponent

A JComponent is a base class for all swing UI components. In order to use a swing component that
inherits from JComponent, component must be in a containment hierarchy whose root is a top-level
Swing container.

2. JLabel

Labels—Text or graphics that provide information. A JLabel object is a component for placing text in a
container.

To create a label, you can use the following constructors:

JLabel(String)—A label with the specified text

JLabel(String, int)—A label with the specified text and alignment

JLabel(String, Icon, int)—A label with the specified text, icon, and alignment
3. JButton

Buttons - Clickable regions with text or graphics indicating their purpose. This class creates a labeled
button.

Constructors you can use for buttons include the following:

JButton(String)—A button labeled with the specified text

JButton(Icon)—A button that displays the specified graphical icon

JButton(String, Icon)—A button with the specified text and graphical icon

4. JTextField

Text fields and text areas—Windows that accept keyboard input and allow text to be edited

A text field is a location on an interface where a user can enter and modify text using the Keyboard

Constructors for text fields include the following:

JTextField()—An empty text field

JTextField(int)—A text field with the specified width

JTextField(String, int)—A text field with the specified text and width

Password fields are text fields that hide the characters a user is typing into the field. They

are represented by the JPasswordField class, a subclass of JTextField. The JPasswordField


constructor methods take the same arguments as those of its parent class.

After you have created a password field, call its setEchoChar(char) method to obscure input by
replacing each input character with the specified character.

5. JTextArea

Text areas, editable text fields that can handle more than one line of input

TextArea includes the following constructors:

JTextArea(int, int)—A text area with the specified number of rows and columns

JTextArea(String, int, int)—A text area with the specified text, rows, and columns

The setLineWrap(boolean) method determines whether text will wrap to the next line

when it reaches the right edge of the component. Call setLineWrap(true) to cause line

wrapping to occur.
The setWrapStyleWord(boolean) method determines what wraps to the next line—

either the current word (true) or the current character (false).

6. JScrollPane

Swing supports scrollbars through a new container that can be used to hold any compo-nent that can be
scrolled: JScrollPane.

A scrolling pane is associated with a component in the pane’s constructors:

JScrollPane(Component)—A scrolling pane that contains the specified component

JScrollPane(Component, int, int)—A scrolling pane with the specified component, vertical scrollbar
configuration, and horizontal scrollbar configuration

Scrollbars are configured using static class variables of the ScrollPaneConstants inter-face. You can use
each of the following for vertical scrollbars:

VERTICAL_SCROLLBAR_ALWAYS

VERTICAL_SCROLLBAR_AS_NEEDED

VERTICAL_SCROLLBAR_NEVER

7. JColorChooser

A JColorChooser provides a pane of controls designed to allow a user to manipulate and

select a color.

Check boxes and radio buttons—Small squares or circles that can be selected or deselected

Check boxes (the JCheckBox class) appear as labeled or unlabeled boxes that contain a check mark when
they are selected and nothing otherwise. Radio buttons (the JRadioButton class) appear as circles that
contain a dot when selected and are also empty otherwise.

8. JCheck Box

A JCheckBox is a graphical component that can be in either an on true or off false state.

The following constructors are available for the JCheckBox class:

JCheckBox(String)—A check box with the specified text label

JCheckBox(String, boolean)—A check box with the specified text label that is selected if the second
argument is true

JCheckBox(Icon)—A check box with the specified graphical icon


JCheckBox(Icon, boolean)—A check box with the specified graphical icon that is selected if the second
argument is true

JCheckBox(String, Icon)—A check box with the specified text label and graphical icon

JCheckBox(String, Icon, boolean)—A check box with the specified text label and graphical icon that is
selected if the third argument is true

9. JRadioButton

The JRadioButton class is a graphical component that can be in either an on true or off

false state in a group.

To organize several radio buttons into a group, allowing only one to be selected at a time,

create a ButtonGroup class object, as demonstrated in the following statement:

ButtonGroup choice = new ButtonGroup();

The ButtonGroup object keeps track of all radio buttons in its group. Call the group’s

add(Component) method to add the specified component to the group.

Drop-down lists—Groups of related items that can be selected from drop-down menus or scrolling windows

10. Combo Boxes

The Swing class JComboBox can be used to create combo boxes, components that present

a drop-down menu from which a single value can be selected. By hiding the menu when

the component is not being used, it takes up less space in a graphical user interface.

The following steps show how a combo box can be created:

1. The JComboBox() constructor is used with no arguments.

2. The combo box’s addItem(Object) method adds items to the list.

11. List

Lists can be created and filled with the contents of an array or a vector. The following

constructors are available:

JList()—Create an empty list.

JList(Object[])—Create a list that contains an array of the specified class (such as String).
JList(Vector)—Create a list that contains the specified java.util.Vector object.

Swing Layout

Layout means the arrangement of components within the container.

A layout manager determines how components will be arranged when they are added to a container.

1. BorderLayout

The borderlayout arranges the components to fit in the five regions: east, west, north, south and center.

2. CardLayout

The CardLayout object treats each component in the container as a card. Only one card is visible at a
time.

3. FlowLayout

The FlowLayout is the default layout. It layouts the components in a directional flow.

4. GridLayout

The GridLayout manages the components in form of a rectangular grid.

5. GridBagLayout

This is the most flexible layout manager class.The object of GridBagLayout aligns thecomponent
vertically, horizontally or along their baseline without requiring the components of same size.

6. GroupLayout

The GroupLayout hierarchically groups components in order to position them in a Container.

7. SpringLayout

A SpringLayout positions the children of its associated container according to a set of constraints.
Event Listeners

If a class wants to respond to a user event under the Java event-handling system, it must

implement the interface that deals with the events. These interfaces are called event listeners.

Each listener handles a specific kind of event.

The java.awt.event package contains all the basic event listeners, as well as the objects

that represent specific events. These listener interfaces are the most useful:

1. ActionListener—Action events, which are generated by a user taking an action on

a component, such as a click on a button

2. AdjustmentListener—Adjustment events, which are generated when a component

is adjusted, such as when a scrollbar is moved

3. FocusListener—Keyboard focus events, which are generated when a component

such as a text field gains or loses the focus

4. ItemListener—Item events, which are generated when an item such as a check

box is changed

5. KeyListener—Keyboard events, which occur when a user enters text on the key-board
6. MouseListener—Mouse events, which are generated by mouse clicks, a mouse

entering a component’s area, and a mouse leaving a component’s area

7. MouseMotionListener—Mouse movement events, which track all movement by a

mouse over a component

8. WindowListener—Window events, which are generated by a window being maxi-mized,


minimized, moved, or closed

Setting Up Components

When you make a class an event listener, you have set up a specific type of event to be

heard by that class. However, the event won’t actually be heard unless you follow up
with a second step: A matching listener must be added to the component. That listener

generates the events when the component is used.

After a component is created, you can call one of the following methods on the compo-nent to
associate a listener with it:

1. addActionListener()—JButton, JCheckBox, JComboBox, JTextField,

JRadioButton, and JMenuItem components

2. addFocusListener()—All Swing components


3. addItemListener()—JButton, JCheckBox, JComboBox, and JRadioButton compo-nents
4. addKeyListener()—All Swing components
5. addMouseListener()—All Swing components
6. addMouseMotionListener()—All Swing components
7. addTextListener()—JTextField and JTextArea components
8. addWindowListener()—JWindow and JFrame components

The ActionListener interface has only one method: actionPerformed(). All classes

that implement ActionListener must have a method with the following structure:

public void actionPerformed(ActionEvent event) {

// handle event here

ActionEvent and all other event objects are part of the java.awt.event package and

subclasses of the EventObject class.

Every event-handling method is sent an event object of some kind. The object’s

getSource() method can be used to determine the component that sent the event, as in

the following example:

public void actionPerformed(ActionEvent event) {

Object source = evt.getSource();

The object returned by the getSource() method can be compared with components by

using the == operator. The following statements can be used within the body of an
actionPerformed() method to handle user clicks on buttons named quitButton and

sortRecords:

if (source == quitButton) {

quitProgram();

if (source == sortRecords) {

sortRecords();

You might also like