2011-12-03 41 views
11

Tôi muốn thêm một số cột vào một bảng (Swing JTable). Một số người trong số họ sẽ có kích thước mặc định (ví dụ: 250), một số khác sẽ bị ẩn (vì vậy kích thước của chúng sẽ là 0). Tôi sử dụng mã này:JTable ẩn và hiển thị các cột

model = new DefaultTableModel(); 
table = new JTable(model); 
setAutoResizeMode(AUTO_RESIZE_OFF); 
for (int i = 1; i < COLUMN_NAMES.length; i++) { 
    model.addColumn(COLUMN_NAMES[i]); 
    if (show[i]) show(index); 
    else hide(index); 
} 
........ 

private void hide(int index) { 
    TableColumn column = getColumnModel().getColumn(index); 
    column.setMinWidth(0); 
    column.setMaxWidth(0); 
    column.setWidth(0); 
    column.setPreferredWidth(0); 
    doLayout(); 
} 

private void show(int index) { 
    final int width = 250; 
    column.setMinWidth(15); 
    column.setMaxWidth(width); 
    column.setWidth(width); 
    column.setPreferredWidth(width); 
    doLayout(); 
} 

vấn đề là khi bảng được hiển thị, tất cả các cột được hiển thị (không có ẩn) và kích thước của chúng không phải là 250 nhưng chúng có cùng kích thước.

Làm cách nào để có được hiệu ứng mong muốn?

+3

Vui lòng xem câu trả lời của Swing-genius StanislavL tại đây: [hide-column-in-jtable-temporary] (http://stackoverflow.com/questions/5270032/hide-column-in-jtable-temporary) –

+0

liên kết tốt đẹp :) +1 – mprabhat

Trả lời

11

JTable#removeColumn remove Cột chỉ từ JTable xem, ở đây example

+1

cảm ơn, nó không phải là giải pháp hoàn hảo cos nó bỏ lỡ vị trí đã ra lệnh trong ví dụ nhưng tôi đã sửa nó. – Randomize

13

Tôi nghĩ rằng bạn đã đặt tên cho phương pháp của bạn không đúng cách, khi bạn muốn ẩn một cột bạn sẽ thiết lập

column.setMinWidth(0); 
column.setMaxWidth(0); 
column.setPreferredWidth(0); 

nhưng trong mã của bạn, bạn đang làm điều này khi bạn muốn hiển thị một cột đó là hoàn toàn ngược lại.

Cũng không cần phải gọi phương thức "setWidth" này trên BảngColumn, đọc TableColumn#setWidth(int).

7

thay vì tái phát minh ra bánh xe bạn có thể cân nhắc sử dụng JXTable (trong the SwingX project) mà hỗ trợ cột ẩn, comlete với một giao diện người dùng điều khiển để cho phép người dùng ẩn/hiển thị chúng động - và một loạt các điều hữu ích khác :).

0
HashMap<String,TableColumn> hashMap_columns = new HashMap<String,TableColumn>(); 

    DefaultTableColumnModel defaultTableColumnModel = (DefaultTableColumnModel)jtable.getColumnModel(); 

    Enumeration<TableColumn> enumeration = defaultTableColumnModel.getColumns(); 

    while (enumeration.hasMoreElements()) 
    { 
      TableColumn tableColumn = enumeration.nextElement(); 

      hashMap_columns.put((String)tableColumn.getIdentifier(),tableColumn); 

    } 


    public void setColumnVisible(String identifier, boolean setVisible) 
    { 
      TableColumn tableColumn = hashMap_columns.get(identifier); 

      if (setVisible) 
      { 
       // using a sorted map removes the need to check column index/position 
       SortedMap<Integer,TableColumn> sortedMap = new TreeMap<Integer,TableColumn>(); 

       // retreive all visible columns 
       Enumeration<TableColumn> enumeration = defaultTableColumnModel.getColumns(); 

       while (enumeration.hasMoreElements()) 
       { 
        TableColumn column = enumeration.nextElement(); 

        sortedMap.put(column.getModelIndex(),column); 
       } 

       // add the column of interest to the sorted map 
       sortedMap.put(tableColumn.getModelIndex(),tableColumn); 

       // remove all visible columns 
       for (TableColumn column: sortedMap.values()) 
       { 
        defaultTableColumnModel.removeColumn(column); 
       } 

       // add all previously visible columns as well as the column of interest 
       for (TableColumn column: sortedMap.values()) 
       { 
        defaultTableColumnModel.addColumn(column); 
       } 
      } 
      else 
      { 
       defaultTableColumnModel.removeColumn(tableColumn); 
      } 
     } 
6

thử một cái gì đó như thế này này ví dụ:

myTableModel = new DefaultTableModel(); 
myTableModel.setColumnIdentifiers(new Object[]{"ID", "Name"}); 
JTable myTable = new JTable(myTableModel); 

// remember to save the references 
TableColumn myTableColumn0 = guiLoteryNumbersTable.getColumnModel().getColumn(0); 
TableColumn myTableColumn1 = guiLoteryNumbersTable.getColumnModel().getColumn(1); 
//... 

// remove temporary the column ("hide") 
myTable.getColumnModel().removeColumn(myTableColumn1); 

// then you restore that column when you need it ("show") 
myTable.getColumnModel().addColumn(myTableColumn1); 

Đó là cách tốt nhất mà tôi biết để ẩn một cột.

+0

Xin chào các bạn, đừng bỏ phiếu cho tôi nếu bạn không nói tại sao câu trả lời của tôi là sai. Đăng một câu trả lời tương tự cho một câu hỏi rất giống nhau và nó đã được bình chọn. Cách của tôi hoạt động tốt anyway, chỉ cần thử nó. – august0490

+0

Rất có thể những người bị bỏ rơi là do câu trả lời trễ 3 năm. Điều đó có thể là tốt, nhưng không phải khi câu trả lời chỉ là sự xây dựng câu trả lời được chấp nhận 3 năm tuổi. – GKFX

Các vấn đề liên quan