Docsity

Java Graphical User Interface Elements: A Comprehensive Guide, Slides of Java Programming

Toggle sidebar
bg1
Some Graphical User Interface Elements in Java
CalinCur escu
Some Graphical User Interface Elements in
Java
Calin Curescu
20 pages
TDDC32 lecture 2, 2006
Reusing old TDDB32 slides by Peter Loborg, Tommy Olsson
Some Graphical User Interface Elements in Java
CalinCur escu
2of 20
TDDC32 lecture 2, 2006
Graphical User Interface
•Ready made dialog windows:
•Or
•… and many more
import javax.swing.*;
class GuiDiemo {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Hi folks!");
}
}
int option = JOptionPane.showConfir mDialog(null, "Maybe...",
"Is this acceptable?", JOptionPane.YES_NO_CANCEL_OPTION);
Some Graphical User Interface Elements in Java
CalinCur escu
3of 20
TDDC32 lecture 2, 2006
Drawing the main components
1. Using a new class for the “GUI Window”
2. Visual entities are held by containers (frames,panels)
3. Placement in a container is organised by a layout manager
4. So let’s create some objects and place them in the main
container:
1. One JLabel layout: BorderLayout
2. One JPanel
5. And in the Jpanel
1. Two JButton layout: FlowLayout
6. Let the main frame calculate its borders
7. And let’s show the result…
Some Graphical User Interface Elements in Java
CalinCur escu
4of 20
TDDC32 lecture 2, 2006
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class WidgetDemo {
public static void main(String[] args) {
JFrame window = new JFrame("Easy...");
Container content = window.getContentPane();
content.setLayout(new BorderLayout());
JLabel label = new JLabel("...isn't it?");
content.add(label, "Center");
JPanel content2 = new JPanel();
content2.setLayout(new FlowLayout());
content2.add(new JButton("..."));
content2.add(new JButton("...no way!"));
content.add(content2, "South");
window.pack();
window.show();
}
}
Example
•But nothing happens when I click!
Some Graphical User Interface Elements in Java
CalinCur escu
5of 20
TDDC32 lecture 2, 2006
Events
•Events are generated by actions
– ActionEvent – most common
• A button click, a menu choice, an enter in a text field
– Caret Event
• When cursor moves, when text is selected
– ChangeEvent
• When the state of an object has changed
–Etc…
•In order to be notified a receiver for these events must be
created
– should be registered with the event source
– listens for that type of events – “event listener”
Some Graphical User Interface Elements in Java
CalinCur escu
6of 20
TDDC32 lecture 2, 2006
Event Listener Example
•Inner Class
•Anonymous Inner Class
• Useful if the class is used only once
class MyListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.println(e);
}
}
...
guiObj.addActionListener(new MyListener());
guiObj.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.println(e);
}
}); //<== Observe the paranteses
pf3
pf4

Partial preview of the text

Download Java Graphical User Interface Elements: A Comprehensive Guide and more Slides Java Programming in PDF only on Docsity!

Some Graphical User Interface Elements in JavaCalin Curescu

Some Graphical User Interface Elements in

Java

Calin Curescu

TDDC32 lecture 2, 200620 pages

Reusing old TDDB32 slides by Peter Loborg, Tommy Olsson

Some Graphical User Interface Elements in JavaCalin Curescu TDDC32 lecture 2, 20062 of 20

Graphical User Interface

  • Ready made dialog windows:
  • Or
  • … and many more

import javax.swing.*; class GuiDiemo { public static void main(String[] args) { JOptionPane.showMessageDialog(null, "Hi folks!"); } }

int option = JOptionPane.showConfirmDialog(null, "Maybe...", "Is this acceptable?", JOptionPane.YES_NO_CANCEL_OPTION);

Some Graphical User Interface Elements in JavaCalin Curescu TDDC32 lecture 2, 20063 of 20

Drawing the main components

  1. Using a new class for the “GUI Window”
  2. Visual entities are held by containers (frames,panels)
  3. Placement in a container is organised by a layout manager
  4. So let’s create some objects and place them in the main container: 1. One JLabel layout: BorderLayout 2. One JPanel
  5. And in the Jpanel
    1. Two JButton layout: FlowLayout
  6. Let the main frame calculate its borders
  7. And let’s show the result…

Some Graphical User Interface Elements in JavaCalin Curescu TDDC32 lecture 2, 20064 of 20

import java.awt.; import java.awt.event.; import javax.swing.*; public class WidgetDemo { public static void main(String[] args) { JFrame window = new JFrame("Easy..."); Container content = window.getContentPane(); content.setLayout(new BorderLayout()); JLabel label = new JLabel("...isn't it?"); content.add(label, "Center"); JPanel content2 = new JPanel(); content2.setLayout(new FlowLayout()); content2.add(new JButton("...")); content2.add(new JButton("...no way!")); content.add(content2, "South"); window.pack(); window.show(); } }

Example

  • But nothing happens when I click!

Events

  • E vents are generated by actions
    • ActionEvent – most common
      • A button click, a menu choice, an enter in a text field
    • Caret Event
      • When cursor moves, when text is selected
    • ChangeEvent
      • When the state of an object has changed
    • Etc…
  • In order to be notified a receiver for these events must be created - should be registered with the event source - listens for that type of events – “event listener”

Event Listener Example

  • Inner Class
  • Anonymous Inner Class
    • Useful if the class is used only once

class MyListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.println(e); } } ... guiObj.addActionListener(new MyListener());

guiObj.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.println(e); } }); //<== Observe the paranteses

Some Graphical User Interface Elements in JavaCalin Curescu TDDC32 lecture 2, 20067 of 20

Events and Threads

  • All events are sent by ONE special thread
    • Which treats graphic events
    • Executes them sequentially
    • Thus, while it executes in a listener, all the GUI is frozen!
  • Listener actions that take time should execute in a new thread
  • Background activities must be executed by another thread
    • I.e. the main thread

Some Graphical User Interface Elements in JavaCalin Curescu TDDC32 lecture 2, 20068 of 20

Package java.awt

  • A part of AWT hierarchy is missing
    • Box.Filler, Checkbox, Choice, Label, List
  • Classes with thick frame belong to SWING

Some Graphical User Interface Elements in JavaCalin Curescu TDDC32 lecture 2, 20069 of 20

Package jawax.swing

  • Lighter components with more functionality
    • Compared to AWT
    • Container belongs to AWT

Some Graphical User Interface Elements in JavaCalin Curescu TDDC32 lecture 2, 200610 of 20

Component, JComponent

  • Abstract classes for objects with graphic representation
    • Handles its own drawing
      • paint(g)
      • repaint()
    • Interacts with user by events
    • Lots of methods such as:
      • setForeground(color),
      • setBackground(color)
      • setFont(font)
      • setVisible(true/false),
      • isVisible()
      • setSize(x, y),
      • getSize()

Container, JContainer

  • Abstract class for components that contain other components
    • a LayoutManager is used to place the inner components
    • setLayout(layout_manager), getLayout()
  • Components can be added and removed
    • add(component)
    • remove(component), removeAll()
  • Many other methods

JButton

  • Buttons that can be clicked - sends ActionEvent
  • Many methods for colouring, textstiles, pictures, etc
    • E.g. b.setToolTipText(“Are you hovering?”)

Some Graphical User Interface Elements in JavaCalin Curescu TDDC32 lecture 2, 200619 of 20

Menus

  • JMenuBar to create a menulist in a window
  • JMenu to create a menu
  • JMenuItem to create menu options
  • Note usage of: w.setJMenubar() as opposed to w.getContentPane()

Some Graphical User Interface Elements in JavaCalin Curescu TDDC32 lecture 2, 200620 of 20

Layout Manager

Some Graphical User Interface Elements in JavaCalin Curescu TDDC32 lecture 2, 200621 of 20

Additional Reading

  • The Java Tutorial
    • Trail: Creating a GUI with JFC/Swing
    • http://java.sun.com/docs/books/tutorial/uiswing/index.html