Weeks 11&12 Graphical Interfaces All the programs that you have created until now used a simple command line interface, which is not user friendly, so a Graphical User Interface (GUI) should be used. The components of a graphical interface are control elements named widgets activated by the user with the mouse or the keyboard. When activated, a control element will generate a certain answer, according to the program. Most graphical interfaces are based on windows. This means that each active application will have a rectangular area on the screen. All the necessary control elements for the user-application dialog will be placed into this window. In order for an application using the graphical interface to run we need a graphical server to put the screen in the graphical state and to offer the necessary functions to draw and control GUI components. How to create an application window A graphical application will always have a main window. You can create this window using the class java.awt.frame which contains the necessary elements to build a window. class MyWindow extends Frame public MyWindow(String title) super(title); setsize(w/2, h/2); setlocation(w/4, h/4); class FirstWindow public static void main(string[] args) Frame f = new MyWindow("My first window"); f.setvisible(true); When the user acts in some way over an element of a GUI using the mouse or the keyboard, an event will appear, the GUI s element being the source of the event. If you want an answer from an 1
element you should handle the event. This means that someone has to catch the event and to execute a certain function. Handling events in Java When you act over a GUI s element, an object a predefined class (java.awt.event) will be created. If there is a listener object of the created event and this object is known to the source of the event, then a method having as parameter the event object will be called. Of course, the programmer should define first this method. Different elements will generate different events. When you act over a window a java.awt.event.windowevent object will be created and when acting over a button a java.awt.event.actionevent object will be created. The event handler is an object having methods with predefined names from predefined interfaces. There is an interface for every event type. For example, to handle a WindowEvent one has to implement WindowListener interface. The method s name reflects the action executed over an element. The interface WindowListener has the following methods: public void windowactivated(windowevent e); public void windowclosed(windowevent e); public void windowclosing(windowevent e); public void windowdeactivated(windowevent e); public void windowdeiconified(windowevent e); public void windowiconified(windowevent e); public void windowopened(windowevent e); To link an event handler to its source the following syntax is needed: EventSourceObject.addEvenimentListener(EventHandlerObject); import java.awt.event.*; class MyWindowHandler implements WindowListener public void windowclosing(windowevent e) System.exit(0); // all the methods from WindowListener interface class MyWindow extends Frame public MyWindow(String title) 2
super(title); MyWindowHandler r = new MyWindowHandler(); this.addwindowlistener(r); setsize(w/2, h/2); setlocation(w/4, h/4); class FirstWindow public static void main(string[] args) Frame f = new MyWindow("The first window"); f.sevisible(true); How to put elements in a window? You have to create objects of the classes which model the elements. Then, for each element you have to use the method add to place them into the window. You can specify where to put an element in the window using layout managers like FlowLayout or BorderLayout. A button in Java is modeled in the class called Button. A button can generate an ActionEvent, when the user clicks on it. The handler of such an event should implement the interface ActionListener, which contains only a method: actionperformed. To an easy control of the relations from the GUI s elements, the applications should be designed accordingly with the following designing template: a class to describe the window; a class for the handler of the possible events; a class to mediate the dialogs, which creates the GUI s elements, put them into the window and, after an event, updates the elements state. import java.awt.event.*; public class MyWindow extends WindowAdapter implements ActionListener private int counter=0; private Frame f; private Button b; 3
private Label lb; public void assembleframe() f = new Frame("My first window"); f.setsize(w/2, h/2); f.setlocation(w/4, h/4); f.addwindowlistener(this); b = new Button("Push"); b.addactionlistener(this); lb = new Label("0"); f.setlayout(new FlowLayout()); f.add(b); f.add(lb); f.setvisible(true); public void windowclosing(windowevent e) System.exit(0); public void actionperformed(actionevent e) counter++; lb.settext(""+counter); public static void main(string[] args) MyWindow win = new MyWindow (); win.assembleframe(); 4
If there are more than one buttons in a window, you can use the following situations for their handlers: create two different classes for the event handlers of the two buttons and define accordingly the method actionperformed; use the information from the generated ActionEvent object, sent as parameter to the method actionperformed. A possibility is to use the method public Object getsource(); Problem week 11 Change the above example to have two buttons, a label and a checkbox. By default, button 1 is enabled, button 2 is disabled and the checkbox is unchecked. When you check the checkbox, button 2 will be enabled and button 1 will be disabled. When you push button 1, the label s content will be incremented and pushing button 2 the label s content will be decremented. The starting label s value is 0. Reading data from the keyboard To read data from the keyboard, a TextField object is needed. The following example shows you how to read the elements of an array using: o a text field to read the elements from the keyboard, o a button to insert an element into a location in the array, o a label to point the index of the current element in the array. import java.awt.event.*; class Table private int[] tab; public Table(int n) tab=new int[n]; public void setelem(int i, int val) tab[i]=val; public int getdim() return tab.length; 5
public class MyWindow extends WindowAdapter implements ActionListener private int counter=0; private Frame f; private Button b; private Label lb; private TextField tx; Table t; public void assembleframe() t = new Table(10); f = new Frame("My first window"); f.setsize(w/2, h/2); f.setlocation(w/4, h/4); f.addwindowlistener(this); b = new Button("Insert"); b.addactionlistener(this); lb = new Label("Element 0 "); tx = new TextField(15); f.setlayout(new FlowLayout()); f.add(b); f.add(tx); f.add(lb); f.setvisible(true); public void windowclosing(windowevent e) System.exit(0); public void actionperformed(actionevent e) 6
if (counter < t.getdim()) try int val = Integer.parseInt(tx.getText()); t.setelem(counter,val); counter++; lb.settext("element "+counter); tx.settext(""); catch (NumberFormatException exp) lb.settext("wrong format number"); else lb.settext("array full"); public static void main(string[] args) MyWindow l = new MyWindow (); l.assembleframe(); Problem week 12 Create a GUI to contain a text field, text area, a button and a label. If there is a text written in the text field, pressing the button will move the text from the text field to the text area, otherwise a dialog will appear pointing that there is no text in the text field. The label will show how many values are written in the text area. The program will end when inside the text field the key e is written. 7