Urgent - java list - pls

hi,

I am trying to use jlist. modified from tutorial. however, i am getting errors . pls help .thanks

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.addListListener( new ListSelectionListener()
                         {
                              public void valueChanged(ListSelectionEvent e)
                              {
                                   if (e.getValueIsAdjusting()) return;

                                   JList theList = (JList) e.getSource();

                                   System.out.print( "list index: " + theList.getSelectedIndex() );
                                   System.out.println( ", value: " + theList.getSelectedValue() );
                              }
                         }
                              );


when i applied to my code , i got error method addListListener((annonymous javax.swing.event.ListSelectionListener))

location: classs javax.swing.JList
Jasbir21Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tommy BraasCommented:
You  need to change:

list.addListListener( new ListSelectionListener()

to
list.addListSelectionListener( new ListSelectionListener()

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jasbir21Author Commented:
hi,

its just that when i modify the code :

if(theList.getSelectedIndex()==0)
                   displayGraph(score);

the graph is not being displayed..
Jasbir21Author Commented:
however, if i put

if(theList.getSelectedIndex()==0)
System.out.println("TEST");

the test gets displayed.

The graph only gets displayed on each time i run the button..
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Jasbir21Author Commented:
For example:

 if(theList.getSelectedIndex()==0)
                   {
                         displayGraph1(score);
                         System.out.println(yscore[0][0]);
                   }
                   else if(theList.getSelectedIndex()==1)
                         displayGraph2(score);

                   else if(theList.getSelectedIndex()==2)
                         displayGraph3();

                   else
                         displayGraph4(stackPopulation);
             }

In order for me to see the graphs, i would need to run the button few times..however, i thought jlist is where i choose the option the graph should be displayed.thanks
Tommy BraasCommented:
You might need to call repaint() on your graphs!
Tommy BraasCommented:
;-D
Manikandan ThiagarajanSenior consultantCommented:
Jaspir,could you please post the full code from beginning, start with import statement.
Jasbir21Author Commented:
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(score);
                     System.out.println(yscore[0][0]);
                }
                else if(theList.getSelectedIndex()==1)
                     displayGraph2(score);

                else if(theList.getSelectedIndex()==2)
                     displayGraph3();

                else
                     displayGraph4(stackPopulation);
           }
       }
 );

actually, i am using jfreegraph..
Jasbir21Author Commented:

import javax.swing.event.*;

import java.lang.ref.WeakReference;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.* ;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;

import org.jfree.chart.renderer.xy.XYDotRenderer;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;

import org.jfree.ui.TextAnchor;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;

import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.plot.Marker;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;


import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.renderer.category.CategoryItemRenderer;

import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.statistics.HistogramDataset;

import org.jfree.ui.LengthAdjustmentType;
import org.jfree.ui.RectangleAnchor;
Jasbir21Author Commented:
public class S extends JApplet
{...
....
Jasbir21Author Commented:
maybe i need to implement listener

public class S extends JApplet implements listener ?? so that it would be sensitive to mouse click...thanks
Manikandan ThiagarajanSenior consultantCommented:
public class S extends JApplet implements MouseListener,MouseMotionListener

this listener is used to implement mouse events
Tommy BraasCommented:
Did you try calling repaint() on the graph currently displayed?
Jasbir21Author Commented:
yup..i tried repaint as well. it does not work..

actually, what happens, is that i need to run the button again to view each selection.

For example, i run the run button once..
then i run it the second time. If the selection is on Graph 2, graph 2 gets displayed.

Lets say i want to see graph 3, i need to put the selection to graph 3 and run the button..

pls help .thanks
Jasbir21Author Commented:
i call repaint by putting the word repaint()

 if(theList.getSelectedIndex()==0)
                { repaint()
                     displayGraph1(score);
                     System.out.println(yscore[0][0]);
                }
Manikandan ThiagarajanSenior consultantCommented:
where is the method

 public void paintComponent(Graphics g) {

// do some painting code
            }

whenever you put repaint() that would call the above method
   
Jasbir21Author Commented:
hi,

i have open a new question at http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21737350.html
pls look at it .thanks
Tommy BraasCommented:
;-D
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.