Problelma no uso do ui:table (selecionar um objeto do ObjectListaDataProvider escolhido por 1 radio)

0 respostas
dreampeppers99

Bem eu tentarei explicar tudo...

Meu ambiente
IDE : Netbeans 5.5.1
Tipo do projeto: Visual Web

Eu criei um POJO chamado User (+Long id,+String name,+String user,+String pwd), depois eu criei um Page1 (aquele visual) e nesta página adicionei alguns controles da paleta básica.
1 - button (query)
2 - button (select)
3 - table
4 - staticText (staLabel)

Então eu coloquei um ObjectListDataProvider (get,set) na page (visão java).

private ObjectListDataProvider objectListDataProvider = new ObjectListDataProvider();
public ObjectListDataProvider getObjectListDataProvider() {
        return objectListDataProvider;
    }
public void setObjectListDataProvider(ObjectListDataProvider objectListDataProvider) {
        this.objectListDataProvider = objectListDataProvider;
    }

No mesmo momento eu adicionei um objeto List list = new ArrayList(); para page1.
Bem no modo design I cliquei no componente table com o auxiliar do mouse e escolhi Table Layout:
Então eu mudei a forma de obter os dados: (primeiro combo box) eu escolhi
objectListDataProvider.
Adicionei 4 colunas
1 - sem texto no cabeçalho ou Expression Value (EL) e escolhi o componente radio button
2 - Header Text: Name, static Text, EL #{currentRow.value['id']}
3 - Header Text: Name, static Text, EL #{currentRow.value['name']}
4 - Header Text: Name, static Text, EL #{currentRow.value['user']}

No botão query i implementei o seguinte código:

public String button1_action() {

//the constructor acept id, user, password
list.add(new User(1l,"paul99","[telefone removido]"));
list.add(new User(2l,"johnfrusciante","[telefone removido]"));
list.add(new User(3l,"jkill9","[telefone removido]"));
objectListDataProvider.setList(lista);

return null;
}

Eu iniciei a aplicação (F6) e cliquei no botão de query e a tabela é mostrada populada...

Todavia eu podia escolher mais do que um radio button... Eu procurei na Internet uma solução e encontrei (Wiki Netbeans)
um bom artigo
http://blogs.sun.com/winston/entry/single_selectable_row_table_component
E adicionei (apenas isto) o código abaixo na página.

private TableSelectPhaseListener tablePhaseListener = new TableSelectPhaseListener();
    
    public void setSelected(Object object) {
        RowKey rowKey = (RowKey)getValue("#{currentRow.tableRow}");
        if (rowKey != null) {
            tablePhaseListener.setSelected(rowKey, object);
        }
    }
    
    public Object getSelected(){
        RowKey rowKey = (RowKey)getValue("#{currentRow.tableRow}");
        return tablePhaseListener.getSelected(rowKey);
        
    }
    
    public Object getSelectedValue() {
        RowKey rowKey = (RowKey)getValue("#{currentRow.tableRow}");
        return (rowKey != null) ? rowKey.getRowId() : null;
        
    }
    
    public boolean getSelectedState() {
        RowKey rowKey = (RowKey)getValue("#{currentRow.tableRow}");
        return tablePhaseListener.isSelected(rowKey);
    }

Agora podia escolher apenas um radio button... certo, mas ainda queria saber qual foi o objeto que o usuário da UI escolheu.

Comecei a buscar novas fonte... e encontrei esse documento:
http://wiki.netbeans.org/wiki/view/VwpFAQdetectradiobutton
Lendo-o pensei é apenas isto.

Código para o botão selecionar...

public String edit(){
RowKey rowkey = tableRowGroup.getRowKey();
User user = (User) dataProvider.getValue(rowKey);
//eu criei um atributo na sessionbean
getSessionBean1().setUser(user);
}

Mas ainda não funcionou...

"Debugando" a parte de selecionar eu notei que sempre o valor do rowkey é nulo.
Então o que eu preciso fazer para saber qual foi o objeto escolhido pelo usuário?

PS: Toda vez que eu "submit" a página (por exemplo click em um button na mesma página que retorna null) minha tabela volta sem nenhum dado (não mantém o estado como os outros componentes jsf)...

Muito obrigado

Criado 26 de setembro de 2007
Respostas 0
Participantes 1