Embed
Email

Inheritance

Document Sample

Shared by: xiuliliaofz
Categories
Tags
Stats
views:
0
posted:
11/5/2011
language:
English
pages:
11
Creating a GUI

with Swing

Introduction

• Very useful link:

http://java.sun.com/docs/books/tutorial/uiswing/index.html



• Swing

– is a part of JFC (Java Foundation Classes)

 JFC is a group of features for building graphical

user interfaces (GUIs).

– is a set of widgets for Java

 widget is an element of GUI (aka: component)

– is a part of the Java's standard library

– intended to replace corresponding components of

AWT (Abstract Window Toolkit).

Swing Components

• Containers

to contain and to manage other components



 Top-Level Containers

(JFrame, JWindow, JApplet and JDialog)

 General-Purpose Containers

(JPanel, JScrollPane, JTabbedPane and so on)

 Special-Purpose Containers



• Basic Components

for interaction with a user



 Uneditable Information Displays

(JLabel, JToolTip and so on)

 Basic Controls

(JButton, JCheckBox, JTextField and so on)

…

Swing Components (examples)

Containers:









Basic components:

JFrame

• Made from several layers (Special-Purpose Containers)









• Components are added to the Content Pane layer.



• The order of the components is determined according to

the layout manager

Layouts









to set a layout of a container:

void setLayout(LayoutManager lm)



6

Swing Example

import javax.swing.*;

import java.awt.BorderLayout;



public class Main {

public static void main(String[] args) {

JFrame f = new JFrame("My First Frame");

JPanel p = new JPanel(new BorderLayout());

f.add(p);



JLabel l = new JLabel("My First Label");

p.add(l, BorderLayout.NORTH);

f.pack();

f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

Events and Listeners

Event

• just object

• stores information about a type of event, its source etc.

• is created on certain actions in an application



Listener

• event handler

• is an object that implements a listener interface

• when an event occurs, all registered listeners are notified

 the appropriate listener method is invoked

 an object describing the event is passed as a

parameter

• to react to an event (on a certain component) a listener

object should be registered with that component

Listeners Example

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;



public class Events {

public static void main(String[] args){

JFrame f = new JFrame();

f.getContentPane().setLayout(new FlowLayout());

JButton b = new JButton("Click me!");

b.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JOptionPane.showMessageDialog(null, "Thank you");

}

});

f.getContentPane().add(b);

f.pack();

f.setVisible(true);

}

}

MVC









• Model

represents the data for the application

• View

the visual representation of that data

• Controller

takes user input on the view and translates that to changes in

the model

JTable

• Separation of the table view from the table model

• JTable – view

• TableModel – model



• DefaultTableModel – a default implementation of

TableModel

• DefaultTableModel can be customized by

subclassing

• TableModel can be implemented from scratch by

subclassing



• TableModelListener – a listener interface

• Notifies model of changes in the view



Related docs
Other docs by xiuliliaofz
Dreaming
Views: 2  |  Downloads: 0
Maurice White BDSc Melb
Views: 0  |  Downloads: 0
article-7901
Views: 0  |  Downloads: 0
Application - City of Laramie
Views: 0  |  Downloads: 0
Project Outline - TeacherWeb
Views: 0  |  Downloads: 0
NSSE EDUCATION
Views: 0  |  Downloads: 0
me344_f03
Views: 0  |  Downloads: 0
Experiment_11a
Views: 0  |  Downloads: 0
CHAPTER 16
Views: 0  |  Downloads: 0
Distributed Data Base Systems
Views: 3  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!