GUI Programming with Java
-
Upload
jussi-pohjolainen -
Category
Education
-
view
597 -
download
1
Transcript of GUI Programming with Java
![Page 1: GUI Programming with Java](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/1.jpg)
GUI – Programming with Java
![Page 2: GUI Programming with Java](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/2.jpg)
JFRAME
![Page 3: GUI Programming with Java](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/3.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/5.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/6.jpg)
GUI
![Page 7: GUI Programming with Java](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/10.jpg)
FlowLayout
![Page 11: GUI Programming with Java](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/11.jpg)
GridLayout
![Page 12: GUI Programming with Java](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/12.jpg)
BorderLayout
• Five cells
– north
– south
– east
– west
– center
![Page 13: GUI Programming with Java](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/15.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/17.jpg)
COMPONENTS
![Page 18: GUI Programming with Java](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/21.jpg)
EVENT HANDLING
![Page 22: GUI Programming with Java](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/22.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/23.jpg)
Separation of GUI and BL
Source ListenerRegistration
![Page 24: GUI Programming with Java](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/24.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/25.jpg)
Listener
• Any class that can handle the events
• => Any class that implements some interface
![Page 26: GUI Programming with Java](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/26.jpg)
Separation of GUI and BL
Source ListenerRegistration
![Page 27: GUI Programming with Java](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/27.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/28.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/29.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/30.jpg)
Separation of GUI and BL
JButton ListenerRegistration
![Page 31: GUI Programming with Java](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/31.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/32.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/33.jpg)
Listener
• Some class that implements the ActionListener
class MyListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// do something
}
}
![Page 34: GUI Programming with Java](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/34.jpg)
Separation of GUI and BL
JButton MyListeneraddActionListener
ActionListener
![Page 35: GUI Programming with Java](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/35.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/36.jpg)
Registration
• Different sources have different methods and interfaces for the registration.
• Registration:
– addXXXListener
• Examples
– addMouseMotionListener(...)
– addMouseListener(...)
– addKeyListener(...)
![Page 37: GUI Programming with Java](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/37.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/38.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/39.jpg)
MENUS
![Page 40: GUI Programming with Java](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/40.jpg)
MenuBar
• JFrame can contain MenuBar
• Needed classes
– JMenuBar
– JMenu
– JMenuItem
• Event handling the same as in JButton (addActionListener)
![Page 41: GUI Programming with Java](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/41.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/42.jpg)
DIALOGS
![Page 43: GUI Programming with Java](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/43.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/44.jpg)
Standard Dialogs
• Really easy way to create dialogs is use standard dialogs
• JOptionPane
• JFileChooser
• JColorChooser
• ...
![Page 45: GUI Programming with Java](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/45.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022042514/55a9404c1a28ab205d8b457a/html5/thumbnails/46.jpg)
JFileChooser
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION)
String file =
chooser.getSelectedFile().getName();