Choice Control

Download Report

Transcript Choice Control

Chapter - 10
Graphical User Interface (continued)
This chapter includes 



Choice Control
List Box
Menu
DialogBox
Choice Control

How does it look ?
 How to incorporate a Choice control ?
 you first take an object of Choice class.
 Two constructors are important here.
 The default constructor: Choice()
The constructor that take an array of String as argument:
Choice (String ar[])
 To add items in the choice control you use the add() method. Such as
Choice c = new Choice();
c.add(“Bengali”);
c.add(“English”);
c.add(“Math”);
 The second constructor takes the items of the choice control from the supplied
array.
String subjects[]={“Bengali”, “English”, “Math”};
Choice d= new Choice (subjects);
 Add the Choice control in your container Window.
add(c);
 Once Choice controls are added in your program they are ready to be used by
the user.
 If the user clicks the choice control and selects an item it generates an
ItemEvent.

To know which item is selected by the user use the methods
getSelectedItem() which returns a String
getSelectedIndex() which returns an integer.
 Let us write a gui application that will have a Choice control with
items:Bengali, English, and Math and a TextField. We will set the selected
items text from the Choice control in the TextField.
 The program is as follows:
import java.awt.*;
import java.awt.event.*;
class test extends Frame implements ItemListener
{
Choice c;
TextField tf;
public test()
{
super("Test");
setLayout(new FlowLayout());
tf=new TextField(20);
add(tf);
c=new Choice();
c.add("Bengali");
c.add("English");
c.add("Math");
add(c);
c.addItemListener(this);
setSize(200,200);
setVisible(true);
}
public void itemStateChanged(ItemEvent e)
{
if (e.getItemSelectable()==c)
tf.setText(c.getSelectedItem());
}
public static void main (String args[])
{
test t=new test();
}
}
List Control
 What is a List Control ?
 How to incorporate a List control ?
 you first take an object of List class.
 The constructor takes an integer and a boolean value:
List (int n, boolean b)
 To add items in the List control you use the add() method. Such as
List l = new List(4, true);
l.add(“Bengali”);
l.add(“English”);
l.add(“Math”);
 Add the List control in your container Window.
add(l);
 Once Choice controls are added in your program they are ready to be used by
the user.
 If the user clicks the list control and selects an item it generates an ItemEvent.
 But Most of time we will depend on some other event after which we will
retrieved the selected items of the list.

To know which items are selected by the user use the methods
getSelectedItems() which returns a array of String
getSelectedIndexes() which returns an array of integer.
Put it Together
 Let us write a gui application that will have a List control with items:Bengali,
English, and Math and a TextField. We will set the selected items text from
the List control in the TextField.
import java.awt.*;
import java.awt.event.*;
public class sc2 extends Frame implements ActionListener
{
TextField tf;
List c;
Button b;
Frame f;
public sc2()
{
super("MY application");
setLayout(new FlowLayout());
tf=new TextField(10);
add(tf);
Font f=Font.getFont("Times New Roman");
tf.setFont(f);
c=new List(4,true);
c.add("Bengali");
c.add("English");
c.add("Math");
add(c);
c.addActionListener(this);
b=new Button("Select");
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent i)
{
String name="";
if (i.getSource()==b)
{
for(int j=0;j<(c.getSelectedItems()).length;j++)
name+=(c.getSelectedItems())[j];
tf.setText(name);
}
}
public static void main(String args[])
{
sc2 a=new sc2();
a.setSize(300,300);
a.setVisible(true);
a.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
}
}
Menu
 Menu is perhaps the most widely used Graphical user interface through which
the user selects different modules of the application.
 In Java the Menu is divided into three component:
MenuBar
Menu
MenuItem.
 To incorporate Menu in a program, We first Create an Object of MenuBar
class with the default constructor.
MenuBar mb;
mb=new MenuBar();
 We create the Menu objects as many as are necessary for the program.
Menu m1;
m1= new Menu(“File”);
 Then we create the MenuItem Objects
MenuItem mi1;
mi1=new MenuItem(“New”);
 MenuItems generate actionevent so we must provide an ActionListener
mi1.addActionListener(this);
 Next we add the MenuItem objects to their corresponding Menu object.
m1.add(mi1);
 Next we add the Menu objects to MenuBar object.
mb.add(m1);
 Finaly we add the MenuBar object to the Applet or Applicatoin by using the
setMenuBar method.
setMenuBar(mb);
 Let us write an application that places all the above codes together.
import java.awt.*;
import java.awt.event.*;
class test2 extends Frame implements ActionListener
{
Label l;
MenuBar mb;
Menu m1,m2;
MenuItem mi1,mi2,mi3;
public test2()
{
super("Test2");
l=new Label();
add(l);
mb=new MenuBar();
m1=new Menu("File");
m2=new Menu("Help");
mi1=new MenuItem("New");
mi2=new MenuItem("Exit");
mi3=new MenuItem("About");
mi1.addActionListener(this);
mi2.addActionListener(this);
mi3.addActionListener(this);
m1.add(mi1);
m1.addSeparator();
m1.add(mi2);
m2.add(mi3);
mb.add(m1);
mb.add(m2);
setMenuBar(mb);
setSize(300,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==mi1)
l.setText("You have Selected New from File Menu");
if (e.getSource()==mi2)
System.exit(0);
if (e.getSource()==mi3)
l.setText("You have Selected About from Help Menu");
}
public static void main (String args[])
{
test2 t=new test2();
}
}
 Note the Separator between the menuitem new and exit.
 This kind of separator is heavily used to separate different groups of
menuitems.
DialogBox
 A dialogue occurs between the user and your program.
 Here, the user mentions
What the program should do
What the program should not do
 Two Types of Dialogs
Modal
ImModal
 Let us write a program which will generate a dialog box like the
following
 import java.awt.*;
 import java.awt.event.*;
 public class DialogWindow extends Frame

implements ActionListener {
private SimpleDialog dialog;
private TextArea textArea;
String newline;
public DialogWindow() {
textArea = new TextArea(5, 40);
textArea.setEditable(false);
add("Center", textArea);
Button button = new Button("Click to bring up dialog");
button.addActionListener(this);
Panel panel = new Panel();
panel.add(button);
add("South", panel);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
newline = System.getProperty("line.separator");
}
public void actionPerformed(ActionEvent event) {
if (dialog == null) {
dialog = new SimpleDialog(this, "A Simple Dialog");
}
dialog.setVisible(true);
}
public void setText(String text) {
textArea.append(text + newline);
}
public static void main(String args[]) {
DialogWindow window = new DialogWindow();
window.setTitle("DialogWindow Application");
window.pack();
window.setVisible(true);
}
}
class SimpleDialog extends Dialog implements ActionListener {
TextField field;
DialogWindow parent;
Button setButton;
SimpleDialog(Frame dw, String title) {
super(dw, title, false);
parent = (DialogWindow)dw;
//Create middle section.
Panel p1 = new Panel();
Label label = new Label("Enter random text here:");
p1.add(label);
field = new TextField(40);
field.addActionListener(this);
p1.add(field);
add("Center", p1);
//Create bottom row.
Panel p2 = new Panel();
p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
Button b = new Button("Cancel");
b.addActionListener(this);
setButton = new Button("Set");
setButton.addActionListener(this);
p2.add(b);
p2.add(setButton);
add("South", p2);
//Initialize this dialog to its preferred size.
pack();
}
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if ( (source == setButton)
| (source == field)) {
parent.setText(field.getText());
}
field.selectAll();
setVisible(false);
}
}