intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

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

Chia sẻ: Bình Yên | Ngày: | Loại File: PDF | Số trang:22

48
lượt xem
3
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

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.

Chủ đề:
Lưu

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

1/3/2016<br /> <br /> 05. Graphic User Interface in Java<br /> <br /> Faculty of Information Technologies<br /> Industrial University of Ho Chi Minh City<br /> 1<br /> <br /> GUI components (p4)<br /> JList<br /> JTable<br /> JTree<br /> <br /> JSplitPane<br /> Jslider<br /> MDI - multiple document interface<br /> 2<br /> <br /> JList<br /> <br /> •<br /> <br /> Purpose<br /> o<br /> <br /> •<br /> <br /> To present a list of items from which the user can choose<br /> <br /> Behavior<br /> o<br /> <br /> Items in JList can be selected individually or in a group<br /> <br /> o<br /> <br /> A JList does not provide support for double-click action<br /> <br /> 3<br /> <br /> 1<br /> <br /> 1/3/2016<br /> <br /> JList – Constructors<br /> <br /> •<br /> <br /> JList()<br /> o<br /> <br /> •<br /> <br /> Constructs a JList with an empty model<br /> <br /> JList( Object[] listData )<br /> o<br /> <br /> Displays the elements of the specified array<br /> <br /> o<br /> <br /> Example:<br /> String[] words= { "quick", "brown", "hungry", "wild", ... };<br /> JList wordList = new JList(words);<br /> <br /> •<br /> <br /> JList ( ListModel dataModel )<br /> o<br /> <br /> Displays the elements in the specified, non-null list model<br /> 4<br /> <br /> JList – Methods<br /> •<br /> •<br /> <br /> int getSelectedIndex()<br /> void setSelectedIndex(int index)<br /> o<br /> <br /> •<br /> <br /> o<br /> <br /> •<br /> <br /> returns the selected values or an empty array if the selection is empty<br /> <br /> boolean isSelectedIndex(int index)<br /> o<br /> <br /> •<br /> <br /> returns the first selected value or null if the selection is empty<br /> <br /> Object[] getSelectedValues()<br /> o<br /> <br /> •<br /> <br /> gets or sets the selected index<br /> <br /> Object getSelectedValue()<br /> <br /> returns true if the specified index is selected<br /> <br /> boolean isSelectionEmpty()<br /> o<br /> <br /> returns true if no item is currently selected<br /> 5<br /> <br /> JList – Methods (contd.)<br /> <br /> •<br /> •<br /> <br /> int getVisibleRowCount()<br /> void setVisibleRowCount( int height )<br /> o<br /> <br /> get or set the number of rows in the list that can be displayed<br /> without a scroll bar<br /> <br /> •<br /> •<br /> <br /> int getSelectionMode()<br /> void setSelectionMode( int mode )<br /> o<br /> <br /> ListSelectionModel.SINGLE_SELECTION,<br /> ListSelectionModel.SINGLE_INTERVAL_SELECTION,<br /> ListSelectionModel.MULTIPLE_INTERVAL_SELECTION,<br /> by default, a user can select multiple items<br /> 6<br /> <br /> 2<br /> <br /> 1/3/2016<br /> <br /> Handle event of JList<br /> <br /> •<br /> <br /> When the current selection changes, JList object<br /> generates a ListSelection event<br /> o<br /> <br /> Method:<br /> public void valueChanged (ListSelectionEvent e) {<br /> Object value = list.getSelectedValue();<br /> //do something with value<br /> }<br /> public void valueChanged (ListSelectionEvent e) {<br /> Object[] items = list.getSelectedValues();<br /> for (Object value : items)<br /> //do something with value<br /> }<br /> 7<br /> <br /> JList with fixed set of choices<br /> <br /> •<br /> <br /> Build JList:<br /> o<br /> <br /> The simplest way to use a JList is to supply an array of strings to<br /> the JList constructor. Cannot add or remove elements once the<br /> JList is created<br /> String options = { "Option 1", ... , "Option N"};<br /> JList optionList = new JList(options);<br /> <br /> •<br /> <br /> Set visible rows<br /> o<br /> <br /> Call setVisibleRowCount and drop JList into JScrollPane<br /> optionList.setVisibleRowCount(4);<br /> JScrollPane scrolList = new JScrollPane(optionList);<br /> someContainer.add(scrolList);<br /> 8<br /> <br /> JList Demo<br /> String[] entries = { "Entry 1", "Entry 2", "Entry 3",<br /> "Entry 4", "Entry 5", "Entry 6" };<br /> JList lstEntry;<br /> lstEntry = new JList(entries);<br /> lstEntry.setVisibleRowCount(4);<br /> <br /> JScrollPane listPane = new JScrollPane(lstEntry);<br /> JPanel pCen = new JPanel();<br /> pCen.setBorder(BorderFactory.createTitledBorder("Simple JList"));<br /> pCen.add(listPane);<br /> add(pCen, BorderLayout.CENTER);<br /> 99<br /> <br /> 3<br /> <br /> 1/3/2016<br /> <br /> JList Demo (contd.)<br /> public void valueChanged(ListSelectionEvent e)<br /> {<br /> txtSelected.setText(lstEntry.getSelectedValue().toString());<br /> <br /> }<br /> <br /> 10<br /> <br /> Editing JList<br /> <br /> •<br /> <br /> To add or remove elements, you must access the<br /> ListModel<br /> <br /> •<br /> <br /> ListModel is an interface. How do you obtain it?<br /> o<br /> <br /> Constructing your own list by creating a class that<br /> implements the ListModel interface<br /> <br /> o<br /> <br /> Using a DefaultListModel class<br /> <br /> 11<br /> <br /> JList with changeable choices<br /> <br /> •<br /> <br /> Build JList:<br /> o<br /> <br /> Create a DefaultListModel, add data, pass to<br /> constructor:<br /> DefaultListModel sampleModel = new DefaultListModel();<br /> JList optionList = new JList(sampleModel);<br /> <br /> •<br /> <br /> Set visible rows<br /> o<br /> <br /> •<br /> <br /> Same: Use setVisibleRowCount and a JScrollPane<br /> <br /> Add/remove elements<br /> o<br /> <br /> Use the model, not the JList directly<br /> 12<br /> <br /> 4<br /> <br /> 1/3/2016<br /> <br /> Methods in DefaultListModel<br /> <br /> •<br /> <br /> void addElement(Object obj)<br /> o<br /> <br /> •<br /> <br /> adds object to the end of the model<br /> <br /> boolean removeElement(Object obj)<br /> o<br /> <br /> removes the first occurrence of the object from the model. Return<br /> <br /> true if the object was contained in the model, false otherwise<br /> <br /> •<br /> <br /> void setElementAt(Object item, int index)<br /> o<br /> <br /> •<br /> <br /> o<br /> <br /> •<br /> <br /> sets item at index<br /> <br /> Object getElementAt(int position)<br /> returns an element of the model at the given position<br /> <br /> int getSize()<br /> o<br /> <br /> returns the number of elements of the model<br /> 13<br /> <br /> Traverse DefaultListModel<br /> <br /> •<br /> <br /> To traverse all elements of the model, using:<br /> Enumeration e = listmodel.elements();<br /> while (e.hasMoreElements())<br /> {<br /> Object o = e.nextElement();<br /> // process o<br /> }<br /> <br /> 14<br /> <br /> JList Demo (edittable)<br /> public class ListEditDemo extends JFrame implements ActionListener {<br /> JButton btnAdd, btnRemove;<br /> JTextField txtName;<br /> DefaultListModel listmodelName;<br /> JList listName;<br /> public ListEditDemo() {<br /> <br /> super("List Edit Demo");<br /> // list<br /> listmodelName = new DefaultListModel();<br /> listName = new JList(listmodelName);<br /> add(new JScrollPane(listName), BorderLayout.CENTER);<br /> 15<br /> <br /> 5<br /> <br />
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2