LAB SESSION 9 * COMPONENTS IN JAPPLET * SETBOUNDS METHOD * DIRECTION OF INPUT TO STDIN
JAPPLET IS A SUB CLASS OF APPLET Components in JAPPLET AND APPLET JApplet Applet JButton Button JTextField TextField JCheckbox Checkbox JRadioButton RadioButton JComboBox Choice control ListBox ScrollBars TextArea
JLABEL JLabel(String str,int align) Str is a message displayed on label Align indicates the message should be aligned in center,left or right There are two methods to set the text and get the text on the Jlabel String getText(); Void setText();
Public applet1 extends JApplet { Public void init() { Container cpane=getContentPane(); Cpane.setLayout(null); JLabel ll=new JButton(String str); ll.setBounds(10,10,50,50); Cpane.add(ll); }
JTextField JTextField(String str,int cols) JTextField(int cols) Here str is string that is intially to be displayed in the textBox when applet is loaded cols-no of cols in textBox(width of the textBox);
Public applet1 extends JApplet { Public void init() { Container cpane=getContentPane(); Cpane.setLayout(null); JTextField tt=new JButton(String str); tt.setBounds(10,10,50,50); Cpane.add(tt); }
JButton JButton(String str) Str is a message displayed on button There are two methods to set the text and get the text on the Jlabel String getText(); Void setText();
Public applet1 extends JApplet { Public void init() { Container cpane=getContentPane(); Cpane.setLayout(null); JButton jb=new JButton(String str); Cpane.add(jb); Jb.setForeground(Color.BLUE); }
JCheckBox JCheckBox(String s,boolean state); Str is a message displayed on checkbox. state is set to True or False indicates whether the textbox should be selected or not The state of the textbox can be changed via this method Void setSelected(boolean state); void getSelected(); //get selected items
Public applet1 extends JApplet { Public void init() { Container cpane=getContentPane(); Cpane.setLayout(null); JCheckBox cb=new JButton(String str); Cb.setBounds(10,10,45,70); Cpane.add(cb); }
JRadioButton JRadioButton(String s,boolean state) Str is a message displayed on Radiobutton State indicates whether it is selected or not The state of the textbox can be changed via this method Void setSelected(boolean state); void getSelected(); //get selected items
Public applet1 extends JApplet { Public void init() { Container cpane=getContentPane(); Cpane.setLayout(null); RadioButtonGroup jpg=new RadioButtonGroup(); JRadioButton bb=new JButton(String str); JRadioButton bb1=new JButton(String str); bb.setBounds(10,10,50,50); bb1.setBounds(10,10,50,50); Jbg.add(bb); Jbg.add(bb1); Cpane.add(bb); }
COMBOBOX(Dropdown list) JComboBox(); JComboBox(vector v); It’s a dropdown list containing items that can be selected We can add items by using this method Void additem(Object ob);
Public applet1 extends JApplet { Public void init() { Container cpane=getContentPane(); Cpane.setLayout(null); JComboBox jcb=new JButton(String str); Cpane.add(jcb); Jcb.add(“france”); Jcb.add(“England”); }
LISTS LIST(int cols,int multipleselect) it list a list of items that can be selected cols indicates the length the list how many can be listed multiselect indicates 0/1 0-only one can be selected at a time 1 user can select 2 or more items Methods used to add items are Void add(String name); Void add(String str,int index);
There are two more methods to get selcted item name or index String getSelectedItem(); int getSelectedIndex(); IF MULTISELECT IS 1 THEN String[] getSelectedItems(); int[] getSelectedIndexes(); Void select(int index); Int itemCount();
Public applet1 extends JApplet { Public void init() { Container cpane=getContentPane(); Cpane.setLayout(null); ListBox lb=new ListBox(5,0); Cpane.add(lb); Jcb.additem(“france”,4); Jcb.additem(“England”); }
SETBOUNDS METHOD This method is used to set the Size and shape of all awt components This method works when we set the layout to null and we want to add the components manually Void setBounds(int x1,int y1,int width,int Height); X1,y1—coordinates where the component has to be placed Width and height –are that of the components
Public applet1 extends JApplet { Public void init() { Container cpane=getContentPane(); Cpane.setLayout(null); JButton b1=new JButton(String str); Jb.setBounds(10,10,50,50); Cpane.add(b1); }
Setting color for components Void setBackground(Color.c); Void setForeground(Color.c); Public applet1 extends JApplet { Public void init() { JButton jb=new JButton(String str); Jb.setBackground(Color.GREEN); Jb.setForeground(Color.BLUE); }