place name in columns from an object array

Asked

Viewed 406 times

0

public class TableModelAgendamento extends AbstractTableModel {

int contador = 1;
int qtdFuncionario;
private TOFuncionario[] func = new TOFuncionario[qtdFuncionario];
String coluna[] = new String[qtdFuncionario];
private List<TOItemAgendamento> dados = new ArrayList();
Date hora;
SimpleDateFormat sddf = new SimpleDateFormat("HH:mm:ss");
TOItemAgendamento itemAgenda;
TOItemAgendamento itemAgendamento;

public TableModelAgendamento(TOFuncionario[] funcionario) {
    TOItemAgendamento recebeFunc;
    TOFuncionario funcio;
    qtdFuncionario = funcionario.length;
    this.func = funcionario;
    for (int i = 1; i < funcionario.length; i++) {
        recebeFunc = new TOItemAgendamento();
        funcio = new TOFuncionario();
        funcio = func[i];
        recebeFunc.getFuncionario().setNome(funcio.getNome());
    }
    zeraTabela();
}

public void zeraTabela() {
    try {
        Date data = sddf.parse("06:30:00");
        Calendar horaInicial = Calendar.getInstance();
        horaInicial.setTime(data);
        data = sddf.parse("21:00:00");
        Calendar horaFinal = Calendar.getInstance();
        horaFinal.setTime(data);
        while (horaInicial.before(horaFinal)) {
            itemAgenda = new TOItemAgendamento();
            String horai = sddf.format(horaInicial.getTime());
            horaInicial.add(Calendar.MINUTE, 30);
            itemAgenda.setHoraMinuto(horai);
            itemAgenda.getFuncionario().setNome("");
            adicionaLinha(itemAgenda);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void limpar() {
    dados = new ArrayList();
    fireTableDataChanged();
}

public void setDados(List<TOItemAgendamento> dados) {
    this.dados = dados;
    fireTableDataChanged();
}

public List<TOItemAgendamento> getDados() {
    return dados;
}

public void adicionaLinha(TOItemAgendamento iagendamento) {
    dados.add(iagendamento);
    fireTableDataChanged();
}

public void removeLinha(int linha) {
    dados.remove(linha);
    fireTableDataChanged();
}

@Override
public String getColumnName(int coluna) {
    TOFuncionario funcio;
    for (int i = 1; i < func.length; i++) {
        funcio = new TOFuncionario();
        funcio = func[i];
        return funcio.getNome();
    }
    return null;
}

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

public TOItemAgendamento getItemCompra(int linha) {
    return dados.get(linha);
}

@Override
public int getColumnCount() {
    return func.length;
}

@Override
public Object getValueAt(int linha, int coluna) {
    TOItemAgendamento itemAgendamento = dados.get(linha);
    int i = 1;
        for(int cont = 0; cont <= qtdFuncionario; cont++){
            System.out.println("get valueat ");
            switch (coluna) {
            case 0:
                return dados.get(linha).getHoraMinuto();
            case Integer.SIZE:
                return dados.get(linha).getHoraMinuto();
            }
    }
    return null;

}

@Override
public void setValueAt(Object valor, int linha, int coluna) {
    TOItemAgendamento itemAgendamento = dados.get(linha);
    switch (coluna) {
        case 0:
            break;
        case 1:
            dados.get(linha).getFuncionario().setNome((String) valor);
            break;
        case 2:
            dados.get(linha).getFuncionario().setNome((String) valor);
            break;
        case 3:
            dados.get(linha).getFuncionario().setNome((String) valor);
            break;
        default:
            JOptionPane.showMessageDialog(null, "Erro no TableModelItemCompra, coluna não esperada em setValueAt.");
    }
    fireTableDataChanged();
}
  • What is the question?

  • I am receiving as parameter in my constructor method an object vector, I need getColumnName to return the employee name of each object of this vector.

  • I still don’t understand what the question is (your code doesn’t work? it works, but it doesn’t do what it wants? doesn’t know what code to put? etc), but I remember a long time ago I did something like this. You call fireTableDataChanged every time the lines change, right? When the columns change, you need to shoot fireTableStructureChanged. Try calling this method whenever func change, this should make Java recalculate all columns.

  • @Override public String getColumnName(int column) { Function function; for (int i = 1; i < func.length; i++) { function = new Function(); function = function[i]; function function.getName(); Return null; } // In this method I need to take the name of the employees to put in the columns, but when I give a Return getName inside the for it arrow all the columns with the same name. Got it ?

  • Now I do! As you had only posted the code (and a large code on top of that) but did not say what I wanted I had no idea what the problem was... But now it’s clear, see my answer below. P.S. If you can, I suggest [Edit] the question by putting this information.

1 answer

2

First, make sure your table has autoCreateColumnsFromModel set as true. Otherwise she will ignore the functions getColumnCount and getColumnName that you created.

Second, its function getColumnName is always returning the second element of the list:

@Override
public String getColumnName(int coluna) {
    TOFuncionario funcio;
    for (int i = 1; i < func.length; i++) { // Começa pelo segundo (índice 1)
        funcio = new TOFuncionario();       // Cria um novo objeto (?!)
        funcio = func[i];                   // Pega o objeto do array
        return funcio.getNome();            // Retorna seu nome
    }
    return null;
}

Instead, take the correct element (which should have the same index of coluna) and return your name:

@Override
public String getColumnName(int coluna) {
    return func[coluna].getNome();
}

Check the rest of your code, in the constructor for example you are also counting the elements from the second (index 1) instead of starting with the first (index 0) - and there also you create an object and soon after throw it away...

Finally, if your field func is dynamically changed (and not only in the constructor, as seems to be the case), remember to call fireTableStructureChanged for the table to auto-update when employees change (in this case, as you are using the employee name as column name, if this value changes it is necessary to call this method so that the table does not continue with the old name).

  • Thanks for the tips, I did it the way you said, but I gave Nullpointerexception. inside getColumnName I just put Return func[column]. getName();

  • @Ana When you built your TableModelAgendamento, you passed an array of TOFuncionario, right? There are null elements in this array? Try printing it on the console - System.out.println( java.util.Arrays.deepToString(funcionario) ) - and see if everything is ok (my guess is that the first one is null; you understood what I said about Java arrays starting from scratch and not from one, right?)

  • already put to print it brings the correct data but when I give the Return it fills all the columns with the first Indice, always with the first name.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.