Controls

The little yet useful underpinnings for the Graphical look

Hang on folks, here we have the programs dished out; the explanation will soon follow. Or for the people in the know, This page is under construction . A brief, clunky note about the programs is made to pass for an explanation, but we will be here soon (and hope you will be too!) with more explanatory stuff. As regards now, the programs will probably speak for themselves. :) Thanks for stopping by. See you soon. So long.


Buttons

We said a while ago that this tutorial was tailor-made for you. How in blazes could a tailor do without buttons ? Try out the following code...
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	Button b;
        public void init()
        {	b = new Button("hell");
                add(b);
        }
}
Here we have a button b added, very much the same as our first AWT program but for one hair-breadth difference. In this way, we have an object b that looks like a Button . Therefore, we can also use the functions of the class Button . With this in mind, let us proceed to the next one...
setLabel()
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	Button b;
        int i=0;
        public void init()
        {	b=new Button("i..." + i);
                add(b);
        }
        public boolean mouseUp(Event e, int x, int y)
        {	i++;
                b.setLabel("i..." + i);
                return true;
        }
}
Out here we first add the button b with the name of "i...0"(because i is 0 in the beginning). Then we include the mouseUp() where we change the label or the name of the button with the setLabel() . Meanwhile, we have a variable i which we increment everytime the user clicks with the mouse. The increased value of i is displayed as the name of the button.
reshape()
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	Button b;
        int i = 0;
        public void init()
        {	b = new Button("hell");
                add(b);
        }
        public boolean mouseUp(Event e, int x, int y)
        {	b.reshape(x, y, 30, 40);
                return true;
        }
}
This program demonstrates how the button or any other feature in the AWT like the text field, list box etc can be moved to any coordinate and resized. For our button b , we use the reshape() which means we want to change the shape and the position of our button. It is apparent that the reshape() consumes four parameters. The coordinates of the position where we want the button to be, and its width and height.
getLabel()
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	Button b;
        int i = 0;
        public void init()
        {	b=new Button("hello");
                add(b);
        }
        public boolean mouseUp(Event e,int x, int y)
        {	showStatus(b.getLabel());
                return true;
        }
}
When we can set a label to a button, it ofcourse is possible to get the label of the button as well. The above program explains how the getLabel() functions. It will become a little clearer with the following program...
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	Button b;
        public void init()
        {	b=new Button("hello");
                add(b);
        }
        public boolean mouseUp(Event e,int x, int y)
        {	if(b.getLabel()=="hello")
                        b.setLabel("hi");
                else
                        b.setLabel("hello");
                return true;
        }
}
When the user clicks with the mouse, it is checked if the label of the button is "hello" or "hi". If it is "hello", the label is being changed to "hi" and vice versa. If a button is beginning to give you the pips, let's move on to a little higher level.
Hide and seek
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	Button a, b, c;
        public void init()
        {	a = new Button("Hide");
                b = new Button("Show");
                c = new Button("Hello");
                add(a);
                add(b);
                add(c);
        }
        public boolean action(Event e,Object o)
        {	if("Hide".equals(o))
                {	c.hide();
                }
                if("Show".equals(o))
                {	c.show();
                }
                return true;
        }
}
Three is a crowd. That's why we have three buttons here (So you don't get the feeling we are being niggardly with their use). The buttons a, b and c are assigned the names "Hide", "Show" and "Hello" respectively. And then, for reasons other than impressing you, we use the action() instead of the variety of mouse functions. The actions function uses the parameters Event and Object . If clicked inside any of the button, the Object o stores the string that is the label of that particular button. Inside the action() function, which keeps a record of all the actions of the user, we have the condition. If the user clicks on the button named "Hide", o will be "Hide" and in that case we are hiding the button. If, on the other hand , the user has clicked on "Show", we show back the button.
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	Button a, b, c;
        public void init()
        {	a = new Button("Enable");
                b = new Button("Disable");
                c = new Button("Hello");
                add(a);
                add(b);
                add(c);
        }
        public boolean action(Event e,Object o)
        {	if("Enable".equals(o))
                {	c.enable();
                }
                if("Disable".equals(o))
                {	c.disable();
                }
                return true;
        }
}
This program works exactly like the previous one. But instead of hide() and the show() , we have the enable() and the disable() . It is hard to find another program more self-explanatory. Most of the functions that work with buttons also work with other members of the AWT, for e.g., a textfield can be hidden/shown or disabled/enabled too.


TextField

import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	Button b;
        TextField t;
        public void init()
        {	b = new Button("click");
                t = new  TextField(60);
                add(b);
                add(t);
        }
        public boolean action (Event e, Object o)
        {	if ("click".equals(o))
                {	showStatus(t.getText());
                        t.setText("Hello how are you");
                }
                return true;
        }
}

We create a TextField 60 characters long and a button to go along with it. When clicked on the button, first the text already present in the TextField will be shown in the status bar. Then, its text of is set to "Hello how are you". The programs here can be a little hard for the people not really conversant with programming. It can be further simplified as follows...
     public boolean action (Event e, Object o)
        {	String s;   boolean  b;
		s = "click";
		b = s.equals(o);	
		if (b)
                {	showStatus(t.getText());
                        t.setText("Hello how are you");
                }
                return true;
        }
appendText()
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	Button b;
        TextArea t;
        public void init()
        {	b = new Button("click");
                t = new  TextArea(5,60);
                add(b);
                add(t);
        }
        public boolean action (Event e, Object o)
        {	if ("click".equals(o))
                {	showStatus(t.getText());
                        t.appendText("Hello how are you");
                }
                return true;
        }
}

We have a text field that has 5 lines and 60 columns. In the action() we don't say setText, but appendText . This means that the text provided in this way will be added on to the text already present in the text field.


CheckboxGroup

import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	CheckboxGroup g;
	Checkbox a, b, c;
	public void init()
	{	g = new CheckboxGroup();
		add(a = new Checkbox("Good", g, false);
		add(b = new Checkbox("Bad", g, false);
		add(c = new Checkbox(null , g, true);
	}
	public boolean mouseUp(Event e, int x, int y)
	{	showStatus("state of a..." + a.getState() + "  
		state of c..." +c.getState());
		return true;
	}		
}
The CheckboxGroup is, in effect, a collection of checkboxes. But, too weirdly to be argued, the CheckboxGroup looks like our usual radio buttons. Here we create an object g that looks like a CheckboxGroup, and in the init function, we initialize it. We add to it three Checkboxes, named "Good", "Bad" and null respectively (null is not a name, it means that no name has been given). We also have to tell that the checkbox belongs to which CheckboxGroup, as in our case is g . The third parameter tells which Checkbox will be currently active. In plain English, this means that when we run the program, the third one will be active. After that if we click in, say, the Checkbox a, and then clicked inside the window, the showStatus() will show us that the "state of a..." is true and that of c is false.


Choice

import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	List l;
        Choice c;
        public void init()
        {	c=new Choice();
                c.addItem("xxx");
                c.addItem("yyy");
                c.addItem("zzz");

                l = new List();
                l.addItem("a1");
                l.addItem("a3");
                l.addItem("a2");
                add(l);
                add(c);
        }
}
We have a List l and a list box (or a Choice as it is called). To the Choice , we add three items with addItem ... xxx, yyy and zzz . Likewise, to the List we add a1, a3 and a2 . We are not really drunk to have the order as a1, a3, a2 and our basic arithmetic is not all that weak either. It is plainly to demonstrate that the items do not get added in a sorted order. After adding the items to the List and Choice , we add them to the Applet window with the add() . That, and a little tinkering with the functions of the previous examples will have you deft in Lists and choices in minutes.
delItem()
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	List l;
        Button b;
        public void init()
        {	b = new Button("Remove");
                l = new List(3, false);
                l.addItem("aaa");
                l.addItem("bbb");
                l.addItem("ccc");
                l.addItem("ddd");
                l.addItem("eee");
                add(l);
                add(b);
        }
        public boolean action (Event e, Object o)
        {	if("Remove".equals(o))
                {	l.delItem(1);
                }
                return true;
        }
}

First a list of 5 members is created (though only 3 will be visible because we specify so) and added to the applet window. A button called "remove" is created. When the user clicks on this button, we delete the item number 1 from the list using delItem() . Note that when we specify 1, the second item gets the scissors because the items start from 0 onwards. Also the 'false' that we specify in the statement new List() (or for the C++ folks, in the constructor) means that multiple selection will not be allowed from the list. If we want to allow multiple selection of the items in a list, we have to specify it as true. consider the following code...
Multiple Selection
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	List l;
        public void init()
        {	l = new List(3,true);
                l.addItem("aaa");
                l.addItem("bbb");
                l.addItem("ccc");
                l.addItem("ddd");
                l.addItem("eee");
                add(l);
        }
}
Now you can select more than one options of the list at the same time.
getSelectedIndex() and getSelectedItem()
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	List l;
        int i=0;
        String s;
        public void init()
        {	l = new List(3,false);
                l.addItem("aaa");
                l.addItem("bbb");
                l.addItem("ccc");
                l.addItem("ddd");
                l.addItem("eee");
                add(l);
        }
        public boolean handleEvent(Event e)
        {	if (e.target instanceof List)
                {	i=l.getSelectedIndex();
                 	s=l.getSelectedItem();
                 	showStatus("Name.."+s+"..no.."+i);
                }
                return true;
        }
}
When an item of the list is selected, the showStatus() will display the number of the item selected and its name on the status line. The getSelectedIndex() returns the number of the item that was selected, and the getSelectedItem returns a string that is the name of the item selected.
countItems()
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	List l;
        public void init()
        {	l = new List(3,false);
                l.addItem("aaa");
                l.addItem("bbb");
                l.addItem("ccc");
                l.addItem("ddd");
                l.addItem("eee");
                add(l);
        }
        public boolean mouseUp(Event e, int x, int y)
        {	showStatus("Number of items.."+l.countItems());
                l.clear();
                return true;
        }
}
When clicked with the mouse, the countItems() will display the number of items in the list box on the status line. The clear() should clear the list but for some old grudge, it does not. (Beta version?)


Scrollbars

import java.applet.*;
import java.awt.*;
public class zzz extends Applet 
{	Scrollbar s;
	public void init()
	{	setLayout(new BorderLayout());
		s = new Scrollbar(Scrollbar.HORIZONTAL,10,5,1,100);
		add("North",s);
	}
	public boolean mouseDown(Event e, int x,int y)
	{	s.setValue(s.getValue()+5);
		showStatus("value " + s.getValue());    
		return true;
	}
}
The above program gives you a horizontal scrollbar with 1 as the minimum value for the scroll bar and 100 as the maximum value/limit. The parameter 10 is the starting point. Everytime we click, we increase the value of the scrollbar by 5. A noteworthy feature is that we'd rather have the borderlayout (refer to the chapter on Layouts ) because the scrollbars are better off in the corners. I still have to see a scrollbar which reposes in the centre of a window.


Labels

import java.awt.*;
import java.applet.*;
public class zzz extends Applet
{	public void init()
	{	Label l=new Label("good");
 		add(l);
  }
}
Inside the init(), we initialize a Label and call it "good". We add it to the current Applet window. When you run the above code, you see a label on the screen.
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	public void init()
 {	setLayout(new BorderLayout());
		Label l=new Label("good");
 		add("North",l);
	}
}
Here we explicitly state the layout we desire, viz., BorderLayout. Then we add the Label to the North. In this way, we can control the positioning of a label.


Text Area
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	TextArea t;
        public void init()
        {	t=new TextArea();
        	add(t);
        }
        public boolean mouseMove(Event e, int x, int y)
        {	t.setForeground(Color.blue);
                if (t.inside(x,y))
                {	showStatus("in");
                        setBackground(Color.red);
                }
                else
                {	showStatus("out");
                         setBackground(Color.blue);
                 }
                 repaint();
                return true;
        }
}

With the setForeground() function, the color of the TextArea is set to blue. The inside() , if fed with two parameters, obediently tells us if the mouse is inside the scope of the text area or not. Depending on whether the mouse is within the text region or outside it, we alternate background colors. If inside, then red, if out, blue. Side by side, the showStatus also shows whether the cursor is "in" the text area or not.
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	TextArea t;
        public void init()
        {	t=new TextArea();
        add(t);
        }
        public boolean handleEvent(Event e)
        {	if(e.target instanceof TextArea)
                {	t.setForeground(Color.blue);
                        setBackground(Color.red);
                }
                else
                {	setBackground(Color.blue);
                 }
                 repaint();
                return false;
        }
}

With this porgram, we introduce the handleEvent() . If the user types inside the textarea or has the mouse inside the region (this is determined through instanceof ), the foreground color is set to blue and the background color to red.
getText()
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	Button b;
        TextArea t,u;
        public void init()
        {	b = new Button("click");
                t = new  TextArea(10,50);
                u = new TextArea("Hello ...",5,10);
                add(b);
                add(t);
                add(u);
        }
        public boolean action (Event e, Object o)
        {	if ("click".equals(o))
                {	showStatus(t.getText()+"....."+u.getText());
                }
                return true;
        }
}

We have a button and two text areas. The button is named "click". The text area called t is placed at 10, 50 and the one called u is placed at 5, 10. But notice the difference. u is given a text to be displayed as an initial text ("Hello ..."). Now, when the user clicks on the button, we get the text of u and show it as the status. Therefore, the status will be "Hello ...".
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	List l;
        TextArea t;
        Button b;
        int i=0;
        public void init()
        {	t = new TextArea(3,10);
                l = new List(3,false);
                b= new Button("click");
                l.addItem("aaa");
                l.addItem("bbb");
                l.addItem("ccc");
                l.addItem("ddd");
                l.addItem("eee");
                add(l);
                add(t);
                add(b);
        }
        public boolean action(Event e, Object o)
        {	if("click".equals(o));
                {	i = l.countItems();
                        for(int j = 0; j <  i; j++)
                                t.appendText(l.getItem(j));
                }
        return true;     
        }
}
Quite a simpleton program, this one. We have a list with 5 items and a text area. Now the names of all the items in the list are to be appended to the text area. Even a blonde could crack this one :) But consider the following code and try to figure what it does...
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	List l, m;
        Button a, b, c;
        TextArea t;
        int i = 0;
        String s;
        public void init()
        {	l = new List(3, false);
                m = new List(5, false);
                a = new Button("Add");
                b = new Button ("Del");
                c = new Button("Go");
                t = new TextArea(5,30);
                l.addItem("aaa");
                l.addItem("bbb");
                l.addItem("ccc");
                l.addItem("ddd");
                l.addItem("eee");
                add(l);add(a);add(b);add(c);
                add(m);add(t);
        }
        public boolean action(Event e, Object o)
        {	if("Add".equals(o))
                {	s=l.getSelectedItem();
                 	m.addItem(s);
                }
                if ("Del".equals(o))
                {	if (m.countItems() > 0)
                        {	i=m.getSelectedIndex();
                          	m.delItem(i);
                        }
                }
                if("Go".equals(o))
                {	t.setText(" ");
                	for(int k = 0; k < m.countItems(); k++)
                		t.appendText(m.getItem(k)+"...");
                }
        	return true;     
        }

}
Cryptic clues: Add will add to the other list box, delete will delete from the second list, go will show the items in the second list on the text area
Or try another modified form of this one:
import java.applet.*;
import java.awt.*;
public class zzz extends Applet
{	List l,m;
        Button a,b,c;
        TextArea t;
        int i=0;
        String s;
        public void init()
        {	l = new List(3,false);
                m = new List(5,false);
                a = new Button("Add");
                b = new Button ("Del");
                c = new Button("Go");
                t = new TextArea(5,30);
                l.addItem("aaa");
                l.addItem("bbb");
                l.addItem("ccc");
                l.addItem("ddd");
                l.addItem("eee");
                add(l);add(a);add(b);add(c);
                add(m);add(t);
        }
        public boolean action(Event e, Object o)
        {	if("Add".equals(o))
                {	i=l.getSelectedIndex();
                 	s=l.getSelectedItem();
                 	m.addItem(s);
                 	l.delItem(i);
                }
                if ("Del".equals(o))
                {	if (m.countItems() > 0)
                        {	s=m.getSelectedItem();
                          	i=m.getSelectedIndex();
                          	m.delItem(i);
                          	l.addItem(s);
                        }
                }
                if("Go".equals(o))
                {	t.setText(" ");
                	for(int k = 0; k < m.countItems();k++)
                		t.appendText(m.getItem(k)+"...");
                }
        	return true;     
        }

}


If you have not yet lost your self-control, rage to the next topic Who framed Roger Rabbit? or just spare a minute for your comments, feedback, suggestions et al to us and then lose it...

Back to our Java page


Vijay Mukhi's Computer Institute
B-13, Everest Building, Tardeo, Bombay 400 034, India.
http://www.neca.com/~vmis
e-mail : vmukhi@giasbm01.vsnl.net.in
1