Componente trava!

19 respostas
S

Pessoal vejam só:

if (jCheckBoxNew.isSelected()) {
            jProgressBar1.setIndeterminate(true); // meu jprogressbar trava
            repaint();
            jPanelNew.repaint();
            
            if (getRadioS().equals("ok")) {
                try {
                    fazer.teste("NS", this);
// após sair da classe acima, o jprogressbar volta a funcionar...
                } catch (Throwable ex) {
                    ex.printStackTrace();
                }

19 Respostas

S

Pessoal vejam só:

Algúem pode me ajudar?

if (jCheckBoxNew.isSelected()) {
            jProgressBar1.setIndeterminate(true); // meu jprogressbar trava
            repaint();
            jPanelNew.repaint();
            
            if (getRadioS().equals("ok")) {
                try {
                    fazer.teste("NS", this);
// após sair da classe acima, o jprogressbar volta a funcionar...
                } catch (Throwable ex) {
                    ex.printStackTrace();
                }
[/quote]
ViniGodoy

O método da classe teste dispara uma thread separada?

ViniGodoy

Tópico movido para o fórum de interface gráfica.

S

Sim,

Porém tudo que eu tento fazer antes de chamaro a classe trava, e após sair daquela classe volta ao normal.

Tentei até inserir uma imagem antes da chamada da classe, porém a imagem só aparece depois que sair da mesma.

ViniGodoy

Que tal postar o código daquela classe? Fica difícil adivinhar o que tem lá dentro.

drsmachado

Geral quer ajuda, mas não se ajuda.
Acha que o pessoal aqui vai copiar o código…

S

Basicamente é isso:

POIFSFileSystem excel= new POIFSFileSystem(new FileInputStream(fileexcel));
            POIFSFileSystem fsexcel = new POIFSFileSystem(new FileInputStream(fileexcel1));
            POIFSFileSystem fsexcel1 = new POIFSFileSystem(new FileInputStream(fileexcel2));

Pegos os valores das celulars do excel e jogo em arquivo texto.

ViniGodoy

E onde você dispara a thread secundária? Pq, aparentemente, está rodando tudo dentro do método de um JButton.

S

Tentei fazer do jeito abaixo, mais deu na mesma, ele trava, e depois volta

Progress progress = new Progress();
    
   public void jogar() throws InterruptedException {

   Thread threadTimer = new Thread();
   threadTimer.start();
   if(!progress.equals(false)) {
       jLabelProgress.setVisible(true);
     
   }
}




    private void jButtonNew(java.awt.event.ActionEvent evt) {                                                           
   jogar();

 if (jCheckBoxNew.isSelected()) {
  
            jLabelProgressOaMATM.setVisible(true);
            repaint();
          jLabelProgress.repaint();
          jPanelNewN.repaint();
ViniGodoy

Não é assim que cria uma thread. Aliás, assim até cria, mas uma Thread que não executa nada.

O correto seria:

private void jButtonNew(java.awt.event.ActionEvent evt) { Thread thread = new Thread(new Runnable() { @Override public void run() { jogar(); if (jCheckBoxNew.isSelected()) { jLabelProgressOaMATM.setVisible(true); if (getRadioS().equals("ok")) { try { fazer.teste("NS", this); } catch (Exception ex) { ex.printStackTrace(); } } }}).start(); }

E não precisa daquele monte de repaint(). Normalmente, nunca se chama repaint() diretamente no código, a menos que você esteja programando seu próprio componente.

S

Esta dando problema no Thread t = new Thread(new Runnable() {, fala que é incompativel.

ViniGodoy

Poste o código todo do botão. E a mensagem de erro completa.

S
private void jButtonNew(java.awt.event.ActionEvent evt) { Thread thread = new Thread(new Runnable() { @Override
public void run() {   
          try {
                    jogar();
                } catch (InterruptedException ex) {

                                    }        if (jCheckBoxNew.isSelected()) {         
            jLabelProgressOaMATM.setVisible(true);     
        if (getRadioS().equals("ok")) {     
          try {     
            fazer.teste("NS", this);     
          } catch (Exception ex) {     
            ex.printStackTrace();     
          }     
        }   
    }}).start();   
}

incompatible types
found : void
required: java.lang.Thread
}}}).start();
2 errors

ViniGodoy

Ops, my mistake. O certo era:

private void jButtonNew(java.awt.event.ActionEvent evt) { new Thread(new Runnable() { @Override public void run() { try { jogar(); } catch (InterruptedException ex) { } if (jCheckBoxNew.isSelected()) { jLabelProgressOaMATM.setVisible(true); if (getRadioS().equals("ok")) { try { fazer.teste("NS", this); } catch (Exception ex) { ex.printStackTrace(); } } }}).start(); }

incompatible types
found : void
required: java.lang.Thread
}}}).start();
2 errors

S

Estamos quase la…

O problema agora esta no this, e eu não posso tirar no codigo.

fazer.teste("NS", this);

ViniGodoy

Use

Caso contrário o this vai se referir ao Runnable que vc criou para a Thread.

S

KK eu testei um aqui simples, sem IF sem nada...

e não esta passando pelo run()

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

        new Thread(new Runnable() {     
     
      public void run() {     
                try {
                    jogar();
                } catch (InterruptedException ex) {
                   
                }
}}).start();
public void jogar() throws InterruptedException {




       jProgress.setVisible(true);

   }
S

Vini,

Veja um novo exemplo, pois continua travando....

minha View:
private void jButtonNewNodeBAtmGenerateActionPerformed(java.awt.event.ActionEvent evt) {   

     Progress progress = new  Progress(null);
         Thread t1 = new Thread(progress);


t1.start();
// quando começar a fazer a classe abaixo trava o Dialog que a Thread chamou acima...


                    fazer.gerar("NS", this);
//após sair da classe acima volta ao normal
public class Progress extends JDialog implements Runnable {
  private String nome;

    public void run() {
ProgressBar dialogo = new ProgressBar(null,false, nome);
      //  dialogo.setBounds(300, 400, 200, 400);
        dialogo.show();
        dialogo.repaint();
        
    }

    public void stop (){
        this.stop();
    }
public Progress(String teste){
    nome=teste;
}}
ViniGodoy

Quem tem que ir numa thread separada é o método que atualiza o JProgressBar, no seu caso, o gerar, e não o código que cria o JProgressBar.

Criado 23 de maio de 2011
Ultima resposta 26 de mai. de 2011
Respostas 19
Participantes 3