0% found this document useful (0 votes)
30 views

Swing: CS-328 Dick Steflik John Margulies

Swing is a Java GUI widget toolkit that improves upon AWT. It uses lightweight components drawn with Java code rather than relying on the operating system's peer components. This makes Swing more portable and able to lay out components consistently across platforms. The key aspects of building a Swing GUI are importing packages, creating a class to hold GUI components and functions, adding components to a JFrame's content pane, and using layout managers to position components. Common Swing components include JPanels, JButtons, JTextFields, JLists, JTables and JTextAreas.

Uploaded by

daom
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Swing: CS-328 Dick Steflik John Margulies

Swing is a Java GUI widget toolkit that improves upon AWT. It uses lightweight components drawn with Java code rather than relying on the operating system's peer components. This makes Swing more portable and able to lay out components consistently across platforms. The key aspects of building a Swing GUI are importing packages, creating a class to hold GUI components and functions, adding components to a JFrame's content pane, and using layout managers to position components. Common Swing components include JPanels, JButtons, JTextFields, JLists, JTables and JTextAreas.

Uploaded by

daom
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

Swing

CS-328 Dick Steflik John Margulies

Swing vs AWT
AWT is Javas original set of classes for building GUIs
Uses peer components of the OS; heavyweight Not truly portable: looks different and lays out inconsistently on different OSs
Due to OSs underlying display management system

Swing is designed to solve AWTs problems


99% java; lightweight components
Drawing of components is done in java Uses 4 of AWTs components Window, frame, dialog, ?

Lays out consistently on all OSs Uses AWT event handling

Implementing a Swing GUI


Import javax.swing.*, java.io.*, java.awt.* Make a specific class to do GUI functions Specify all the GUI functions/components in the classs constructor (or methods / classes called by the constructor) Run the GUI by instantiating the class in the classs main method

Implementing a Swing GUI

JFrame
Frames are the basis of any Java GUI Frame is the actual window that encompasses your GUI objects; a GUI can have multiple frames The J prefix is at the beginning of any Swing components name (to distinguish them from AWT components) JFrame is a wrapper around AWTs Frame

JFrame - Code

Frame/Pane

Panes/JPanels
The terms pane and panel are used interchangeably in Java If a frame is a window, a pane is the glass Panes hold a windows GUI components Every frame has at least one pane, the default Content Pane

Panes
Useful for layout
If you want to group certain GUI components together, put them inside a pane, then add that pane to the frame

Needed to add components to the frame


Nothing can be added directly to the frame; instead, everything, including other panes, is added to the frames content pane

Content Pane
When a frame is created, the content pane is created with it To add a component to the content pane (and thus to the frame), use:
frameName.getContentPane().add(comp onent name);

where frameName is the name of the frame

Text Areas
Specified by Javas JTextarea class Multiple constructors allow you to create a new text area with a specified size and/or specified text A text area is just a white space of variable size that can hold text If text goes out of the areas bounds, it will exist but some of it will not be seen
Wrap the text area in a scrollable pane

Text Areas

JTextarea Methods
textarea.setText(String); textarea.getText(String); textarea.append(String); textarea.setEditable(boolean);

JScrollPane
Similar to a regular pane, only, when necessary, a scrollbar appears to allow scrolling through the panes contents Particularly useful for embedding tables and text areas, as these tend to contain more content than they can show at one time

JScrollPane
Default constructor (JScrollPane()) creates a scrollable pane that you can add components to Alternatively, you can initialize a pane to wrap itself around a component
JScrollPane newPane = new JScrollPane(JTextArea area);

JScrollPane

JTextField
A Java text field is essentially the same as a text area, only limited to one line Very similar set of methods JPasswordField is the same as JTextField, only the contents are hidden Different constructors allow you to predefine the number of columns and/or the default text

JButton
Java class that allows you to define a button Multiple constructors allow you to initialize a button with a predefined label and/or a predefined icon Although the buttons action can be defined in the constructor, defining a buttons action can take many lines of code and should be done separately

Defining a JButton
JButton button = new JButton(Press Me!); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { /* insert action here */ } });
/* setting an action requires that you import java.awt.event.* */

Model-View-Controller
Design pattern often used in Swing objects Breaks a GUI object down into three parts
Model manages the data used by the object View manages the graphical/textual output of the object Controller interprets user input, commanding the model and view to change as necessary

Model-View-Controller
Swing components that use the MVC pattern, such as JList and JTable, generally have one class that controls both the view and the controller and a separate class that controls the model

Model-View-Controller
Programmer instantiates a model (e.g., the DefaultTableModel class), then loads that model with the data to be displayed in the GUI The view/controller class (e.g., the JTable class) is then instantiated from the model
If the programmer instantiates the GUI object without a model, the view/controller class creates an empty model to work from
JTable table = new JTable(DefaultTableModel model);

JList
A simple GUI object design to hold lists of objects and allow users to make selections from the list Can be created from a ListModel, a Vector, or an array (all essentially lists themselves)

JTable
Usually created from a DefaultTableModel
Can also be created from an array of arrays or a Vector of Vectors, or can have no initial data

Create a DefaultTableModel, then initialize a table from the DefaultTableModel

When you add items to your frame


Text area is added first, then text field, then button

Layout Managers
Every pane has a layout manager Layout managers tell Java where to put components when you add them to a pane The default layout manager is FlowLayout, which lays out components from left to right until there is no room left on a line, then starts the next line Lays out components in the order they are added Layouts can be nested, one inside of another making them quite versatile

Other Layout Managers


BorderLayout
Defines five regions: North, South, East, West, and Center Programmer specifies which objects go to which regions

GridLayout
Programmer defines matrix dimensions; objects are then put in the matrix in the order they are added, left to right, top to bottom

BoxLayout
BoxLayout is a simple way to come close to absolute positioning (which
isnt recommended)
Panes can be laid out either top to bottom or left to right Panes laid out with BoxLayout can be put in other BoxLayout panes, creating a grid of completely variable size and a very controlled layout

BoxLayout

BoxLayout
Pane.setLayout(new BoxLayout(Pane, BoxLayout.Y_AXIS)); where Pane is the name of the pane you are laying out

Events and Event Handling


Components (AWT and Swing) generate events in response to user actions
(button clicks, mouse movement, item selection) different components generate different events Buttons generate action events Cursor movement generates mouse events ect. The program must provide event handlers to catch and process events Unprocessed events are passed up through the event hierarchy and handled by a default (do nothing) handler

For the entire Java API specification, including all the Swing APIs, go to
http://java.sun.com/j2se/1.4/docs/api/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy