Danh mục

Bài giảng Lập trình Java: Buổi 5 - Industrial university of Ho Chi Minh City

Số trang: 22      Loại file: pdf      Dung lượng: 1.22 MB      Lượt xem: 9      Lượt tải: 0    
Thư viện của tui

Xem trước 3 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Bài giảng "Lập trình Java - Graphic user interface in Java" trình bày các nội dung: JList, JTable, JTree, JSplitPane, Jslider, MDI - multiple document interface. Mời các bạn cùng tham khảo nội dung chi tiết.
Nội dung trích xuất từ tài liệu:
Bài giảng Lập trình Java: Buổi 5 - Industrial university of Ho Chi Minh City1/3/201605. Graphic User Interface in JavaFaculty of Information TechnologiesIndustrial University of Ho Chi Minh City1GUI components (p4)JListJTableJTreeJSplitPaneJsliderMDI - multiple document interface2JList•Purposeo•To present a list of items from which the user can chooseBehavioroItems in JList can be selected individually or in a groupoA JList does not provide support for double-click action311/3/2016JList – Constructors•JList()o•Constructs a JList with an empty modelJList( Object[] listData )oDisplays the elements of the specified arrayoExample:String[] words= { quick, brown, hungry, wild, ... };JList wordList = new JList(words);•JList ( ListModel dataModel )oDisplays the elements in the specified, non-null list model4JList – Methods••int getSelectedIndex()void setSelectedIndex(int index)o•o•returns the selected values or an empty array if the selection is emptyboolean isSelectedIndex(int index)o•returns the first selected value or null if the selection is emptyObject[] getSelectedValues()o•gets or sets the selected indexObject getSelectedValue()returns true if the specified index is selectedboolean isSelectionEmpty()oreturns true if no item is currently selected5JList – Methods (contd.)••int getVisibleRowCount()void setVisibleRowCount( int height )oget or set the number of rows in the list that can be displayedwithout a scroll bar••int getSelectionMode()void setSelectionMode( int mode )oListSelectionModel.SINGLE_SELECTION,ListSelectionModel.SINGLE_INTERVAL_SELECTION,ListSelectionModel.MULTIPLE_INTERVAL_SELECTION,by default, a user can select multiple items621/3/2016Handle event of JList•When the current selection changes, JList objectgenerates a ListSelection eventoMethod:public void valueChanged (ListSelectionEvent e) {Object value = list.getSelectedValue();//do something with value}public void valueChanged (ListSelectionEvent e) {Object[] items = list.getSelectedValues();for (Object value : items)//do something with value}7JList with fixed set of choices•Build JList:oThe simplest way to use a JList is to supply an array of strings tothe JList constructor. Cannot add or remove elements once theJList is createdString options = { Option 1, ... , Option N};JList optionList = new JList(options);•Set visible rowsoCall setVisibleRowCount and drop JList into JScrollPaneoptionList.setVisibleRowCount(4);JScrollPane scrolList = new JScrollPane(optionList);someContainer.add(scrolList);8JList DemoString[] entries = { Entry 1, Entry 2, Entry 3,Entry 4, Entry 5, Entry 6 };JList lstEntry;lstEntry = new JList(entries);lstEntry.setVisibleRowCount(4);JScrollPane listPane = new JScrollPane(lstEntry);JPanel pCen = new JPanel();pCen.setBorder(BorderFactory.createTitledBorder(Simple JList));pCen.add(listPane);add(pCen, BorderLayout.CENTER);9931/3/2016JList Demo (contd.)public void valueChanged(ListSelectionEvent e){txtSelected.setText(lstEntry.getSelectedValue().toString());}10Editing JList•To add or remove elements, you must access theListModel•ListModel is an interface. How do you obtain it?oConstructing your own list by creating a class thatimplements the ListModel interfaceoUsing a DefaultListModel class11JList with changeable choices•Build JList:oCreate a DefaultListModel, add data, pass toconstructor:DefaultListModel sampleModel = new DefaultListModel();JList optionList = new JList(sampleModel);•Set visible rowso•Same: Use setVisibleRowCount and a JScrollPaneAdd/remove elementsoUse the model, not the JList directly1241/3/2016Methods in DefaultListModel•void addElement(Object obj)o•adds object to the end of the modelboolean removeElement(Object obj)oremoves the first occurrence of the object from the model. Returntrue if the object was contained in the model, false otherwise•void setElementAt(Object item, int index)o•o•sets item at indexObject getElementAt(int position)returns an element of the model at the given positionint getSize()oreturns the number of elements of the model13Traverse DefaultListModel•To traverse all elements of the model, using:Enumeration e = listmodel.elements();while (e.hasMoreElements()){Object o = e.nextElement();// process o}14JList Demo (edittable)public class ListEditDemo extends JFrame implements ActionListener {JButton btnAdd, btnRemove;JTextField txtName;DefaultListModel listmodelName;JList listName;public ListEditDemo() {super(List Edit Demo);// listlistmodelName = new DefaultListModel();listName = new JList(listmodelName);add(new JScrollPane(listName), BorderLayout.CENTER);155 ...

Tài liệu được xem nhiều: