1

I have tried a lots of tutorial and examples listed here, but doesn't work with me, i think this because i use netbeans.

I tried :

let me show my code.

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    vet = new int[Integer.valueOf(tamVet.getText())];
    ordenado = new int[Integer.valueOf(tamVet.getText())];
    metUtilizado = (String) CBMetodo.getSelectedItem();
    if (CBMetodo.getSelectedItem() == "Ordenado") {
        for (int i = 0; i < Integer.valueOf(tamVet.getText()); i++) {
            vet[i] = i;
        }
    } else if (CBMetodo.getSelectedItem() == "Reverso") {
        for (int i = 0; i < Integer.valueOf(tamVet.getText()); i++) {
            vet[i] = -i;
        }
    } else if (CBMetodo.getSelectedItem() == "Aleatório") {
        Random randomGenerator = new Random();
        for (int i = 0; i < Integer.valueOf(tamVet.getText()); i++) {
            vet[i] = randomGenerator.nextInt(Integer.valueOf(tamVet.getText()));
        }
    }
    vetList.clear();

    for (int i = 0; i < vet.length; i++) {
        vetList.add("[" + i + "] " + String.valueOf(vet[i]));
        PB.setValue(((i+1)*100)/vet.length); // i want update here
    }
} 

i am new on Java i want to UPDATE the value on this ...can anyone help me with this problem?

Community
  • 1
  • 1
  • It's quite hard to see what your issue is, and the formatting of yoru code makes it quite hard to read. Could you edit it to be more clear please? – Phil Anderson May 28 '15 at 15:48
  • First of all, change all CBMetodo.getSelectedItem() == "Aleatório" to CBMetodo.getSelectedItem().equals("Aleatório") .. use EQUAL not == – Solano May 28 '15 at 15:48
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. 3) *"doesn't work with me, i think this because i use netbeans."* You should not use an automagic IDE until you understand Java. Even then, it is a poor tradesman who blames his tools. – Andrew Thompson May 29 '15 at 05:06

1 Answers1

1

First: If you are trying to update your PB value and show it at same thread, you can have some issues (it will display the content only in the end of the loop).

Best way is to split One thread for showing the user-interface and another thread to update the variable that holds your progress bar value.

Try this, using appropriate comparison for java.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    vet = new int[Integer.valueOf(tamVet.getText())];
    ordenado = new int[Integer.valueOf(tamVet.getText())];
    metUtilizado = (String) CBMetodo.getSelectedItem();
    if (CBMetodo.getSelectedItem().equals("Ordenado")) {
        for (int i = 0; i < Integer.valueOf(tamVet.getText()); i++) {
            vet[i] = i;
        }
    } else if (CBMetodo.getSelectedItem().equals("Reverso")) {
        for (int i = 0; i < Integer.valueOf(tamVet.getText()); i++) {
            vet[i] = -i;
        }
    } else if (CBMetodo.getSelectedItem().equals("Aleatório")) {
        Random randomGenerator = new Random();
        for (int i = 0; i < Integer.valueOf(tamVet.getText()); i++) {
            vet[i] = randomGenerator.nextInt(Integer.valueOf(tamVet.getText()));
        }
    }
    vetList.clear();
    new Thread()
    {
        public void run() {
            for (int i = 0; i < vet.length; i++) {
                int N_DIVISOES = vet.length/4;
                vetList.add("[" + i + "] " + String.valueOf(vet[i]));
                PB.setValue((i/N_DIVISOES) * N_DIVISOES); // i want update here
            }
        }
     }.start();
} 
Solano
  • 550
  • 2
  • 9
  • I do not need to update every percentage, I need at least that when 25% 50% 75%. – Guilherme Magno May 28 '15 at 15:59
  • If you want that update at 25 50 75... use the logic that i changed in the for loop. N_DIVISOES will give you "de quanto em quanto" you want to update your progress bar. – Solano May 28 '15 at 16:09
  • Dont forget to set your max progress bar value as vet.length – Solano May 28 '15 at 16:11
  • the problem is the bar only update at the end of for! and i need it working ON for – Guilherme Magno May 28 '15 at 16:14
  • It is because you are running the update in the same thread as the user interface. let me show you updating the post – Solano May 28 '15 at 16:15
  • check this new approach using Thread() to execute your value update in a separated thread. PS: check if it will complain about "you need to declare variable xyz as final" – Solano May 28 '15 at 16:22