Link to home
Start Free TrialLog in
Avatar of Jasbir21
Jasbir21

asked on

Java - jlist - urgent

HELP....

I am have a list where a user has an option to choose graph 1, graph 2.
User click the run button once.

If graph 1 is highlited, graph 1 should display . If graph 2 is highlignted, graph 2 should be displayed.

What is happenning is that i need to click to run button each time to see the display..it is as if the list is not sensitif . pls help .


listModel = new DefaultListModel();
          listModel.addElement("Graph 1");
          listModel.addElement("graph 2 ");
          listModel.addElement("graph 3");
        listModel.addElement("graph 4");
            

            //Create the list and put it in a scroll pane.
            list = new JList(listModel);
            list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            list.setSelectedIndex(0);

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

                   JList theList = (JList) e.getSource();
                   if(theList.getSelectedIndex()==0)
                   {
                        
                         displayGraph1(yscore);
                         System.out.println("Graph 1");
                   }
                  
                  if(theList.getSelectedIndex()==1)
                   {
                        

                        displayGraph2(yscore);
                         System.out.println("Grapth 2");

                   }

                   if(theList.getSelectedIndex()==2)
                   {
                        
                         displayGraph3();
                         System.out.println("Graph 3");
                   }

                   if(theList.getSelectedIndex()==3)
                   {
                        
                         displayGraph4(stackPopulation);
                         System.out.println("Graph 4");
                   }
                  

                   //else
                  //       System.out.println("What i s wrong");
             }
       }
             );
Avatar of Manikandan Thiagarajan
Manikandan Thiagarajan
Flag of India image

could you please post the full code now?
Avatar of zzynx
I don't know what you mean with your run button.
It is also not mentioned in the code snippet.
What does displayGraph() looks like?

Do you mean that when you have clicked graph 1 and afterwards graph 2,
that graph 1 stays visible (and doesn't change to graph 2) ?

In that case you probably need to call

    revalidate();

followed by

    repaint();

on the panel containing the graph
Do you see *all* the

    System.out.println("Graph x");

when you select the list items?
I guess you do.
Well, that proves that the code snippet you posted is OK.

The fact that you don't see the graphs you selected points to a "GUI refresh" problem.
Try the revalidate()/repaint() approach.
Avatar of Jasbir21
Jasbir21

ASKER

hi, i am using jfreegraph...
public void displayGraph4(){

 chart = ChartFactory.createStackedBarChart(
                  "Stack",  // chart title
                  "x",                  // domain axis label
                  "Py",                     // range axis label
                  dataset,                     // data
                  PlotOrientation.VERTICAL,    // the plot orientation
                  true,                        // legend
                  false,                       // tooltips
                  false                        // urls
                  );


                        CategoryPlot plot = chart.getCategoryPlot();
            
            
            CategoryItemRenderer renderer = new ExtendedStackedBarRenderer();
            renderer.setItemLabelsVisible(true);
            renderer.setItemLabelGenerator(
                  new StandardCategoryItemLabelGenerator());
            renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator());
            plot.setRenderer(renderer);
       
       
            
            chartPanel=new ChartPanel(chart);
            chartPanel.setPopupMenu(null);
            }
i have a code which does a lot stuff (run iterations...)when i click the run button.

then it would collect the data and display on graph .
OK.
And where do you make the created chartPanel visible?
Where do you add it to a main frame/panel?
What's the code for that?
actually, after running nothing gets displayed.

even though Graph 1 is highlighted, i dont see "Graph 1".

however, if i highlight graph 2, and then go to graph 1, then , i see "Graph 1"

Meaning, i dont view the graph , but see the word printed.
>> i have a code which does a lot stuff (run iterations...)when i click the run button.
So, it collects the data for all the 4 graphs? Is that right?
xSpring=Spring.constant(388);
          ySpring=Spring.constant(10);
            wSpring=Spring.constant(400);
            hSpring=Spring.constant(360);

            Constr=layoutCont.getConstraints(chartPanel);
            Constr.setX(xSpring);
            Constr.setY(ySpring);
            Constr.setWidth(wSpring);
            Constr.setHeight(hSpring);
            container.add(chartPanel);
yup...
>> container.add(chartPanel);

and 'container' is?  (we're almost there ;°)

>> yup...
OK. Now I understand why you only want to press it once.
i think, the list is not so sensitif..
because, when i run the program once and Graph 1 is highlighted, Graph 1 is not being printed out.

Only after highlight Graph 2 and then go to Graph 1, then only I see, the wording Graph 1..
in displayGraph4().

each graph , displayGraph 1, displayGraph2..., have the container...
That's the behaviour we're going to solve.
>> 'container' is?
what is 'container' (JPanel, getContentPane(), ...)

Can I see that code?
actually, i modify the listener code..the example say , need to add this

list.addListSelectionListener(this) but i did not because, i got error.. i hope that didnot make any difference...
layoutCont=new SpringLayout();
         container = getContentPane();

this is in the main code..
lets say i remove the options

and put :

displayGraph1();
displayGraph2();
displayGraph3();
displayGraph4();

the graphs gets displayed one by o ne...
That code is just OK. (Although I would replace the 4 if's by a switch)
>> layoutCont=new SpringLayout();
>>         container = getContentPane();
>> this is in the main code..
OK.

Now, when you select a list item, all your code does is:

              displayGraphx();
              System.out.println("Graph x");

try adding this two lines:

              container.validate();
              container.repaint();
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium 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
just replaced it with switch case

however, when i push the run button and graph 1 highlighted,"Graph 1" is not displayed..
What's the idea.

  At start up you add a (probably not yet initialized) chartPanel to container.
  When you select a list item, chartPanel is recreated  ( chartPanel = .... )
  but that does NOT mean that container contains that new instance!!!!
  It still has (and shows) the old one.
>> however, when i push the run button and graph 1 highlighted,"Graph 1" is not displayed..
You need the additions I told you:

      container.remove(chartPanel);
      displayGraphx();
      System.out.println("Graph x");
      container.add(chartPanel);
      container.validate();  // not sure if those are still needed after that add()
      container.repaint();   // idem (try it for yourself)


Have to go for lunch now.
See you later on.
we are nearly there :-)
all the graphs are displaying now except for one problem:

---->
----->
>> however, when i push the run button and graph 1 highlighted,"Graph 1" is not displayed..

------------------------ all i see is the plain graph ...only after select another item then go to first item, i get to see the first graph...


at startup i have this:
chart......
//add the chart to the chart panel
      chartPanel=new ChartPanel(chart);

   chartPanel.setPreferredSize(new Dimension(500, 400));

          
       xSpring=Spring.constant(388);
       ySpring=Spring.constant(10);
       wSpring=Spring.constant(400);
       hSpring=Spring.constant(360);
       Constr=layoutCont.getConstraints(chartPanel);
       Constr.setX(xSpring);
       Constr.setY(ySpring);
       Constr.setWidth(wSpring);
       Constr.setHeight(hSpring);
      
       // add the chart panel to the container
       container.add(chartPanel);        
       container.validate();
   container.repaint();  


lots of lines of codes...

then i have switch case...


Make sure you're not doing processing-intensive operations from an event handler - that can cause display problems. Use a SwingWorker thread
Back form lunch
>> we are nearly there :-)
>> all the graphs are displaying now
Fine!

>> except for one problem:
>> when i push the run button and graph 1 highlighted,
Do you have to push the run button only after you have selected one graph from the list?

>> "Graph 1" is not displayed..
>> all i see is the plain graph
What's "the plain graph"? How does it differ from "Graph 1"?

>> ...only after select another item then go to first item, i get to see the first graph...
So, there's a problem with ONLY the 1st graph. Correct?
>> Do you have to push the run button only after you have selected one graph from the list?
I mean: is the user allowed to push the run button without selecting anything in the list?
--->>
Do you have to push the run button only after you have selected one graph from the list?

actually, the list got 4 graphs:
the first one is always highlighted.

>> "Graph 1" is not displayed..
>> all i see is the plain graph
What's "the plain graph"? How does it differ from "Graph 1"?
the plain graph is the graph which is first initialized .
>> ...only after select another item then go to first item, i get to see the first graph...
-->>So, there's a problem with ONLY the 1st graph. Correct?

actually, i change the forth graph on item 4 to be first..and still got same problem..
when the problem appears , the first item of the list gets highlighted...

i am assuming that if that item is highligted, the list "thinks" that first option is chosen ..so,it should display..
>> actually, i change the forth graph on item 4 to be first..and still got same problem..
Of course I didn't meant "Graph 1" when I said that. I just meant: the first graph in the list.

>> actually, the list got 4 graphs:
>> the first one is always highlighted.
Automatically you mean? You don't have any code to select it?
Ooohh. I see:

Please place this line

 list.setSelectedIndex(0);

AFTER

 list.addListSelectionListener( new ListSelectionListener() {
 });

and your problem will be solved.
>> actually, the list got 4 graphs:
>> the first one is always highlighted.
Automatically you mean? You don't have any code to select it?

yup..i used the list code above and the first item gets highlighted..

listModel = new DefaultListModel();
         listModel.addElement("Graph 1");
         listModel.addElement("graph 2 ");
         listModel.addElement("graph 3");
        listModel.addElement("graph 4");
         

          //Create the list and put it in a scroll pane.
          list = new JList(listModel);
          list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
          list.setSelectedIndex(0);
----->>>
  list.setSelectedIndex(0);
i just changed this to (2) and the third graph gets selected..this should select the graph
just saw this
Reason:

When you programmatically select the 1st item in the list [ list.setSelectedIndex(0); ]
you should make sure the list selection listener is already added/active on the list.
So, you should really change that order:

First add the list selection listener, then select the 1st element.
Then, the valueChanged() will be triggered (and your graph will be visible).
In the other case it isn't. That's why your graph wasn't visible
So, in case you have missed my comment:

Please move

 list.setSelectedIndex(0);

AFTER

 list.addListSelectionListener( new ListSelectionListener() {

        ....

 });

and your problem will be solved.
Please place this line

 list.setSelectedIndex(0);

AFTER

 list.addListSelectionListener( new ListSelectionListener() {
 });

and your problem will be solved.
----->>>>

actually, the blank graph gets displayed..then when i click run, that container for that graph is not refreshed..it is still with old graph..i would need to go to the next ooption and then go back...
>> actually, the blank graph gets displayed
"blank graph"?

>> then when i click run, that container for that graph is not refreshed.
Can I see that switch statement?
Previously you said that all graphs displayed well.
I can't believe that moving
        list.setSelectedIndex(0);
makes this no longer work...
???
sorry..i am confusing you...

actually, i just realized whaat is going on..

the values should be generated once the program is runned once..however, when i change the setselected to index(0), blank graph is displayed because the programis not runned . only after the program is runned once, the graph that is being highlighted would  be blank..becasue it is not refreshed..only by changeing to the next option/...it becomes the correct one,,
pls correct me if i am wrong..
I only understand parts of it.
But I *think* I understand the base line:

       list.setSelectedIndex(0);

leads to a blank graph because the <Run> button is not yet pressed (=the graph data is not yet collected)

Right.

Well, what can I say?
Make sure that data is available when you do

         list.setSelectedIndex(0);

I find it rather strange that the user has to press that "Run" button.
But you'll probably have your reasons.
I would do it like this: disable the list as long as the "Run" button is not yet pressed.  [ setEnabled(true); ]
(That prevents them from selecting items)
Once it is pressed, enabled the list again [ setEnabled(true); ]

But, then I wonder why is "getting the graph data" (=actionPerformed() from your "Run" button)
is not the first thing you do when the program starts.
?
great :-)

actually, user need to enter data and then press the run button . and then the data entered by user gets generated on the graph ...
actually, in run got swing worker and then got lots of method..that method calculate the values that is entered by user..

then outside the run action performed..
got the list action listener
Well, good GUI design guides the user in his actions.
So, disable what he has not (yet) access to.
(in your case: disable the list until he has pressed the run button)
When he presses the run button:
1) Get the data
2) enable the list
3) select the 1st graph in the list (which will make the 1st graph appear)
thanks a lot for helping  and teaching.. thankyou
You're welcome.
Thanx 4 axxepting
---->>
When he presses the run button:
1) Get the data
2) enable the list
3) select the 1st graph in the list (which will make the 1st graph appear)
got it :-)