lop jlist trong java swing

Ngày đăng: 02/12/2017, 21:42

http://vietjack.com/java_swing/index.jsp Copyright © vietjack.com Lớp JList Java Swing Lớp JList thành phần mà hiển thị danh sách đối tượng cho phép người dùng lựa chọn nhiều item Một Model riêng rẽ, ListModel, trì nội dung list Cú pháp khai báo lớp javax.swing.JList là: public class JList extends JComponent implements Scrollable, Accessible Lớp kế thừa phương thức từ lớp sau:  javax.swing.JComponent  java.awt.Container  java.awt.Component  java.lang.Object Lớp JList có trường sau:  static int HORIZONTAL_WRAP: Trình bày layout theo phong cách tờ báo (“newspaper style”) với ô tràn theo chiều ngang sau chiều dọc  static int VERTICAL: Chỉ cách bố trí theo chiều dọc, cột đơn; layout mặc định  static int VERTICAL_WRAP: Chỉ layout theo phong cách tờ báo “newspaper style” với ô tràn theo chiều dọc sau chiều ngang Các constructor lớp JList Java Swing JList(): Xây dựng JList với model empty, read-only JList(ListModel dataModel): Xây dựng JList mà hiển thị phần tử từ model cho nonnull JList(Object[] listData): Xây dựng JList mà hiển thị phần tử mảng cho JList(Vector listData): Xây dựng JList mà hiển thị phần tử Vector cho http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java_swing/index.jsp Copyright © vietjack.com Các phương thức lớp JList Java Swing STT Phương thức & Miêu tả void addListSelectionListener(ListSelectionListener listener) Thêm Listener tới list, để thông báo xuất thay đổi tới selection; cách ưu tiên để nghe trạng thái thay đổi void addSelectionInterval(int anchor, int lead) Thiết lập selection thành kết hợp khoảng interval cho với selection void clearSelection() Xóa selection sau gọi phương thức này, isSelectionEmpty trả true protected ListSelectionModel createSelectionModel() Trả instance DefaultListSelectionModel; gọi xây dựng để thuyết minh thuộc tính selection model list void ensureIndexIsVisible(int index) Cuộn danh sách bên viewport bao quanh để làm cho cho hồn tồn nhìn thấy protected void fireSelectionValueChanged(int firstIndex, int lastIndex, boolean isAdjusting) Thông báo cho ListSelectionListeners để thêm trực tiếp tới danh sách thay đổi selection tạo tới selection model AccessibleContext getAccessibleContext() Lấy AccessibleContext liên kết với JList http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java_swing/index.jsp Copyright © vietjack.com int getFirstVisibleIndex() Trả mục nhỏ list mà nhìn thấy int getLastVisibleIndex() Trả mục lớn list mà nhìn thấy 10 Dimension getPreferredScrollableViewportSize() Tính tốn kích cỡ viewport cần thiết để hiển thị hàng visibleRowCount 11 int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) Trả khoảng cách để để để trưng bày hàng trước khối (với theo chiều dọc) cột (với theo chiều ngang) 12 void removeListSelectionListener(ListSelectionListener listener) Xóa selection listener từ list 13 void removeSelectionInterval(int index0, int index1) Thiết lập selection để thiết lập khác interval cho selection 14 void setDragEnabled(boolean b) Tắt bật xử lý hoạt động drag mặc định 15 void setDropMode(DropMode dropMode) Thiết lập drop mode cho thành phần 16 void setFixedCellHeight(int height) Thiết lập giá trị cố định để sử dụng cho chiều cao ô list http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java_swing/index.jsp 17 Copyright © vietjack.com void setLayoutOrientation(int layoutOrientation) Định nghĩa cách ô list bố trí 18 void setListData(Object[] listData) Xây dựng read-only ListModel từ mảng đối tượng gọi setModel với model 19 void setListData(Vector listData) Xây dựng read-only ListModel từ Vector gọi setModel với model 20 void setModel(ListModel model) Thiết lập model mà biểu diễn nội dung “value” list, thơng báo thay đổi thuộc tính tới listener, sau xóa selection list 21 void setPrototypeCellValue(Object prototypeCellValue) Thiết lập thuộc tính prototypeCellValue, sau (nếu giá trị non-null) tính tốn thuộc tính fixedCellWidth fixedCellHeight yêu cầu thành phần cell renderer cho giá trị cho (và mục 0) từ cell renderer đó, sử dụng kích cỡ thành phần 22 void setSelectedIndex(int index) Lựa chọn đơn 23 void setSelectedIndices(int[] indices) Thay đổi selection thành tập hợp mục xác định mảng cho 24 void setSelectedValue(Object anObject, boolean shouldScroll) Thiết lập đối tượng cho từ list http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java_swing/index.jsp 25 Copyright © vietjack.com void setSelectionBackground(Color selectionBackground) Thiết lập màu sử dụng để vẽ màu background item chọn, mà Call Renderer sử dụng để điền vào ô chọn 26 void setSelectionForeground(Color selectionForeground) Thiết lập màu sử dụng để vẽ foreground item chọn, mà Call Renderer sử dụng để truyền text đồ họa 27 void setSelectionInterval(int anchor, int lead) Thiết lập interval cho 28 void setSelectionMode(int selectionMode) Thiết lập selection mode cho list 29 void setSelectionModel(ListSelectionModel selectionModel) Thiết lập selectionModel cho list tới trình triển khai non-null ListSelectionModel 30 void setUI(ListUI ui) Thiết lập ListUI, đối tượng L&F mà truyền đối tượng 31 void setValueIsAdjusting(boolean b) Thiết lập thuộc tính valueIsAdjusting selection model 32 void setVisibleRowCount(int visibleRowCount) Thiết lập thuộc tính visibleRowCount, mà có ý nghĩa khác phụ thuộc vào hướng bố trí layout orientation: Với hướng bố trí VERTICAL, phương thức thiết lập số hàng ưu tiên để hiển thị (không yêu cầu cuốn); với hướng khác, phương thức tác động đến việc bao ô http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java_swing/index.jsp Copyright © vietjack.com void updateUI() 33 Phục hồi thuộc tính ListUI thiết lập tới giá trị cung cấp L&F Chương trình ví dụ lớp JList package com.vietjack.gui; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingControlDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingControlDemo(){ prepareGUI(); } public static void main(String[] args){ SwingControlDemo swingControlDemo = new SwingControlDemo(); swingControlDemo.showListDemo(); } private void prepareGUI(){ mainFrame = new JFrame(“Vi du Java Swing”); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java_swing/index.jsp Copyright © vietjack.com System.exit(0); } }); headerLabel = new JLabel(“”, JLabel.CENTER); statusLabel = new JLabel(“”,JLabel.CENTER); statusLabel.setSize(350,100); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showListDemo(){ headerLabel.setText(“Control in action: JList”); final DefaultListModel fruitsName = new DefaultListModel(); fruitsName.addElement(“Apple”); fruitsName.addElement(“Grapes”); fruitsName.addElement(“Mango”); fruitsName.addElement(“Peer”); final JList fruitList = new JList(fruitsName); fruitList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); fruitList.setSelectedIndex(0); fruitList.setVisibleRowCount(3); http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java_swing/index.jsp Copyright © vietjack.com JScrollPane fruitListScrollPane = new JScrollPane(fruitList); final DefaultListModel vegName = new DefaultListModel(); vegName.addElement(“Lady Finger”); vegName.addElement(“Onion”); vegName.addElement(“Potato”); vegName.addElement(“Tomato”); final JList vegList = new JList(vegName); vegList.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); vegList.setSelectedIndex(0); vegList.setVisibleRowCount(3); JScrollPane vegListScrollPane = new JScrollPane(vegList); JButton showButton = new JButton(“Show”); showButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String data = “”; if (fruitList.getSelectedIndex() != -1) { data = “Fruits Selected: ” + fruitList.getSelectedValue(); statusLabel.setText(data); } if(vegList.getSelectedIndex() != -1){ data += ” Vegetables selected: “; for(Object vegetable:vegList.getSelectedValues()){ data += vegetable + ” “; } http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java_swing/index.jsp Copyright © vietjack.com } statusLabel.setText(data); } }); controlPanel.add(fruitListScrollPane); controlPanel.add(vegListScrollPane); controlPanel.add(showButton); mainFrame.setVisible(true); } } http://vietjack.com/ Trang chia sẻ học online miễn phí Page … main(String[] args){ SwingControlDemo swingControlDemo = new SwingControlDemo(); swingControlDemo.showListDemo(); } private void prepareGUI(){ mainFrame = new JFrame(“Vi du Java Swing” ); mainFrame.setSize(400,400);… java. awt.event.*; import javax .swing. *; public class SwingControlDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingControlDemo(){…http://vietjack.com /java_ swing/ index.jsp Copyright © vietjack.com Các phương thức lớp JList Java Swing STT Phương thức & Miêu tả void addListSelectionListener(ListSelectionListener

– Xem thêm –

Xem thêm: lop jlist trong java swing,