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.
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 .