0

i have a jTable where it's loaded the result of a search, based on some jComboBox. I'm using DefaultTableModel for that, for the other jTables i use TableModel.

The fields that are shown at the jTable, after the search are always the same, but how can i create an TableModel that would work if the fields shown at the jTable were not always the same? Without creating one for every possible combination?

First scenario i perform a search based on the jComboBox and it returns all data and shows it on the jTable. In the second i select what i want to see from the object returned in some jRadioButtons.

jTable from First scenario.

[![https://i.stack.imgur.com/FPElv.png][1]][1]

jTable from Second scenario.

https://i.stack.imgur.com/g3qnj.png

One of my TableModels:

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import modelo.Setor;

public class ModeloSetor extends AbstractTableModel {

private List<Setor> setores;

public ModeloSetor() {
    setores = new ArrayList<Setor>();
}

@Override
public int getRowCount() {
    return setores.size();
}

@Override
public int getColumnCount() {
    return 2;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    Setor setor = setores.get(rowIndex);
    switch (columnIndex) {
        case 0:
            return setor.getId();
        case 1:
            return setor.getNome();
        default:
            return "";
    }
}

public void adicionar(Setor setor) {
    setores.add(setor);
    fireTableRowsInserted(setores.size() - 1, setores.size() - 1);
}

public void remover(Setor setor) {
    setores.remove(setor);
    fireTableRowsInserted(setores.size() - 1, setores.size() - 1);
}

public Setor getSetor(int linha) {
    return setores.get(linha);

}

public ModeloSetor(List<Setor> lista) {
    this();
    setores.addAll(lista);
}

public void adicionaLista(List<Setor> lista) {
    int i = setores.size();
    setores.addAll(lista);
    fireTableRowsInserted(i, i + lista.size());
}

public void limpaLista() {
    if (setores.size() > 0) {
        int i = setores.size();
        setores.clear();
        fireTableRowsDeleted(0, i - 1);
    }
}

@Override
public String getColumnName(int coluna) {
    switch (coluna) {
        case 0:
            return "Id";
        case 1:
            return "Nome";
        default:
            return "";

    }
}

}

Thanks for now.

Here is a part of the method where i'm working:

  private void jButtonSearchActionPerformed(java.awt.event.ActionEvent evt) {        


    jTableResultado.getColumnModel().getColumn(0).setMaxWidth(50);
    jTableResultado.getColumnModel().getColumn(1).setPreferredWidth(180);
    jTableResultado.getColumnModel().getColumn(2).setPreferredWidth(250);
    jTableResultado.getColumnModel().getColumn(3).setPreferredWidth(310);
    jTableResultado.getColumnModel().getColumn(4).setPreferredWidth(100);
    jTableResultado.getColumnModel().getColumn(5).setPreferredWidth(250);
    jTableResultado.getColumnModel().getColumn(6).setPreferredWidth(150);
    jTableResultado.getColumnModel().getColumn(7).setPreferredWidth(150);
    jTableResultado.getColumnModel().getColumn(8).setPreferredWidth(100);
    jTableResultado.getColumnModel().getColumn(9).setPreferredWidth(100);
    PersonDao personDao = new PersonDao();
    Person person= new Person();
    Condutor condutor = new Condutor();

    DefaultTableModel modelo = (DefaultTableModel) jTableResultado.getModel();
    modelo.setNumRows(0);

    TableColumnManager tcm = new TableColumnManager(jTableResultado);
    Date dataInicial = (dataInicialParaInserir(jTDataInicial.getText()));
    Date dataFinal = (dataFinalParaInserir(jTDataFinal.getText()));

     if (jRadioCodigoPerson.isSelected()) {
        tcm.hideColumn("Id");
    } else {
        tcm.showColumn("Id");
    }
    if (jRadioNamePerson.isSelected()) {
        tcm.hideColumn("Name");
    } else {
        tcm.showColumn("Name");
    }


    List<Object[]> listaObjetos = personDao.consultarDataParametros(person, dataInicial, dataFinal,
    condutor);
    for (Object[] t : listaObjetos) {

        modelo.addRow(new Object[]{t[0], t[1], t[2], t[3], t[4], t[5], t[6], t[7], t[8], t[9]});
    }

}

But, it is showing duplicated the columns "Id" and "Name" each time i click the button .

Rodrigo
  • 153
  • 2
  • 9
  • 1
    Use the [*strategy pattern*](https://en.wikipedia.org/wiki/Strategy_pattern). – trashgod Apr 06 '16 at 20:28
  • 1
    +1 for strategy pattern suggestion (interesting approach). *In the second i select what i want to see from the object returned in some jRadioButtons.* What about hidding/showing columns as needed, as shown [here](http://stackoverflow.com/a/6796673/1795530) and [here](http://stackoverflow.com/a/6798013/1795530)? – dic19 Apr 06 '16 at 20:40
  • @dic19: I too [*find enums useful for implementing the strategy pattern,*](http://stackoverflow.com/a/15076852/230513) especially with `JComboBox`. – trashgod Apr 06 '16 at 21:40

1 Answers1

3

In the second i select what i want to see from the object returned in some jRadioButtons.

You always do your query to return all the data possible.

Then you can remove the TableColumn from the TableColumnModel of the JTable to only display the columns you want.

So if you create your first table and you don't want to see the "Line" column you just do something like:

table.removeColumn( table.getColumn( "Line" ) );

If you need the ability to redisplay the "Line" column then you can just add the TableColumn back to the TableColumnModel of the table. For this you might want to check out the Table Column Manager which is a class that will help manage this for you by using the hideColumn(...) and showColumn(...) methods.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I started to try by your suggestion, but i am having some problems, maybe you may help again. One thing is that i wanted that every time i click the button the model was "reseted", another is that why is it showing two times the columns . – Rodrigo Apr 07 '16 at 06:10
  • @Rodrigo, The `TableColumnManager` should only ever be created once, when you first create the JTable and load the data into the table. It looks like you are creating a new TableColumnManager on an existing table every time the ActionListener is invoked. Start be creating a simple demo. I already showed you the basic 3 lines of code that are needed. So then you can add a couple of radio button to toggle columns "C" and "D". You test code will be about 15-20 lines of code. Get that working first. Then once you understand that you can fix your real code. – camickr Apr 07 '16 at 14:40
  • I solved the problem by using `SwingX ` and `JXTable`, but thanks for the suggestions. – Rodrigo Apr 08 '16 at 01:13