Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 15 Basic GUI programming

Similar presentations


Presentation on theme: "Lecture 15 Basic GUI programming"— Presentation transcript:

1 Lecture 15 Basic GUI programming
D&D 12 Date

2 Goals By the end of this lesson, you should:
Be able to write Java applications using input and message dialogs Be able to write Java applications using JFrame windows, JLabels and JButtons Be able to handle button click events and understand the mechanism behind ActionEvent and ActionListeners

3 Input and message dialogs
Java Swing components A simple GUI example Events Notes Summary import javax.swing.JOptionPane; public class PopUpDialogs { public static void main(String[] args) { String name = JOptionPane.showInputDialog("Please enter your name"); JOptionPane.showMessageDialog(null, "Hello " + name + "!\nNow I know who you are!", "Greeting", JOptionPane.INFORMATION_MESSAGE); } When it comes to GUI (graphical user interface) programming, Java’s standard library are the Swing components, with a bit of support from awt, which we’ve already seen. Pop-up dialogs like the input dialog and message dialog above are launched via a class method of the javax.swing.JOptionPane class. Note that the input dialog blocks execution of the method until the user has entered a name.

4 More complex GUIs Input and message dialogs Java Swing components A simple GUI example Events Notes Summary We’ve already dealt with a GUI – remember the stick figures from Lecture 10? Remember how this was based on a JFrame that contained a JPanel with the stick figures? Most Swing applications also use a JFrame to provide the window in which the application runs. Typically, we will extend the JFrame class for this purpose.

5 Swing Components Things such as buttons, checkboxes, text fields, lists, images, menus, etc. that we find in GUIs are known as components. The Java Swing classes know two types of components: Heavyweight components: Like AWT components, they use operating system specific code in the background and are only partially written in Java. Lightweight components: Written entirely in Java. Most Swing components are lightweight. The only heavyweight Swing component we have met so far has been the JFrame. Some basic Swing components have counterparts in AWT, e.g., a JButton is the Swing version of a Button. Swing has more components than AWT. Input and message dialogs Java Swing components A simple GUI example Events Notes Summary

6 Lightweight Swing Components and containers
Input and message dialogs Java Swing components A simple GUI example Events Notes Summary In Swing, each lightweight component is an instance of a subclass of JComponent. This class adds tooltips, accessibility, keyboard shortcuts etc. Each JComponent is a Container, and each container is a Component, which in turn is a subclass of the Object class, the common ancestor of all Java objects. A Container is a Component that we can attach other Components to (e.g., attaching a JButton to a JFrame). We do so with its add()method. A Component in turn implements all the common features of awt and Swing components. Object java.awt.Component java.awt.Container javax.swing.JComponent javax.swing.AbstractButton javax.swing.JButton

7 The HelloWorld application
Input and message dialogs Java Swing components A simple GUI example Events Notes Summary This “HelloWorld” application is a Swing application that simply consists of a window with a label and a button. When you click on the button, the label changes.

8 Starting a Swing application
import javax.swing.JFrame; import javax.swing.SwingUtilities; public class RunHelloWorld implements Runnable { public void run() { HelloWorld h = new HelloWorld(); h.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); h.setSize(300,150); h.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new RunHelloWorld()); Input and message dialogs Java Swing components A simple GUI example Events Notes Summary Need this import only because of the JFrame.EXIT_ON_CLOSE Runnable method, will start on the event-dispatching thread Swing is not thread-safe (we’ll find out later what that means exactly). As many components in Swing work with events, we should run them in the thread that dispatches the events. We’ll do this by getting main() to instantiate that class that contains it and schedule it to invoke the actual startup method, run(), in the event-dispatching thread. run() is part of the Runnable interface.

9 The HelloWorld class Input and message dialogs Java Swing components A simple GUI example Events Notes Summary import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JButton; Before the actual class declaration, we import several component classes: From awt: FlowLayout (to enable the JFrame to arrange components automatically) The ActionListener interface and the ActionEvent class From Swing: The classes for a JFrame, JLabel, and JButton

10 The HelloWorld class Input and message dialogs Java Swing components A simple GUI example Events Notes Summary public class HelloWorld extends JFrame implements ActionListener { private final JLabel label; private final JButton button; public HelloWorld() { super("Hello World with Swing GUI components"); setLayout(new FlowLayout()); label = new JLabel("Hey you, push my button!"); add(label); button = new JButton("Push me!"); button.addActionListener(this); add(button); } public void actionPerformed(ActionEvent event) { label.setText("Thanks!"); A call to super() sets the window title, and a call to setLayout() passes a new FlowLayout object, which manages the arrangement of components in the order in which we will add them.

11 The HelloWorld class Then: Create components (instantiate them)
Input and message dialogs Java Swing components A simple GUI example Events Notes Summary public class HelloWorld extends JFrame implements ActionListener { private final JLabel label; private final JButton button; public HelloWorld() { super("Hello World with Swing GUI components"); setLayout(new FlowLayout()); label = new JLabel("Hey you, push my button!"); add(label); button = new JButton("Push me!"); button.addActionListener(this); add(button); } public void actionPerformed(ActionEvent event) { label.setText("Thanks!"); Then: Create components (instantiate them) Configure the components (e.g., by adding an ActionListener) Add the components to the extended JFrame.

12 Remarks A JFrame isn’t actually a JComponent, but it is a subclass of Container, so we can add() other components to it. We have already met the ActionListener interface and the actionPerformed() method, as well as the addActionListener()method in Lecture 10 in our stick figure application. Back then, the component that triggered the event was our timer. Now, it is our button, operated by the user. Many components can trigger events. These include anything from a mouse hovering over a component to a key being pressed, a component getting focus and so on. Events are objects, and there are many event classes defined in java.awt.event. Their common ancestor is AWTEvent, which is a subclass of EventObject, whose superclass is Object. See Fig in D&D. ActionEvent is one of these classes. Input and message dialogs Java Swing components A simple GUI example Events Notes Summary

13 Events Input and message dialogs Java Swing components A simple GUI example Events Notes Summary A JComponent can in principle have several events associated with it. For example, a JButton can receive focus (user hitting the tab key until the button becomes active), or the user could click on it, etc. Events are instances of subclasses of AWTEvent. This includes the ActionEvent in the previous example. Not every event necessarily requires handling (e.g., we mightn’t want anything to happen just because a button gets focus). Every event class is associated with a particular type of event listener, e.g., ActionEvent is associated with an ActionListener.

14 Event listeners Input and message dialogs Java Swing components A simple GUI example Events Notes Summary Event listeners are interfaces. A class that is meant to handle events of the respective type must implement that interface, i.e., the methods that the interface declares. All event listener interfaces inherit from a common ancestor interface named EventListener. For example, the class that handled our ActionEvent had to implement the ActionListener interface, which involved implementing the actionPerformed() method. Each JComponent maintains a list (listenerList) of references to objects that implement the respective event listener. We can register event handling objects in that list via addActionListener() and similar methods that the subclasses of JComponent provide. Some of these add…Listener() methods cannot be implemented in JComponent itself. Can you figure out why?

15 Deitel & Deitel … do a couple of things differently in Chapter 12:
They start Swing applications directly from main() because the book hasn’t touched on concurrency at this point. That’s generally fine for simple stuff but isn’t the “proper” way of going about. See: We have implemented the ActionListener interface right on our extended JFrame class: public class HelloWorld extends JFrame implements ActionListener { So when we add the ActionListener for the button, we pass this as the reference of the object that implements the actionPerformed() method. Deitel & Deitel also use a variety of other techniques, some of which we will still meet. They use our technique in Figs and Input and message dialogs Java Swing components A simple GUI example Events Notes Summary

16 What do we know Input and message dialogs Java Swing components
A simple GUI example Events Notes Summary The java.awt package lets us build simple graphical applications and provides a set of basic window controls. The javax.swing package partially replaces and extends this set of controls by mostly lightweight controls written entirely in Java. Lightweight Swing components are subclasses of JComponent JComponents can dispatch events to event listeners. Events are objects, event listeners are instances of classes that implement the appropriate listener interface. Pop-up dialogs based on JOptionPane offer a very simple GUI, more complex GUIs can be built on top of a JFrame. We also know how to use a JLabel and a JButton.

17 Resources Input and message dialogs Java Swing components
A simple GUI example Events Notes Summary D&D Chapter 12

18 Next Lecture More GUI programming (Chapter 12)


Download ppt "Lecture 15 Basic GUI programming"

Similar presentations


Ads by Google