Objectives • To use the set of GUI components • To use event-handling model for different components • To study layout managers, for flexible window layouts that don't depend on a particular window size or screen resolution
Introduction • The Java Foundation Classes (JFC) provide two frameworks for building GUI-based application and interestingly both rely on the same event handling model: – AWT – Swing • AWT relies on the underlying operating system on a specific platform to represent its GUI components (i.e components in AWT are called Heavyweight), • Swing implements a new set of lightweight GUI components that are written in Java and has a pluggable look and feel. • These lightweight components are not dependent on the underlying window system.
Components and Containers • A graphical user interface is developed with the help of graphical elements like – buttons, scrollbars, lists, textfields, etc. • These elements are called components. • In AWT, these components are instances of the respective Component classes. • Components cannot exist alone; they are found within containers. • Actually, containers are themselves components, thus they can be placed inside other containers. • In AWT, all containers are objects of class Container or one of its subtypes.
Component • subclass of the Object class & super class of classes such as – Button, – Label, – CheckBox, – RadioButton, Choice, Canvas, etc • Componenets are added to a window using the add() method – Component add(Component ComObj) – ComObj is the object of the Component, which is to be added – This method returns the reference to the ComObj. • If you wish to remove a Component from a window, use remove() method – void remove(Component ComObj)2
Button • The Button class belongs to java.awt package – public class Button extends Component implements Accessible • This class creates a button which when pushed or pressed generates an event. • The two constructors belonging to this Button class are: – Button() throws HeadlessException – Button(String str)throws HeadLessException; • To create a button – Button buttonName = new Button(Str); – ‘buttonname’ is the name you give to the button object and ‘Str’ is the text you want to appear on the button. • Once the object for Button is created, it needs to be added to the applet or any other container using – add(buttonname); – void setLabel(String str) for changing the button’s label – String getLabel() for getting the Buttons label’s text
The Output Label • Labels consist of a text string for display only and they never call an action method. • A Label can be justified LEFT, RIGHT, or CENTERED. – new Label(“This label is for demonstration.”, Label.RIGHT); Label Example /*<applet code=”LabelClass” width=350 height=100></applet>*/ import java.applet.*; import java.awt.*; public class LabelClass extends Applet { public void init(){ Label firstLabel = new Label(“Labels exist simply “); add(firstLabel); Label secLabel = new Label(“to place text on the screen”); add(secLabel); Label thirdLabel = new Label(“They can be aligned left, right or center.”); add(thirdLabel);}}
Checkbox • Checkboxes are used as on-off or yes-no switches • if you click on an unchecked checkbox, it will get checked and vice versa. • Constructors of Checkbox – Checkbox() – Checkbox(String str) – Checkbox(String str, boolean on) – Checkbox(String str, CheckBoxGroup cbg, boolean on)
Radio Buttons • are special kind of checkboxes where only one box can be selected at a time. • The CheckboxGroup class is used to group together a set of checkboxes – CheckboxGroup fruits = new CheckboxGroup(); • After creating checkbox group, the individual checkboxes are added to that group. – add(new Checkbox(“mango”, fruits, false)); – add(new Checkbox(“papaya”, fruits, false)); – add(new Checkbox(“guava”, fruits, false)); – add(new Checkbox(“apple”, true, yes));
Radio Button Example /*<applet code=”RadioDemo.class” width=300 height=200></applet>*/ import java.applet.*; import java.awt.*; import java.awt.event.*; public class RadioDemo extends Applet implements ItemListener{ Checkbox red, white, green; CheckboxGroup cbg; public void init(){ add(new Label(“The 4 radio buttons will change the screen color.”)); cbg = new CheckboxGroup(); red = new Checkbox(“Red”,cbg,false); white = new Checkbox(“White”,cbg,false); green = new Checkbox(“Green”,cbg,false); add(new Label(“Notice that you can only select one radio button.”)); add(new Label(“And selecting a radio button triggers an event”));
Example /*<applet code=ShopList.class width=600 height=600></applet>*/ import java.applet.*;import java.awt.*;import java.awt.event.*; public class ShopList extends Applet implements ActionListener { List original; List copy; public void init(){ original= new List(8,false); copy= new List(10,false); populateList(); add(original); Button b1 = new Button(“>>>>”); add(b1); add(copy); Button b2 = new Button(“Clear”); add(b2); add(new Label(“Select an item from the list on the left and hit >>>> to place it in the other list”)); b1.addActionListener(this); b2.addActionListener(this);}
Container classes • Window – Window is a top-level display surface. An object of Window class is not Attached to nor embedded within another container. An instance of the Window does not have border, title bar or menu. • Frame – Frame is a top-level window with a border and title. An instance of the Frame class may have a menu bar, title bar and borders. It is otherwise like an object of the Window class. • Dialog – Dialog is top-level display surface (a window) with a border and title. An object of the Dialog class cannot exist without an associated object of the Frame class. • Panel – Panel is generic container for holding components. An instance of the Panel class provides a container to which components can be added. It does not add any new method; it simply implements the Container.
FlowLayout • arranges components from left-to-right and top-to- bottom, centering components horizontally. • The direction of flow is determined by the container’s componentOrientation property. – ComponentOrientation.LEFT_TO_RIGHT : Items run left to right and lines flow top to bottom, e.g. English, French, etc. – ComponentOrientation.RIGHT_TO_LEFT : Items run right to left and lines flow top to bottom, e.g. Arabic, Hebrew, etc. • There is five pixel gap between the components arranged in this layout. • the default layout for the Applet.
BorderLayout • This is the default layout of the Frame. – public class BorderLayout extends Object implements LayoutManager2, Serializable • There can be only one component in each region and the regions are identified as constants: – NORTH, SOUTH, EAST, WEST, and CENTER. • Any of these five constant names can be used while adding a component to a container. – Panel pnl = new Panel(); – pnl.setLayout(new BorderLayout()); – pnl.add(new Button(“submit”), BorderLayout.NORTH);
Menu • Menu is a class which inherits MenuItem class and two interfaces: – MenuContainer and – Accessible. • Menubar deploys a menu object which is a dropdown menu component. – It shows a list of menu choices. • To implement this concept we use three classes: – MenuBar, – Menu, and – MenuItem.
Example import java.awt.event.*; import java.awt.*; public class DemoMenu extends Frame implements ActionListener{ public void demoMenu() { setTitle(“MenuDemo”); setSize(250,150); MenuBar menuBar = new MenuBar(); setMenuBar(menuBar); MenuShortcut n=new MenuShortcut(KeyEvent.VK_N); MenuShortcut o=new MenuShortcut(KeyEvent.VK_O); MenuShortcut x=new MenuShortcut(KeyEvent.VK_X); Menu fileMenu = new Menu(“File”); Menu editMenu = new Menu(“Edit”); MenuItem newAction = new MenuItem(“New”,n); MenuItem openAction = new MenuItem(“Open”,o); MenuItem exitAction = new MenuItem(“Exit”,x);
Summary • In this chapter we have emphasized on Graphical User Interface (GUI) for input and output. • Java has a package named as java.awt, having various classes responsible for generating various GUI frameworks. • These components include Button, Scrollbar, Choicebox, List, TextField, etc. • AWT defines ways to lay the AWT components in containers. • There are many layout managers in AWT like – FlowLayout, – GridLayout, – GridBagLayout, – CardLayout, etc, • Menu is a class that inherits MenuItem class and two interfaces: MenuContainer and Accessible.