46
GUI Programming with Java

GUI Programming with Java

Embed Size (px)

Citation preview

Page 1: GUI Programming with Java

GUI – Programming with Java

Page 2: GUI Programming with Java

JFRAME

Page 3: GUI Programming with Java

Creating Windows

• Windows

– javax.swing.JFrame – Empty window

• By default, JFrame is not visible

– setVisible(true)!

• Java Look And Feel (Metal) by default

Page 4: GUI Programming with Java

Creating JFrame

import javax.swing.JFrame;

class Testing {

public static void main(String [] args) {

JFrame mywin = new JFrame();

mywin.setVisible(true);

}

}

Page 5: GUI Programming with Java

Inheritance

import javax.swing.JFrame;

class MyJFrame extends JFrame {

public MyJFrame() {

setTitle("My Window!");

}

}

class Testing {

public static void main(String [] args) {

MyJFrame mywin = new MyJFrame();

mywin.setVisible(true);

}

}

Page 6: GUI Programming with Java

GUI

Page 7: GUI Programming with Java

AWT vs. Swing

• AWT = Abstract Window Toolkit– java.awt.*;

– Old class library for Java

– Uses native GUI - components

• Swing– javax.swing.*;

– From Java 1.2 ->

– Extends AWT

– GUI – components 100% Java

– Pluggable look and feel

Page 8: GUI Programming with Java

Layouts

• UI is build on top of layouts• Layout determinates where UI-elements are

layed.• You can add a layout to Jframe• Layouts

– FlowLayout– GridLayout– BorderLayout– GridBagLAyout– CardLayout

Page 9: GUI Programming with Java

Layout and Components

import javax.swing.*;

import java.awt.*;

class MyJFrame extends JFrame {

public MyJFrame() {

setTitle("My Window!");

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

FlowLayout layout = new FlowLayout();

setLayout(layout);

add(clickMe);

}

}

class Testing {

public static void main(String [] args) {

MyJFrame mywin = new MyJFrame();

mywin.setVisible(true);

}

}

Page 10: GUI Programming with Java

FlowLayout

Page 11: GUI Programming with Java

GridLayout

Page 12: GUI Programming with Java

BorderLayout

• Five cells

– north

– south

– east

– west

– center

Page 13: GUI Programming with Java

Other layouts

BoxLayout- Components in row or column

CardLayout-Window holds "cards", which can hold other components

GridBagLayout- Lot of possibilities, hard to use

Page 14: GUI Programming with Java

JPanel

• It's possible to combine layouts using JPanel

• JPanel can have it's own layout. JPanel may hold other components

• JFrame can hold JPanels, that hold components..

Page 15: GUI Programming with Java

Combining LayoutsJPanel left = new JPanel();

left.setLayout(new GridLayout(2,1));

JPanel right = new JPanel();

right.setLayout(new GridLayout(3,1));

left right

MyJFrame

setLayout(new Gridlayout(1,2));

add(left);

add(right);

Page 16: GUI Programming with Java

Combining Layouts

public MyJFrame(){

setLayout(new GridLayout(1,2));

JPanel left = new JPanel();

left.setLayout(new GridLayout(2,1));

left.add(new JButton("1"));

left.add(new JButton("2"));

JPanel right = new JPanel();

right.setLayout(new GridLayout(3,1));

right.add(new JButton("3"));

right.add(new JButton("4"));

right.add(new JButton("5"));

add(left);

add(right);

}

Page 17: GUI Programming with Java

COMPONENTS

Page 18: GUI Programming with Java

Components

• UI – components can be added to window (JFrame) using the add() – method

• All components derive from JComponent• UI – components?

– JButton– JLabel– JMenuItem– JTable– JTextArea– JTextField...

Page 19: GUI Programming with Java

JComponent

• Some component's common methods

– setVisible(boolean)

– setFont(Font)

– setEnabled(boolean)

– ... See JComponent API

• http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html

Page 20: GUI Programming with Java

Components

• JButton

– JButton mybutton = new JButton("Click!");

• JLabel

– JLabel mylabel = new JLabel("Some text");

• JTextField

– JTextField myfield = new JTextField();

– String text = myfield.getText();

• JTextArea...

Page 21: GUI Programming with Java

EVENT HANDLING

Page 22: GUI Programming with Java

Delegation Event Handling

• Delegation Event Model:– Simple and easy to learn

– Support a clean separation between application and GUI code

– Facilitate the creation of robust event handling code which is less error-prone (strong compile-time checking)

– Flexible enough to enable varied application models for event flow and propagation

– For visual tool builders, enable run-time discovery of both events that a component generates as well as the events it may observe

– Support backward binary compatibility with the old model

Page 23: GUI Programming with Java

Separation of GUI and BL

Source ListenerRegistration

Page 24: GUI Programming with Java

Concepts: Event Source

• Event source is usually some component that can raise events

• Examples of event source

– JButton (button is clicked)

– JMenuItem

– JTextField

Page 25: GUI Programming with Java

Listener

• Any class that can handle the events

• => Any class that implements some interface

Page 26: GUI Programming with Java

Separation of GUI and BL

Source ListenerRegistration

Page 27: GUI Programming with Java

Recap on Polymorphism

interface AbleToMove {

public void start();

public void stop();

}

class Car implements AbleToMove {

public void start() {

// do something

}

public void stop() {

// do something

}

}

Page 28: GUI Programming with Java

Recap on Polymorphism

class Test {

public static void main(String [] args) {

Airplane a = new Airplane();

someMethod(a);

}

public static void someMethod(AbleToMove x) {

x.start();

}

} You can pass whateverobject you desire as long asthe object implements AbleToMoveinterface!

Page 29: GUI Programming with Java

Example

• JButton is a event source.

– JButton source = new JButton();

• When the button is clicked we want that something happens

• We need a class that listens to the button.

• We register the source and the listener with each other!

Page 30: GUI Programming with Java

Separation of GUI and BL

JButton ListenerRegistration

Page 31: GUI Programming with Java

Registration

• JButton holds a method addActionListener– public void addActionListener(ActionListener l)

• So you can call it like– JButton source = new JButton("Click!");

– source.addActionListener(...);

• Parameter ActionListener? What is it?

• It's an interface!

• This means that you can pass whatever object as long as the object implements the interface!

Page 32: GUI Programming with Java

ActionListener

• So the listener can be what ever object as long as it implements the ActionListener.

• ActionListener:interface ActionListener {

public void actionPerformed(ActionEvent e);

}

Page 33: GUI Programming with Java

Listener

• Some class that implements the ActionListener

class MyListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

// do something

}

}

Page 34: GUI Programming with Java

Separation of GUI and BL

JButton MyListeneraddActionListener

ActionListener

Page 35: GUI Programming with Java

Example Usage

// Create the source

JButton button = new JButton("click me");

// Create the listener

MyListener listener = new MyListener();

// Registration

button.addActionListener(listener);

Page 36: GUI Programming with Java

Registration

• Different sources have different methods and interfaces for the registration.

• Registration:

– addXXXListener

• Examples

– addMouseMotionListener(...)

– addMouseListener(...)

– addKeyListener(...)

Page 37: GUI Programming with Java

Example: Listening to Mouse in Window

• // Source

• JFrame a = new JFrame();

• // Listener

• SomeClass listener = new SomeClass();

• // Registration

• a.addMouseMotionListener(listener);

Page 38: GUI Programming with Java

BL and GUI in the same class

• In small programs it is usual that the GUI - and the application code is implemented in the same class.

• No the listener is the same class:

– class MyJFrame extends JFrame implements

ActionListener

• And registration:

– source.addActionListener(this);

Page 39: GUI Programming with Java

MENUS

Page 40: GUI Programming with Java

MenuBar

• JFrame can contain MenuBar

• Needed classes

– JMenuBar

– JMenu

– JMenuItem

• Event handling the same as in JButton (addActionListener)

Page 41: GUI Programming with Java

Creating MenuBar

edit.add(pref);

menubar.add(edit);

setJMenuBar(menubar);

JMenuBar menubar = new JMenuBar();

JMenu edit = new JMenu("Edit");

JMenuItem pref = new JMenu("Preferen..

Page 42: GUI Programming with Java

DIALOGS

Page 43: GUI Programming with Java

About Dialogs

• JFrame is used for windows, JDialog for dialogs.

– You can inherit the JDialog.

• Every dialog needs to know who the host window is

– dialog belongs to the window

Page 44: GUI Programming with Java

Standard Dialogs

• Really easy way to create dialogs is use standard dialogs

• JOptionPane

• JFileChooser

• JColorChooser

• ...

Page 45: GUI Programming with Java

JOptionPane

• With one line of code, you can pop up a dialog

– JOptionPane.showMessageDialog(hostframe, "Hello!");

• Also available

– showConfirmDialog

• yes/no/cancel

– showInputDialog

• prompt user

Page 46: GUI Programming with Java

JFileChooser

JFileChooser chooser = new JFileChooser();

int returnVal = chooser.showOpenDialog(parent);

if(returnVal == JFileChooser.APPROVE_OPTION)

String file =

chooser.getSelectedFile().getName();