Link to home
Start Free TrialLog in
Avatar of Jasbir21
Jasbir21

asked on

Urgent-JList -not hearing

Hi,

I hope i am explaining alright...

But this what i have:

I have
worker = new SwingWorker()
{
does a lot of calculations..

....

list.setEnabled(true);
return doWork();
}
           };

progressBar.setValue(0);
worker.start();

list.setEnabled(false);
            
            

list.setSelectedIndex(0);

         
            list.addListSelectionListener( new ListSelectionListener()
            {
                  public void valueChanged(ListSelectionEvent event)
                  {
                        
                        if (event.getValueIsAdjusting()) return;

                        JList theList = (JList) event.getSource();
                  
                       
                              switch(theList.getSelectedIndex())
                              {
                                    case 0:
             
  .....


my problem is that the list is not sensitif, once i press the run button, i need to go to list to refresh..

meaning that i thought when i put the list.setSelectedIndex(0) and when the thread ends it needs to go to updated..how to make the list to hear and get automatically updated...

maybe something like..

if(setEnabled(true))
 list.setSelectedIndex(0)

but i get error...pls help.thanks


Pls help..thanks
SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gauravkrtomar
gauravkrtomar

try repainting the JList.
As after updating any component u need to repaint it so that the effect is visible. After your thread finishes u can do list.repaint().

P.S.:- Can u post ur code with a bit of more clarifications as it is still not clear
I dont think revalidate() is neede, u need to call it on the container only when the container's layout or the childs has changed and you need to recalculate everything, otherwise repaint() is sufficient. And in this case u dont need to calculate the rendering of the list from scratch.
Avatar of Jasbir21

ASKER

actually, this program is pretty long..it is a simulation program ..

what happens, is that while the program is running the list would be setEnabled false.
once the program is runned the list would be setEnabled true...


what  i  am trying to do is that if the setEnabled becames true..then list.setEnabledIndex(0)
however, now what is happening is that , even before i run the program, the list.setEnabledIndex(0) is highlighted...

i do have :

container.add(chartPanel);
            container.validate();  
            container.repaint();

in the switch case of the program...
>> even before i run the program

before you run the program?

Or before you click the button?

Can you explain what it is you want?

You want the list to have nothing selected?

Tim
I am not sure but i think JList has by default 0 index selected try setting it to -1.

just a wild guess ;) not sure if it works.
--->>>before you run the program?

---->>>Or before you click the button?

Yeah..before i click the button..

what i want is that , before i run the button, nothing should be selected...

and when the run is finished, the list.setEnabled becames true, select the the first option that is O.
>> I am not sure but i think JList has by default 0 index selected try setting it to -1.

That should do it... ;-)
actually, the default 0 should only be selected once the setEnabled becames true...

tried the  list.setSelectedIndex(-1) but did not get index 0.

actually, index 0 works for the first option...

my problem is that i would need to refresh the list to see the result(For example, clicking the option....

i think, maybe once the list becames enabled, then set it index is 0 ..

because when the list is enabled, it means,that all calculations are ready to be displayed...

is there such thing as ...

if(setEnabled becomes true)
llist.setSelectedIndex(0);
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this adds the propertylistener which listens to the enabled property

dataList.addPropertyChangeListener(new PropertyChangeListener() {

                  public void propertyChange(PropertyChangeEvent evt) {
                        if(evt.getPropertyName().equals("enabled")){
                              //u will get the Boolean tr
                              Boolean newValue=(Boolean) evt.getNewValue();
                              Boolean oldValue=(Boolean) evt.getNewValue();
// do whatever u want to do depending upon the newValue and oldValue
                        }

                  }

            });
list.setEnabled(true);
list.setSelectedIndex(0);
list.repaint();


when i try the above, the first run it gets updates...but when i press the run button again, it does update...
it works with clearselection..thanks..
as well with repaint now...thanks
>>when i try the above, the first run it 'gets' updates...
>>but when i press the run button again, it 'does' update..
i am not able to get the difference b/w get and does.

Plus look at the above code for the listener, i think it does solve one of ur problems.
Also keep in mind:-
list.clearCache();
list.setSelectedIndex() but only after list.setEnabled(true)
and list.repaint() if u want to render the list.
what is clearCache() for ?
i get error with Jlist ...

thanks for helping solving ..it works with clearselection and repaint...

btw..whats clearCache()n for
sorry actually it was a typo mistake.............I was writting clearSelection only but wrote clearCache(), i was doing some dbase things so i wrote that instead ;)
no problem...thanks for helping :-)