How do I program buttons to de/select all rows & columns in a table?

218 Views Asked by At

This is my code. I use a controller in order to have everything more clean.

public class ControladorModificarMiembros implements IControladorModificarMiembros{

private VentanaModificarMiembros ventana;

private List<MiembroEnGrupo> miembros = new ArrayList<>();
        
ControladorModificarMiembros()
{
    this.ventana = new VentanaModificarMiembros(this,null);  //Instancia la ventana
    this.ventana.setLocationRelativeTo(null);       //Centra la ventana
    this.ventana.getTablaMiembros().setModel(new ModeloTablaMiembros());     //Asignamos modelo a la tabla       
    
    
    ModeloTablaMiembros mta = (ModeloTablaMiembros) this.ventana.getTablaMiembros().getModel();
    
    IGestorAutores ga = GestorAutores.crear();
    List<MiembroEnGrupo> miembrosengrupo = new ArrayList<>();
    
    for(Autor g : ga.verAutores())
    {
        MiembroEnGrupo e = new MiembroEnGrupo(g,null);    
        miembrosengrupo.add(e);
    }
    
    mta.asignarMiembrosEnGrupo(miembrosengrupo);
    
    JComboBox comboRoles = new JComboBox();
    comboRoles.setModel(new ModeloComboRol());
    
    TableColumn tmpColumn = this.ventana.getTablaMiembros().getColumnModel().getColumn(1);
    tmpColumn.setCellEditor(new DefaultCellEditor(comboRoles));
    
    this.ventana.setVisible(true); //Makes it visible
}

@Override
public void btnTodosClic(ActionEvent evt) {
    ModeloTablaAutores mta = (ModeloTablaAutores) this.ventana.getTablaMiembros().getModel();
    mta.actualizar(); 

//HERE IS WHERE I HAVE TO PROGRAM THE JBUTTON IN ORDER TO PRESS IT AND AUTOMATICALLY SELECT ALL THE TABLE

@Override
public void btnNingunoClic(ActionEvent evt) {

    ModeloTablaAutores mtg = (ModeloTablaAutores) this.ventana.getTablaMiembros().getModel(); 

//HERE IS WHERE I HAVE TO PROGRAM THE JBUTTON THAT WILL ALLOW TO DESELECT THE ROW THAT IS ALREADY SELECTED

1

There are 1 best solutions below

0
maloomeister On

JTable provides this functionality for free. With selectAll() and clearSelection.

All you need to do is, to call these methods on your table in the corresponding actionListener of your buttons.

Demo snippet:

public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
        buildGui();
    });
}

private static void buildGui() {
    JFrame frame = new JFrame();
    String[] header = { "Data1", "Data2" };
    Object[][] data = new Object[][] { { "Entry1", "Entry2" }, { "Entry3", "Entry4" } };
    JTable table = new JTable(data, header);
    JScrollPane sp = new JScrollPane(table);
    
    JButton btnSelect = new JButton("Select All");
    // on button click of "SelectAll"
    btnSelect.addActionListener((event) -> table.selectAll());
    JButton btnUnselect = new JButton("Unselect All");
    // on button click of "Unselect All"
    btnUnselect.addActionListener((event) -> table.clearSelection());
    
    JPanel btnPanel = new JPanel();
    btnPanel.add(btnSelect);
    btnPanel.add(btnUnselect);
    
    frame.add(sp);
    frame.add(btnPanel, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}
  1. Nothing selected:

unselected

  1. "SelectAll" pressed:

selectall

After pressing "Unselect All", it is back to 1.