![]() ![]() ![]() You can set a list's model when you create the list or by calling the setModel method. To create a list whose items can be changed individually, set the list's model to an instance of a mutable list model class, such as an instance ofĭefaultListModel. The default list model is immutable you cannot add, remove, or replace items in the list. If you initialize a list with an array or vector, the constructor implicitly creates a default list model. Other JList constructors let you initialize a list from a Vector or from an object that adheres to the In our example, the strings happen to be boys' names. The array is filled with strings that were passed in from another object. The code passes an array to the list's constructor. JScrollPane listScroller = new JScrollPane(list) tLayoutOrientation(JList.HORIZONTAL_WRAP) tSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION) List = new JList(data) //data has type Object ![]() ListDialog.java that creates and sets up its list: For this approach, you must subclass AbstractListModel and implement the getSize and getElementAt methods inherited from the ListModel interface. AbstractListModel - you manage the data and invoke the "fire" methods.The examples in this page use DefaultListModel. DefaultListModel - everything is pretty much taken care of for you.There are three ways to create a list model: Adding Items to and Removing Items from a List.This rest of this section discusses the following topics: In ListDemo, try adding (hiring) and removing (firing) a few items.The resulting dialog is a ListDialog instance that has been customized to have the title Name Chooser. button in the window titled Name That Baby. To bring up the ListDialog, click the Pick a new name.Alternatively, to compile and run the example yourself, consult the example index. Click the Launch button to run ListDialogRunner.Java™ Web Start ( download JDK 7 or later). Click the Launch button to run ListDemo using. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |