Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation.

Similar presentations

Presentation on theme: "Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation."— Presentation transcript:

1 Creating a GUI with Swing

2 Introduction Very useful link: 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).

3 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)  …

4 Swing Components (examples) Basic components: Containers:

5 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

6 6 Layouts to set a layout of a container: void setLayout(LayoutManager lm)

7 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); }

8 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

9 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); }

10 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

11 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

Download ppt "Creating a GUI with Swing. Introduction Very useful link: Swing – is a part of JFC (Java Foundation."

Similar presentations

Ads by Google