Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8722557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:26:42+00:00 2026-06-13T07:26:42+00:00

I am new to Swing, UI and MVC I have created a code based

  • 0

I am new to Swing, UI and MVC
I have created a code based on MVC. Now my problem is that that in the controller part i have an actioneventlistener which listens to different button clicks. Out of all those buttons i have “select all” and “de-select all”. In my view i have a table, one of the column of that table contains “check boxes”. Now, when i click the “select-all” button i want to check all the check boxes and with “de-select all” i want to uncheck all of them.

Below is my code which is not working. Please tell me what am i doing wrong here. Also, if someone knows a more elagent way please share. Thanks

In my view

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

Table Model

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 Views Action Listener

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

Main Controller

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);
}       
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:26:43+00:00Added an answer on June 13, 2026 at 7:26 am

    It looks like the issue is related to the way you recreate the table once the button is clicked. You are creating a new table and adding it to a content pane. However the old controls also remain there. If you add:

    getContentPane().removeAll();
    

    Before calling:

    setContentPane(this.createContentPane());
    

    It should fix the immediate issue. However, you should consider using much more efficient way of updating the table – simply update the model or replace it. Removing the whole table is not necessary.

    EDIT:

    Here is a simplfied example how to update the model:

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

    Then, just execute this method from the controller.

    You can also rebuild the model if necessary, ie (again, simplified):

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to java.I'm creating a swing based UI. I've created 2 frames, each
I am quiet new to Swing. I have a JTable in which images are
I am new to Swing. There is a silly question that I have. It
I have created a swing application as bellow which shows main tasks in tabs
We have a one Java Swing app. Now there is one requirement that it
I am new to Java Swing I want to develop an POS application which
i am new to netbeans ide , i have a swing desktop application and
I'm practicing writing MVC applications. I have a Mastermind game, that I would like
I am new to Swing. I want to write a program which reads a
I'm trying to build a new java swing component, I realise that I might

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.