Docsity

Programing in JAva-Java Network Programming-Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Programing in JAva-Java Network Programming-Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Introduction to JFC Swing

Agenda

  • About JFC and Swing
  • Pluggable Look and Feel
  • Swing Components
  • Borders
  • Layout Management
  • Events Handling

About JFC and Swing (cont)

  • Swing features:
    • The Swing Components
      • Dialog, Tabbed pane, Buttons, File Chooser, ...
    • Pluggable Look and Feel
    • Accessibility API
      • Screen readers, Braille displays, ...
    • Java 2DTM^ API (Java 2 Platform only)
    • Drag and Drop (Java 2 Platform only)
      • Between Java applications and native applications.

Pluggable Look and Feel

Each picture shows the same program but with a different look and feel

Containers

  • Descendents of the java.awt.Container class
  • Components that can contain other components.
  • Use a layout manager to position and size the components contained in them.
  • Components are added to a container using one of the various forms of its add method - Depending on which layout manager is used by the container panel.add(component);

Top Level Containers

  • Every program that presents a Swing GUI contains at least one top-level container.
  • A Top level container provides the support that Swing components need to perform their painting and event-handling.
  • Swing provides three top-level containers:
    • JFrame (Main window)
    • JDialog (Secondary window)
    • JApplet (An applet display area within a browser window)

JFrame

  • A frame implemented as an instance of the

JFrame class, is a window that has

decorations such as a border, a title and

buttons for closing and iconifying the

window.

  • The decorations on a frame are platform dependent.
  • Applications with a GUI typically use at

least one frame.

Example 1

import javax.swing.;*

public class HelloWorldSwing {

public static void main(String[] args) { JFrame frame = new JFrame("HelloWorldSwing"); final JLabel label = new JLabel("Hello World"); frame.getContentPane().add(label); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }

pack() causes a window to be sized to fit the preferred size and layouts of its sub-components

JDialog

  • Every dialog is dependent on a frame
    • Destroying a frame destroys all its dependent dialogs.
    • When the frame is iconified, its dependent dialogs disappear from the screen.
    • When the frame is deiconified, its dependent dialogs return to the screen.
  • A dialog can be modal. When a modal dialog is visible it blocks user input to all other windows in the program.

JDialog (cont)

  • To create custom dialogs, use the JDialog

class directly (as in the previous examples).

  • Swing provides several standard dialogs
    • JProgressBar, JFileChooser, JColorChooser, ...
  • The JOptionPane class can be used to create

simple modal dialogs

  • icons, title, text and buttons can be customized.

JComponent

  • JComponent is the base class for all Swing

components except top-level containers.

  • JLabel, JButton, JList, JPanel, JTable, ...
  • To use a component that inherits from

JComponent, it must be placed in a

containment hierarchy who’s base is a top-

level container.

JComponent (cont)

  • The JComponent class provides the following (partial list): - Pluggable Look & Feel - Keystroke handling - Tooltip support - Accessibility - An infrastructure for painting - Support for borders.
  • All descendents of JComponent are also Containers - A JButton can contain text, icons etc.

Simple Borders

Titled Borders