JScrollPane não se atualiza

13 respostas
P

É o seguinte, eu tenho um JTable com aproximadamente 1000 linhas… este JTable está dentro de um JScroolPane, e embaixo deste JScroolPane tem uns botões para navegar nas linhas da Tabela. Acontece que quando eu navego para o último registro, ele até navega, mas o JScroolPane não desse até o último registro, alguém sabe como fazer isso…

13 Respostas

Heber

Olha aí:

myTable.getSelectionModel().addListSelectionListene( new ListSelectionListener(){ public void valueChanged(ListSelectionEvent e){ JScrollBar vertBart= myScrollPane.getVerticalScrollBar(); vertBart.setValue(myTable.getRowHeight()*myTable.getSelectedRow()); } } );

P

“Heber”:
Olha aí:

myTable.getSelectionModel().addListSelectionListene( new ListSelectionListener(){ public void valueChanged(ListSelectionEvent e){ JScrollBar vertBart= myScrollPane.getVerticalScrollBar(); vertBart.setValue(myTable.getRowHeight()*myTable.getSelectedRow()); } } );

Onde está a classe ListSelectionListener?

Heber
javax.swing.event
P

“Heber”:
Olha aí:

myTable.getSelectionModel().addListSelectionListene( new ListSelectionListener(){ public void valueChanged(ListSelectionEvent e){ JScrollBar vertBart= myScrollPane.getVerticalScrollBar(); vertBart.setValue(myTable.getRowHeight()*myTable.getSelectedRow()); } } );

O JScrollBar que vc criou ali seria o que eu já tenho criado né?

Heber

Exatamente, vc vai Pegar esse JScrollBar do seu JScrollPane.

P

“Ping”:
“Heber”:
Olha aí:

myTable.getSelectionModel().addListSelectionListene( new ListSelectionListener(){ public void valueChanged(ListSelectionEvent e){ JScrollBar vertBart= myScrollPane.getVerticalScrollBar(); vertBart.setValue(myTable.getRowHeight()*myTable.getSelectedRow()); } } );

O JScrollBar que vc criou ali seria o que eu já tenho criado né?


Acho que eu não me expliquei direito, o vertBart ou o myScrollPane é o JScrollPane que eu já criei?

e tb está dando o seguinte erro

tabelas/Cidade_Consulta.java [109:1] cannot resolve symbol
symbol : method addListSelectionListene ()
location: interface javax.swing.ListSelectionModel
jTable1.getSelectionModel().addListSelectionListene(
^
1 error
Errors compiling Cidade_Consulta.

P

faltava só um r

estava assim antes

jTable1.getSelectionModel().addListSelectionListene(

o certo é
jTable1.getSelectionModel().addListSelectionListener(

Heber

Sim, o vertBart ou o myScrollPane é o JScrollPane onde a JTable está!

E me desculpe pelo erro de digitação … :oops:

P

“Heber”:
Sim, o vertBart ou o myScrollPane é o JScrollPane onde a JTable está!

E me desculpe pelo erro de digitação … :oops:

Cara, eu consegui compilar com o código que passou, mas não é bem aquilo… porque se eu estou na linha 1 e dou um next ele sobe todas as linhas para que a linha 2 apareça no topo, ou seja a linha 1 some da visão do usuário… deu pra entender?

Heber

Entendi, nesse caso acho que teria que ser testado se a linha já está visível antes de mandar mostrá-la !!

P

Pois é… e como que eu faria isso

Heber

Olha se isso funciona, não tive como testar …

myTable.getSelectionModel().addListSelectionListene( new ListSelectionListener(){ public void valueChanged(ListSelectionEvent e){ int height = myScrollPane.getHeight(); int posicao = myTable.getRowHeight() * myTable.getSelectedRow(); JScrollBar vertBart= myScrollPane.getVerticalScrollBar(); if((posicao < vertBart.getValue()) || (posicao > (vertBart.getValue() + height))) vertBart.setValue(posicao); } } );

P
"Heber":
Olha se isso funciona, não tive como testar ...
myTable.getSelectionModel().addListSelectionListene(
    new ListSelectionListener(){
        public void valueChanged(ListSelectionEvent e){
            int height = myScrollPane.getHeight();
            int posicao = myTable.getRowHeight() * myTable.getSelectedRow();
            JScrollBar vertBart= myScrollPane.getVerticalScrollBar();
            if((posicao < vertBart.getValue())
                    || (posicao > (vertBart.getValue() + height)))
                vertBart.setValue(posicao);
        }
    }
);

Valeu cara, funcionaou, só precisei alterar algumas coisinhas... código:

jTable1.getSelectionModel().addListSelectionListener(
            new ListSelectionListener(){
                public void valueChanged(ListSelectionEvent e){
                    System.out.println("altura do scroll = "+jScrollPane1.getHeight());
                    System.out.println("altura da tabela vezes a linha selecionada = "+jTable1.getRowHeight() * jTable1.getSelectedRow());
                    
                    int height = jScrollPane1.getHeight();
                    int posicao = jTable1.getRowHeight() * jTable1.getSelectedRow();
                    JScrollBar vertBart= jScrollPane1.getVerticalScrollBar();
                    System.out.println("valor da barra vertical do scrool = "+vertBart.getValue());
                    if((posicao < vertBart.getValue()))
                        vertBart.setValue(posicao);
                    else if((posicao+32 > (vertBart.getValue() + height)))
                    {
                        if (jTable1.getSelectedRow()==((conexaoCid.pegaTabela().length)-1))
                            vertBart.setValue(posicao);
                        else
                            vertBart.setValue(vertBart.getValue()+16);
                    }                        
                }
            }
        );
[/quote]
Criado 7 de abril de 2004
Ultima resposta 7 de abr. de 2004
Respostas 13
Participantes 2