tailieunhanh - Beginning Java SE 6 Platform From Novice to Professional phần 4

Nó rất dễ dàng để giới thiệu phân loại vào một thành phần bảng. Sau khi tạo mô hình của bảng và khởi tạo các thành phần bảng với mô hình này, thông qua các mô hình để TableRowSorter '. Sau đó vượt qua RowSorter setRowSorter khoảng trống công cộng của JTable | 130 CHAPTER 4 GUI TOOLKITS SWING Its abstract M I subclass which supports sorting and filtering around a grid-based data model The DefaultRowSorter M I s M extends TableModel subclass which provides table component sorting and filtering via It is easy to introduce sorting to a table component. After creating the table s model and initializing the table component with this model pass the model to TableRowSorter M extends TableModel s constructor. Then pass the resulting RowSorter M to JTable s public void setRowSorter RowSorter extends TableModel sorter method TableModel model . JTable table new JTable model RowSorter TableModel sorter new TableRowSorter TableModel model sorter To demonstrate how easy it is to add sorting to your tables I have designed a simple application that itemizes some grocery items and their prices in a two-column table. Listing 4-3 presents the source code. Listing 4-3. import . import . public class PriceList1 extends JFrame public PriceList1 String title super title setDefaultCloseOperation EXIT_ON_CLOSE String columns Item Price Object rows Bag of potatoes Magazine CHAPTER 4 GUI TOOLKITS SWING 131 Can of soup DVD movie TableModel model new DefaultTableModel rows columns JTable table new JTable model RowSorter TableModel sorter new TableRowSorter TableModel model sorter getContentPane .add new JScrollPane table setSize 200 150 setVisible true public static void main String args Runnable r new Runnable public void run new PriceList1 Price List 1 r Run this application and you will see output similar to the table shown in Figure 4-3. Figure 4-3. Unsorted table Click the Item column s header and the rows will sort in ascending order of this column s values. A small up triangle will appear beside the .

crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.