Download ppt - Lists and scrollbars

Transcript
Page 1: Lists and scrollbars

http://improvejava.blogspot.in/

Lists and Scrollbars

1

Page 2: Lists and scrollbars

http://improvejava.blogspot.in/

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

• Lists• Scrollbars

2

Objectives

Page 3: Lists and scrollbars

http://improvejava.blogspot.in/

Recap

3

In the previous class, you have leant

• Checkbox and CheckboxGroups • Handling the respective events

Page 4: Lists and scrollbars

http://improvejava.blogspot.in/

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)

4

Page 5: Lists and scrollbars

http://improvejava.blogspot.in/

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 item may be selected

5

Page 6: Lists and scrollbars

http://improvejava.blogspot.in/

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

6

Page 7: Lists and scrollbars

http://improvejava.blogspot.in/

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( )

7

Page 8: Lists and scrollbars

http://improvejava.blogspot.in/

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)

8

Page 9: Lists and scrollbars

http://improvejava.blogspot.in/

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);

9

Page 10: Lists and scrollbars

http://improvejava.blogspot.in/

// add items to os listos.add("Windows 98/XP");os.add("Windows NT/2000");os.add("Solaris");os.add("MacOS");// add items to browser listbrowser.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");

Handling Lists contd..

10

Page 11: Lists and scrollbars

http://improvejava.blogspot.in/

browser.add("Lynx 2.4");browser.select(1);// add lists to windowadd(os);add(browser);// register to receive action eventsos.addActionListener(this);browser.addActionListener(this);

}public void actionPerformed(ActionEvent ae) {

repaint();}

Handling Lists contd..

11

Page 12: Lists and scrollbars

http://improvejava.blogspot.in/

// 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);

}}

Handling Lists contd..

12

Page 13: Lists and scrollbars

http://improvejava.blogspot.in/

• Sample output of ListDemo is shown here

Fig. 72.1 Output of ListDemo

Handling Lists contd..

13

Page 14: Lists and scrollbars

http://improvejava.blogspot.in/

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

14

Page 15: Lists and scrollbars

http://improvejava.blogspot.in/

• 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

Scroll Bars contd..

15

Page 16: Lists and scrollbars

http://improvejava.blogspot.in/

• 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)

Scroll Bars contd..

16

Page 17: Lists and scrollbars

http://improvejava.blogspot.in/

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

Scroll Bars contd..

17

Page 18: Lists and scrollbars

http://improvejava.blogspot.in/

• 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>

*/

Handling Scroll Bars contd..

18

Page 19: Lists and scrollbars

http://improvejava.blogspot.in/

public class SBDemo extends Appletimplements 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 eventsvertSB.addAdjustmentListener(this);horzSB.addAdjustmentListener(this);addMouseMotionListener(this);

}

Handling Scroll Bars contd..

19

Page 20: Lists and scrollbars

http://improvejava.blogspot.in/

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 MouseMotionListenerpublic void mouseMoved(MouseEvent me) {}

Handling Scroll Bars contd..

20

Page 21: Lists and scrollbars

http://improvejava.blogspot.in/

// 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 positiong.drawString("*", horzSB.getValue(),vertSB.getValue());

}}

Handling Scroll Bars contd..

21

Page 22: Lists and scrollbars

http://improvejava.blogspot.in/

• Sample output of SBDemo is shown here

Fig. 72.1 Output of SBDemo

Handling Lists

22

Page 23: Lists and scrollbars

http://improvejava.blogspot.in/

Summary

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

23

Page 24: Lists and scrollbars

http://improvejava.blogspot.in/

Quiz

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

24

Page 25: Lists and scrollbars

http://improvejava.blogspot.in/

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

25


Recommended