2012-10-24 152 views
1

我是Swing,UI和MVC的新手 我已經創建了一個基於MVC的代碼。現在我的問題是,在控制器部分,我有一個actioneventlistener,它可以監聽不同的按鈕點擊。在所有這些按鈕中,我有「全部選擇」和「全部取消選擇」。在我看來,我有一個表格,該表格的其中一列包含「複選框」。現在,當我點擊「全選」按鈕時,我想檢查所有複選框,並選擇「全部取消」,我想取消全部選中。如何點擊一個按鈕檢查所有複選框

下面是我的代碼不工作。請告訴我我在這裏做錯了什麼。另外,如果有人知道更加靈活的方式,請分享。由於

在我看來

public class CustomerSelectorDialogUI extends JFrame{ 

    public CustomerSelectorDialogUI(TestApplicationUI ownerView, DummyCustomerStore dCStore, boolean modality) { 

    //super(ownerView, modality); 
    setTitle("[=] Customer Selection Dialog [=]"); 
    //setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
    custSelectPanel = new JPanel(); 
    buttonPanel = new JPanel(); 
    selectAllButton = new JButton(" Select All "); 
    clearAllButton = new JButton(" Clear All "); 
    applyButton = new JButton(" Apply "); 
    cancelButton = new JButton(" Cancel "); 

    PopulateAndShow(dCStore, Boolean.FALSE); 
} 

public void PopulateAndShow(DummyCustomerStore dCStore, Boolean select) { 
    List data = new ArrayList(); 
    for (Customer customer : dCStore.getAllCustomers()) { 
     Object record[] = new Object[COLUMN_COUNT]; 
     record[0] = (select == false) ? Boolean.FALSE : Boolean.TRUE; 
     record[1] = Integer.toString(customer.customerId); 
     record[2] = customer.fullName; 
     data.add(record); 
    } 
    tModel = new TableModel(data); 

      // In the above for loop accoring to user input (i.e click on check all or 
      // uncheck all) i have tried to update the data. As it can be seen that i 
      // have a condition for record[0]. 
      //After the loop, here i have tried several options like validate(). repaint but to no avail 

      customerTable = new JTable(tModel); 
    scrollPane = new JScrollPane(customerTable); 

    setContentPane(this.createContentPane()); 

    setSize(480, 580); 
    setResizable(false); 
    setVisible(true); 


} 

private JPanel createContentPane() { 
    custSelectPanel.setLayout(null); 

    customerTable.setDragEnabled(false); 
    customerTable.setFillsViewportHeight(true); 

    scrollPane.setLocation(10, 10); 
    scrollPane.setSize(450,450); 

    custSelectPanel.add(scrollPane); 

    buttonPanel.setLayout(null); 
    buttonPanel.setLocation(10, 480); 
    buttonPanel.setSize(450, 100); 
    custSelectPanel.add(buttonPanel); 

    selectAllButton.setLocation(0, 0); 
    selectAllButton.setSize(100, 40); 
    buttonPanel.add(selectAllButton); 


    clearAllButton.setLocation(110, 0); 
    clearAllButton.setSize(100, 40); 
    buttonPanel.add(clearAllButton); 

    applyButton.setLocation(240, 0); 
    applyButton.setSize(100, 40); 
    buttonPanel.add(applyButton); 

    cancelButton.setLocation(350, 0); 
    cancelButton.setSize(100, 40); 
    buttonPanel.add(cancelButton); 

    return custSelectPanel; 
} 
} 

表型號

private class TableModel extends AbstractTableModel { 

    private List data; 
    public TableModel(List data) { 
     this.data = data; 
    } 

    private String[] columnNames = {"Selected ", 
      "Customer Id ", 
      "Customer Name " 
    }; 

    public int getColumnCount() { 
     return COLUMN_COUNT; 
    } 
    public int getRowCount() { 
     return data == null ? 0 : data.size(); 
    } 

    public String getColumnName(int col) { 
     return columnNames[col]; 
    } 

    public void setValueAt(Object value, int rowIndex, int columnIndex) { 
     getRecord(rowIndex)[columnIndex] = value; 
     super.fireTableCellUpdated(rowIndex, columnIndex); 
    } 

    private Object[] getRecord(int rowIndex) { 
     return (Object[]) data.get(rowIndex); 
    } 

    public Object getValueAt(int rowIndex, int columnIndex) { 
     return getRecord(rowIndex)[columnIndex]; 
    } 

    public Class getColumnClass(int columnIndex) { 
     if (data == null || data.size() == 0) { 
      return Object.class; 
     } 
     Object o = getValueAt(0, columnIndex); 
     return o == null ? Object.class : o.getClass(); 
    } 

    public boolean isCellEditable(int row, int col) { 
     if (col > 0) { 
      return false; 
     } else { 
      return true; 
     } 
    } 
} 
} 

A瀏覽的動作監聽

class CustomerSelectorUIListener implements ActionListener{ 

CustomerSelectorDialogUI custSelectView; 
Controller controller; 

public CustomerSelectorUIListener (Controller controller, CustomerSelectorDialogUI custSelectView) { 
    this.custSelectView = custSelectView; 
    this.controller = controller; 
} 

@Override 
public void actionPerformed(ActionEvent e) { 
    String actionEvent = e.getActionCommand(); 

    else if (actionEvent.equals("clearAllButton")) 
    { 
     controller.checkButtonControl(false); 
    }  
    else if (actionEvent.equals("selectAllButton")) 
    { 
     controller.checkButtonControl(true); 
    }  
} 
} 

主控制器

public class Controller implements ActionListener{ 

CustomerSelectorDialogUI selectUI; 
DummyCustomerStore store; 

public Controller(DummyCustomerStore store, TestApplicationUI appUI) 
{ 
    this.store = store; 
    this.appUI = appUI; 
    appUI.ButtonListener(this);   
} 

@Override 
public void actionPerformed(ActionEvent event) { 
    String viewAction = event.getActionCommand(); 

    if (viewAction.equals("TEST")) { 
     selectUI = new CustomerSelectorDialogUI(appUI, store, true); 
     selectUI.showTextActionListeners(new CustomerSelectorUIListener(this, selectUI)); 
     selectUI.setVisible(true); 
    } 
} 

public void checkButtonControl (Boolean checkAll) { 
    selectUI.PopulateAndShow(store, checkAll); 
}  
} 
+0

請出示表格模型的實現。 – tenorsax

+0

請參閱編輯 – LivingThing

+0

'controller.checkButtonControl(true);'是否被執行?目前尚不清楚/顯示您如何設置控制器。 – tenorsax

回答

2

它看起來像問題與你重新創建表一旦按鈕被點擊的方式。您正在創建一個新表並將其添加到內容窗格。然而,舊的控制措施也保持在那裏。如果添加:

getContentPane().removeAll(); 

在致電:

setContentPane(this.createContentPane()); 

它應該可以解決眼前的問題。但是,您應該考慮使用更有效的方式更新表格 - 只需更新模型或將其替換即可。不需要移除整個表格。

編輯:

下面是一個簡化的例子,如何來更新模型:

public void toggleSelection(Boolean select) { 
    for (int rowIndex = 0; rowIndex < tModel.getRowCount(); rowIndex++) { 
     tModel.setValueAt(select, rowIndex, 0); 
    } 
} 

然後,就執行從控制器此方法。

您也可以在必要時重新建立模型,即(再次,簡化):

public void toggleSelection(Boolean select) { 
    List data = new ArrayList(); 

    for (int idx = 0; idx < 5; idx++){ 
     Object record[] = new Object[] {select, "test", "test"}; 
     data.add(record); 
    } 

    TableModel model = new TableModel(data); 
    customerTable.setModel(model); 
} 
+0

真棒,工作!謝謝。那麼,您能否更多地解釋一下(編輯您的答案)如何更有效地做到這一點。 – LivingThing

+0

@LivingThing不客氣!:) – tenorsax

+1

@LivingThing看看編輯是否澄清了一些事情。 – tenorsax

0

加入revalidate()PopulateAndShow可以幫助

setSize(480, 580); 
setResizable(false); 
setVisible(true); 
revalidate(); 
+0

試過,無法正常工作 – LivingThing

+0

可否請您發佈所有代碼,以便我們編譯並運行自己? – 2012-10-24 04:52:40

相關問題