GUI Component - 4
JList Displays a series of items The user can select one or more items Class JList extends directly class Jcomponent Class Jlist supports Single-selection lists: one item to be selected at a time Multiple-selection lists: any number of items can be selected(ctrl,shift)
Jlist - event ListSelectionEvent Handled by ListSelectionListener that should implement the method valuechanged
JList Does not provide a scrollbar??!! The scrollbar can be provided using JScrollPane where the scrollbar appears at the right side of Jlist The scrollbar appears only when the number of items in the list exceeds the number of visible items. To register add the method: addlistselectionlistener
Constructors JList() Constructs an empty list Jlist(object[] listdata) Constructs a list that contains the data in listdata Example: private String size[]={ 12, 14, 16, 18, 20, 22 }; list1= new Jlist(size);
Methods void setvisiblerowcount(int visiblerowcount) Sets the number of rows that can be visible void setselectionmode(int selectionmode) Determines whether single or multiple items can be selected: ListSelectionModel.SINGLE_SELECTION Only one list index can be selected at a time. ListSelectionModel.SINGLE_INTERVAL_SELECTION One contiguous index interval can be selected at a time. ListSelectionModel.MULTIPLE_INTERVAL_SELECTION In this mode, there's no restriction on what can be selected. int getselectionindex() Returns the first selected index, -1 if nothing is selected void setfixedcellhieght(int height) Sets the heights of every cell in the list (in pixels) void setfixedcellwidth(int width) Sets the width of every cell in the list
Methods void setselectedindex(int x) Sets the selected index to x void setlistdata(object[] listdata) Constructs a ListModel from an array of objects object getselectedvalue() Returns the first selected value, null if no selection object getselectedvalues() Returns an array of the values for the selected cells void valuchanged(listselectionevent e) Called whenever the value of the selection changed
NOTE There are no events to indicate that a user has made multiple selections in a multipleselection list. Another GUI componenet should be used to specify when the multiple selection should be processed (known as external event)
JTextArea Area to manipulate multiple lines of text Subclass of JTextComponent No action event: an external event indicates when to process the text. No scrollbar No wrap lines.
Constructors JTextArea() JTextArea (int rows, int cols) New empty text area with the specified number of row and columns JTextArea(String text) New text area with the specified text JTextArea(String text, int rows, int cols) New text area with specified text and no. of cols and rows.
Methods void setlinewrap(boolean wrap) String getselectedtext() void settext(string text) void setreditable(boolean b) String gettext() void append(string s)
JComboBox List of items from which the user can make a single selection Also called a drop-down list JComboBox provides a scrollbar that allow the user to scroll through all the elements For each item selected from a JcomboBox, another item is unselected
Constructor JComboBox(object[] items) Creates a JComboBox that contains the elements in the specified array.
Methods void additemlistener(itemlistener a) void setmaximumrowcount(int count) Sets the maximum number of rows. int getselectedindex() Returns the first item in the list that matches the given item object getselecteditem() Return the current selected item int getstatechange() Returns the type of change (selected or not)
Quiz 10 min If you read this before the class you can solve the quiz and bring the solution to the class tomorrow. Write java statements that are needed to do the following: Constructs a list that contains: A, B, C, D, E, F, G For the previous list write a statement to: Determine that the number of item to be showed is 4 Allow the user to select any number of items Add scrollbar Show how the action will be performed when the user select more than one item