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;
        }
    }
} 
}

视图动作监听器

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);
}       
}
4

2 回答 2

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);
}
于 2012-10-24T04:56:02.030 回答
0

添加可能会revalidate()有所PopulateAndShow帮助

setSize(480, 580);
setResizable(false);
setVisible(true);
revalidate();
于 2012-10-24T04:47:14.153 回答