SlideShare a Scribd company logo
Lists and Scrollbars




http://improvejava.blogspot.in/
                                  1
Objectives

On completion of this period, you would be
able to know

• Lists
• Scrollbars




               http://improvejava.blogspot.in/
                                                 2
Recap

In the previous class, you have leant

• Checkbox and CheckboxGroups
• Handling the respective events




                  http://improvejava.blogspot.in/
                                                    3
Lists                       contd..


• The List class provides a compact, multiple-
  choice, scrolling selection list
• It can also be created to allow multiple
  selections
• List provides these constructors
  – List( )
  – List(int numRows)
  – List(int numRows, boolean multipleSelect)



                   http://improvejava.blogspot.in/    4
Lists                    contd..


• The first version creates
   – a List control that allows
     only one item to be
     selected at any one time
• In the second form
   – the value of numRows
     specifies the number of
     entries in the list that will
     always be visible
• In the third form
   – if multipleSelect is true,
     then the user may select
     two or more items at a time
   – If it is false, then only one
                           http://improvejava.blogspot.in/   5
     item may be selected
Lists                          contd..


• To add a selection to the list, call add( )
• It has the following two forms
   – void add(String name)
   – void add(String name, int index)
   – Here, name is the name of the item added to the list
   – The first form adds items to the end of the list
   – The second form adds the item at the index specified
     by index
   – Indexing begins at zero
   – You can specify –1 to add the item to the end of the
     list
                    http://improvejava.blogspot.in/         6
Lists                                contd..


• For lists that allow only single selection you can
  determine which item is currently selected by
  calling
   – either getSelectedItem( )
   – or getSelectedIndex( )
   – These methods are shown here
      • String getSelectedItem( )
      • int getSelectedIndex( )


                     http://improvejava.blogspot.in/        7
Lists                           contd..

• For lists that allow multiple selection you must
  use
  – either getSelectedItems( )
  – or getSelectedIndexes( )
  – They are shown here
     • String[ ] getSelectedItems( )
     • int[ ] getSelectedIndexes( )
• Given an index, you can obtain the name
  associated with the item at that index by calling
  – getItem( )
  – It has this general form
     • String getItem(int index)


                      http://improvejava.blogspot.in/     8
Handling Lists
// Demonstrate Lists.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="ListDemo" width=300 height=180>
</applet>
*/
public class ListDemo extends Applet implements ActionListener {
    List os, browser;
    String msg = "";

  public void init() {
       os = new List(4, true);
       browser = new List(4, false);

                        http://improvejava.blogspot.in/            9
Handling Lists                                 contd..

// add items to os list
os.add("Windows 98/XP");
os.add("Windows NT/2000");
os.add("Solaris");
os.add("MacOS");
// add items to browser list
browser.add("Netscape 3.x");
browser.add("Netscape 4.x");
browser.add("Netscape 5.x");
browser.add("Netscape 6.x");
browser.add("Internet Explorer 4.0");
browser.add("Internet Explorer 5.0");
browser.add("Internet Explorer 6.0");


                 http://improvejava.blogspot.in/             10
Handling Lists                            contd..

      browser.add("Lynx 2.4");
      browser.select(1);
      // add lists to window
      add(os);
      add(browser);
      // register to receive action events
      os.addActionListener(this);
      browser.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
       repaint();
}
             http://improvejava.blogspot.in/             11
Handling Lists                                  contd..

    // Display current selections.
    public void paint(Graphics g) {
             int idx[];
             msg = "Current OS: ";
             idx = os.getSelectedIndexes();
             for(int i=0; i<idx.length; i++)
                        msg += os.getItem(idx[i]) + " ";
             g.drawString(msg, 6, 120);
             msg = "Current Browser: ";
             msg += browser.getSelectedItem();
             g.drawString(msg, 6, 140);
    }
}


                      http://improvejava.blogspot.in/             12
Handling Lists                     contd..


• Sample output of ListDemo is shown here




              Fig. 72.1 Output of ListDemo



                 http://improvejava.blogspot.in/        13
Scroll Bars

• Scroll bars are used to select continuous values
  between a specified minimum and maximum
• Scroll bars may be oriented horizontally or
  vertically
• Scroll bars are encapsulated by the Scrollbar
  class




                  http://improvejava.blogspot.in/   14
Scroll Bars                                  contd..

• Scrollbar defines the following constructors
   – Scrollbar( )
   – Scrollbar(int style)
   – Scrollbar(int style, int initialValue, int thumbSize, int min, int max)
• The first form creates a vertical scroll bar
• The second and third forms allow you to specify the
  orientation of the scroll bar
• If style is Scrollbar.VERTICAL, a vertical scroll bar is
  created
• If style is Scrollbar.HORIZONTAL, the scroll bar is
  horizontal


                          http://improvejava.blogspot.in/                 15
Scroll Bars                          contd..


• In the third form of the constructor, the initial
  value of the scroll bar is passed in initialValue
• The number of units represented by the height
  of the thumb is passed in thumbSize
• The minimum and maximum values for the scroll
  bar are specified by min and max
• setValues( )is used to change the parameters
• The syntax of it is
  – void setValues(int initialValue, int thumbSize, int min,
    int max)

                    http://improvejava.blogspot.in/             16
Scroll Bars                         contd..


• The other methods of interest are
  – int getValue( )
  – void setValue(int newValue)
  – int getMinimum( )
  – int getMaximum( )
  – void setUnitIncrement(int newIncr)
  – void setBlockIncrement(int newIncr)


                http://improvejava.blogspot.in/         17
Handling Scroll Bars                          contd..


• To process scroll bar events, you need to
  implement the AdjustmentListener interface
• Example program
// Demonstrate scroll bars.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="SBDemo" width=300 height=200>
</applet>
*/

                      http://improvejava.blogspot.in/             18
Handling Scroll Bars                             contd..
public class SBDemo extends Applet
implements AdjustmentListener, MouseMotionListener {
   String msg = "";
   Scrollbar vertSB, horzSB;
   public void init() {
         int width = Integer.parseInt(getParameter("width"));
         int height = Integer.parseInt(getParameter("height"));
         vertSB = new Scrollbar(Scrollbar.VERTICAL,0, 1, 0, height);
         horzSB = new Scrollbar(Scrollbar.HORIZONTAL,0, 1, 0, width);
         add(vertSB);
         add(horzSB);
         // register to receive adjustment events
         vertSB.addAdjustmentListener(this);
         horzSB.addAdjustmentListener(this);
         addMouseMotionListener(this);
   }
                        http://improvejava.blogspot.in/             19
Handling Scroll Bars                          contd..

public void adjustmentValueChanged(AdjustmentEvent ae) {
     repaint();
}
// Update scroll bars to reflect mouse dragging.
public void mouseDragged(MouseEvent me) {
     int x = me.getX();
     int y = me.getY();
     vertSB.setValue(y);
     horzSB.setValue(x);
     repaint();
}
// Necessary for MouseMotionListener
public void mouseMoved(MouseEvent me) {
}

                    http://improvejava.blogspot.in/             20
Handling Scroll Bars                            contd..

    // Display current value of scroll bars.
    public void paint(Graphics g) {
          msg = "Vertical: " + vertSB.getValue();
          msg += ", Horizontal: " + horzSB.getValue();
          g.drawString(msg, 6, 160);
          // show current mouse drag position
          g.drawString("*", horzSB.getValue(),
          vertSB.getValue());
    }
}




                          http://improvejava.blogspot.in/             21
Handling Lists

• Sample output of SBDemo is shown here




             Fig. 72.1 Output of SBDemo



                http://improvejava.blogspot.in/   22
Summary

• In this class we discussed
  – Lists
  – Scrollbars
  – The related programs




                http://improvejava.blogspot.in/   23
Quiz

1. Is it possible to select multiple items in a List
    – Yes
    – No




                   http://improvejava.blogspot.in/     24
Frequently Asked Questions

1. Explain the different constructors of a List
2. How to add items to the List ?
3. Write different methods of scrollbar




                   http://improvejava.blogspot.in/   25
Ad

More Related Content

What's hot (20)

Java input
Java inputJava input
Java input
Jin Castor
 
Applets in java
Applets in javaApplets in java
Applets in java
Wani Zahoor
 
Event handling
Event handlingEvent handling
Event handling
Shree M.L.Kakadiya MCA mahila college, Amreli
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
ShahDhruv21
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20
myrajendra
 
Javascript arrays
Javascript arraysJavascript arrays
Javascript arrays
Hassan Dar
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
Google
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++
Neeru Mittal
 
Java Notes
Java NotesJava Notes
Java Notes
Abhishek Khune
 
Exception Handling in C#
Exception Handling in C#Exception Handling in C#
Exception Handling in C#
Abid Kohistani
 
ASP.NET Page Life Cycle
ASP.NET Page Life CycleASP.NET Page Life Cycle
ASP.NET Page Life Cycle
Abhishek Sur
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
Indu Sharma Bhardwaj
 
Windowforms controls c#
Windowforms controls c#Windowforms controls c#
Windowforms controls c#
prabhu rajendran
 
4. listbox
4. listbox4. listbox
4. listbox
chauhankapil
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Interface
InterfaceInterface
Interface
kamal kotecha
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Java Threads
Java ThreadsJava Threads
Java Threads
M Vishnuvardhan Reddy
 
Java IO
Java IOJava IO
Java IO
UTSAB NEUPANE
 

Similar to Lists and scrollbars (20)

Online birth certificate system and computer engineering
Online birth certificate system and computer engineeringOnline birth certificate system and computer engineering
Online birth certificate system and computer engineering
sayedshaad02
 
Unit – I-AWT-updated.pptx
Unit – I-AWT-updated.pptxUnit – I-AWT-updated.pptx
Unit – I-AWT-updated.pptx
ssuser10ef65
 
Text field and textarea
Text field and textareaText field and textarea
Text field and textarea
myrajendra
 
Mod_5 of Java 5th module notes for ktu students
Mod_5 of Java 5th module notes for ktu studentsMod_5 of Java 5th module notes for ktu students
Mod_5 of Java 5th module notes for ktu students
heytherenewworld
 
Performing Data Science with HBase
Performing Data Science with HBasePerforming Data Science with HBase
Performing Data Science with HBase
WibiData
 
Labels and buttons
Labels and buttonsLabels and buttons
Labels and buttons
myrajendra
 
Deep Dive Into Swift
Deep Dive Into SwiftDeep Dive Into Swift
Deep Dive Into Swift
Sarath C
 
Menu bars and menus
Menu bars and menusMenu bars and menus
Menu bars and menus
myrajendra
 
02basics
02basics02basics
02basics
Waheed Warraich
 
Twig tips and tricks
Twig tips and tricksTwig tips and tricks
Twig tips and tricks
Javier Eguiluz
 
13 advanced-swing
13 advanced-swing13 advanced-swing
13 advanced-swing
Nataraj Dg
 
JavaScript Robotics
JavaScript RoboticsJavaScript Robotics
JavaScript Robotics
Anna Gerber
 
Class and Object.pptx from nit patna ece department
Class and Object.pptx from nit patna ece departmentClass and Object.pptx from nit patna ece department
Class and Object.pptx from nit patna ece department
om2348023vats
 
Building Single-Page Web Appplications in dart - Devoxx France 2013
Building Single-Page Web Appplications in dart - Devoxx France 2013Building Single-Page Web Appplications in dart - Devoxx France 2013
Building Single-Page Web Appplications in dart - Devoxx France 2013
yohanbeschi
 
Gui
GuiGui
Gui
Sardar Alam
 
Ifi7184 lesson3
Ifi7184 lesson3Ifi7184 lesson3
Ifi7184 lesson3
Sónia
 
AWT stands for Abstract Window Toolkit. AWT is collection of classes and int...
AWT stands for Abstract Window Toolkit.  AWT is collection of classes and int...AWT stands for Abstract Window Toolkit.  AWT is collection of classes and int...
AWT stands for Abstract Window Toolkit. AWT is collection of classes and int...
Prashant416351
 
wwc start-launched
wwc start-launchedwwc start-launched
wwc start-launched
Mat Schaffer
 
PTW Rails Bootcamp
PTW Rails BootcampPTW Rails Bootcamp
PTW Rails Bootcamp
Mat Schaffer
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
Ratnala Charan kumar
 
Online birth certificate system and computer engineering
Online birth certificate system and computer engineeringOnline birth certificate system and computer engineering
Online birth certificate system and computer engineering
sayedshaad02
 
Unit – I-AWT-updated.pptx
Unit – I-AWT-updated.pptxUnit – I-AWT-updated.pptx
Unit – I-AWT-updated.pptx
ssuser10ef65
 
Text field and textarea
Text field and textareaText field and textarea
Text field and textarea
myrajendra
 
Mod_5 of Java 5th module notes for ktu students
Mod_5 of Java 5th module notes for ktu studentsMod_5 of Java 5th module notes for ktu students
Mod_5 of Java 5th module notes for ktu students
heytherenewworld
 
Performing Data Science with HBase
Performing Data Science with HBasePerforming Data Science with HBase
Performing Data Science with HBase
WibiData
 
Labels and buttons
Labels and buttonsLabels and buttons
Labels and buttons
myrajendra
 
Deep Dive Into Swift
Deep Dive Into SwiftDeep Dive Into Swift
Deep Dive Into Swift
Sarath C
 
Menu bars and menus
Menu bars and menusMenu bars and menus
Menu bars and menus
myrajendra
 
13 advanced-swing
13 advanced-swing13 advanced-swing
13 advanced-swing
Nataraj Dg
 
JavaScript Robotics
JavaScript RoboticsJavaScript Robotics
JavaScript Robotics
Anna Gerber
 
Class and Object.pptx from nit patna ece department
Class and Object.pptx from nit patna ece departmentClass and Object.pptx from nit patna ece department
Class and Object.pptx from nit patna ece department
om2348023vats
 
Building Single-Page Web Appplications in dart - Devoxx France 2013
Building Single-Page Web Appplications in dart - Devoxx France 2013Building Single-Page Web Appplications in dart - Devoxx France 2013
Building Single-Page Web Appplications in dart - Devoxx France 2013
yohanbeschi
 
Ifi7184 lesson3
Ifi7184 lesson3Ifi7184 lesson3
Ifi7184 lesson3
Sónia
 
AWT stands for Abstract Window Toolkit. AWT is collection of classes and int...
AWT stands for Abstract Window Toolkit.  AWT is collection of classes and int...AWT stands for Abstract Window Toolkit.  AWT is collection of classes and int...
AWT stands for Abstract Window Toolkit. AWT is collection of classes and int...
Prashant416351
 
wwc start-launched
wwc start-launchedwwc start-launched
wwc start-launched
Mat Schaffer
 
PTW Rails Bootcamp
PTW Rails BootcampPTW Rails Bootcamp
PTW Rails Bootcamp
Mat Schaffer
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
Ratnala Charan kumar
 
Ad

More from myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
myrajendra
 
Data type
Data typeData type
Data type
myrajendra
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
myrajendra
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
myrajendra
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
myrajendra
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
myrajendra
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
myrajendra
 
Dao example
Dao exampleDao example
Dao example
myrajendra
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
myrajendra
 
Internal
InternalInternal
Internal
myrajendra
 
3. elements
3. elements3. elements
3. elements
myrajendra
 
2. attributes
2. attributes2. attributes
2. attributes
myrajendra
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
myrajendra
 
Headings
HeadingsHeadings
Headings
myrajendra
 
Forms
FormsForms
Forms
myrajendra
 
Css
CssCss
Css
myrajendra
 
Views
ViewsViews
Views
myrajendra
 
Views
ViewsViews
Views
myrajendra
 
Views
ViewsViews
Views
myrajendra
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
myrajendra
 
Ad

Lists and scrollbars

  • 2. Objectives On completion of this period, you would be able to know • Lists • Scrollbars http://improvejava.blogspot.in/ 2
  • 3. Recap In the previous class, you have leant • Checkbox and CheckboxGroups • Handling the respective events http://improvejava.blogspot.in/ 3
  • 4. Lists contd.. • The List class provides a compact, multiple- choice, scrolling selection list • It can also be created to allow multiple selections • List provides these constructors – List( ) – List(int numRows) – List(int numRows, boolean multipleSelect) http://improvejava.blogspot.in/ 4
  • 5. Lists contd.. • The first version creates – a List control that allows only one item to be selected at any one time • In the second form – the value of numRows specifies the number of entries in the list that will always be visible • In the third form – if multipleSelect is true, then the user may select two or more items at a time – If it is false, then only one http://improvejava.blogspot.in/ 5 item may be selected
  • 6. Lists contd.. • To add a selection to the list, call add( ) • It has the following two forms – void add(String name) – void add(String name, int index) – Here, name is the name of the item added to the list – The first form adds items to the end of the list – The second form adds the item at the index specified by index – Indexing begins at zero – You can specify –1 to add the item to the end of the list http://improvejava.blogspot.in/ 6
  • 7. Lists contd.. • For lists that allow only single selection you can determine which item is currently selected by calling – either getSelectedItem( ) – or getSelectedIndex( ) – These methods are shown here • String getSelectedItem( ) • int getSelectedIndex( ) http://improvejava.blogspot.in/ 7
  • 8. Lists contd.. • For lists that allow multiple selection you must use – either getSelectedItems( ) – or getSelectedIndexes( ) – They are shown here • String[ ] getSelectedItems( ) • int[ ] getSelectedIndexes( ) • Given an index, you can obtain the name associated with the item at that index by calling – getItem( ) – It has this general form • String getItem(int index) http://improvejava.blogspot.in/ 8
  • 9. Handling Lists // Demonstrate Lists. import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="ListDemo" width=300 height=180> </applet> */ public class ListDemo extends Applet implements ActionListener { List os, browser; String msg = ""; public void init() { os = new List(4, true); browser = new List(4, false); http://improvejava.blogspot.in/ 9
  • 10. Handling Lists contd.. // add items to os list os.add("Windows 98/XP"); os.add("Windows NT/2000"); os.add("Solaris"); os.add("MacOS"); // add items to browser list browser.add("Netscape 3.x"); browser.add("Netscape 4.x"); browser.add("Netscape 5.x"); browser.add("Netscape 6.x"); browser.add("Internet Explorer 4.0"); browser.add("Internet Explorer 5.0"); browser.add("Internet Explorer 6.0"); http://improvejava.blogspot.in/ 10
  • 11. Handling Lists contd.. browser.add("Lynx 2.4"); browser.select(1); // add lists to window add(os); add(browser); // register to receive action events os.addActionListener(this); browser.addActionListener(this); } public void actionPerformed(ActionEvent ae) { repaint(); } http://improvejava.blogspot.in/ 11
  • 12. Handling Lists contd.. // Display current selections. public void paint(Graphics g) { int idx[]; msg = "Current OS: "; idx = os.getSelectedIndexes(); for(int i=0; i<idx.length; i++) msg += os.getItem(idx[i]) + " "; g.drawString(msg, 6, 120); msg = "Current Browser: "; msg += browser.getSelectedItem(); g.drawString(msg, 6, 140); } } http://improvejava.blogspot.in/ 12
  • 13. Handling Lists contd.. • Sample output of ListDemo is shown here Fig. 72.1 Output of ListDemo http://improvejava.blogspot.in/ 13
  • 14. Scroll Bars • Scroll bars are used to select continuous values between a specified minimum and maximum • Scroll bars may be oriented horizontally or vertically • Scroll bars are encapsulated by the Scrollbar class http://improvejava.blogspot.in/ 14
  • 15. Scroll Bars contd.. • Scrollbar defines the following constructors – Scrollbar( ) – Scrollbar(int style) – Scrollbar(int style, int initialValue, int thumbSize, int min, int max) • The first form creates a vertical scroll bar • The second and third forms allow you to specify the orientation of the scroll bar • If style is Scrollbar.VERTICAL, a vertical scroll bar is created • If style is Scrollbar.HORIZONTAL, the scroll bar is horizontal http://improvejava.blogspot.in/ 15
  • 16. Scroll Bars contd.. • In the third form of the constructor, the initial value of the scroll bar is passed in initialValue • The number of units represented by the height of the thumb is passed in thumbSize • The minimum and maximum values for the scroll bar are specified by min and max • setValues( )is used to change the parameters • The syntax of it is – void setValues(int initialValue, int thumbSize, int min, int max) http://improvejava.blogspot.in/ 16
  • 17. Scroll Bars contd.. • The other methods of interest are – int getValue( ) – void setValue(int newValue) – int getMinimum( ) – int getMaximum( ) – void setUnitIncrement(int newIncr) – void setBlockIncrement(int newIncr) http://improvejava.blogspot.in/ 17
  • 18. Handling Scroll Bars contd.. • To process scroll bar events, you need to implement the AdjustmentListener interface • Example program // Demonstrate scroll bars. import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="SBDemo" width=300 height=200> </applet> */ http://improvejava.blogspot.in/ 18
  • 19. Handling Scroll Bars contd.. public class SBDemo extends Applet implements AdjustmentListener, MouseMotionListener { String msg = ""; Scrollbar vertSB, horzSB; public void init() { int width = Integer.parseInt(getParameter("width")); int height = Integer.parseInt(getParameter("height")); vertSB = new Scrollbar(Scrollbar.VERTICAL,0, 1, 0, height); horzSB = new Scrollbar(Scrollbar.HORIZONTAL,0, 1, 0, width); add(vertSB); add(horzSB); // register to receive adjustment events vertSB.addAdjustmentListener(this); horzSB.addAdjustmentListener(this); addMouseMotionListener(this); } http://improvejava.blogspot.in/ 19
  • 20. Handling Scroll Bars contd.. public void adjustmentValueChanged(AdjustmentEvent ae) { repaint(); } // Update scroll bars to reflect mouse dragging. public void mouseDragged(MouseEvent me) { int x = me.getX(); int y = me.getY(); vertSB.setValue(y); horzSB.setValue(x); repaint(); } // Necessary for MouseMotionListener public void mouseMoved(MouseEvent me) { } http://improvejava.blogspot.in/ 20
  • 21. Handling Scroll Bars contd.. // Display current value of scroll bars. public void paint(Graphics g) { msg = "Vertical: " + vertSB.getValue(); msg += ", Horizontal: " + horzSB.getValue(); g.drawString(msg, 6, 160); // show current mouse drag position g.drawString("*", horzSB.getValue(), vertSB.getValue()); } } http://improvejava.blogspot.in/ 21
  • 22. Handling Lists • Sample output of SBDemo is shown here Fig. 72.1 Output of SBDemo http://improvejava.blogspot.in/ 22
  • 23. Summary • In this class we discussed – Lists – Scrollbars – The related programs http://improvejava.blogspot.in/ 23
  • 24. Quiz 1. Is it possible to select multiple items in a List – Yes – No http://improvejava.blogspot.in/ 24
  • 25. Frequently Asked Questions 1. Explain the different constructors of a List 2. How to add items to the List ? 3. Write different methods of scrollbar http://improvejava.blogspot.in/ 25