Link to home
Start Free TrialLog in
Avatar of tooki
tooki

asked on

Easy Question (URGENT Please) ....

Hi,
I am new in Java. I have writtena Java program that gives an user-interface.
I have made two JPanels. One JPanel (top) is used for drawing area and the lower JPanel is used for some JButtons, JTextField and one JList. Everything works perfrctly except the ActionListener of the JList...
When I add the ActionListener for the JList, it gives the following error:
"interface javax.swing.event.ListSelectionListener is an abstract class. It cannot be instantiated.
new ListSelectionListener()."
I am new in Java, if you have any suggestion to fix it, please modify the lines in my code.
I have written within /*...*/ the line of codes, creating the problem.
Thanks..
Here is my complete code (In single file):
//***********************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class CustomPanelTest extends JFrame {
   private JPanel buttonPanel;
   private CustomPanel myPanel;
   private JButton b1, b2, b3;
   private JTextField t1;
   private JList randomList;
   public static int myint=0;
   String mychoice = "random";

   private String randomNames[] =
   { "one", "two", "three",
     "four", "five", "six" };
                                                                         
   public CustomPanelTest()                                              
   {                                                                    
      super( "CustomPanel Test" );                                      
                                                                         
      myPanel = new CustomPanel();   // instantiate canvas              
      myPanel.setBackground( Color.green );                              
                                                                         
      b2 = new JButton( "Clear" );                                      
      b2.addActionListener(                                              
         new ActionListener() {                                          
            public void actionPerformed( ActionEvent e )                
            {                                                            
               myint=0;                                                  
               myPanel.draw( CustomPanel.CIRCLE );                      
            }                                                            
         }                                                              
      );                                                                
                                                                         
      b1 = new JButton( "Draw" );                                        
      b1.addActionListener(                                              
         new ActionListener() {                                          
            public void actionPerformed( ActionEvent e )                  
            {                                                            
               if (myint == 0)                                            
               myint = 250;                                              
               myPanel.draw( CustomPanel.CIRCLE );                        
                                                                                    }                                                                                         }                                                                  
      );                                                                    
                                                                             
                                                                             
      b3 = new JButton( "About");                                            
      b3.addActionListener(                                                  
         new ActionListener() {                                              
            public void actionPerformed( ActionEvent e )                    
            {                                                                
               JOptionPane.showMessageDialog( null,                          
              "Robert\n" + "Login: vle" );                  
                                                             
           }                                                
        }                                                    
     );                                                      
                                                                             
      t1 = new JTextField( 10 );                                            
      t1.addActionListener(                                                  
         new ActionListener() {                                              
            public void actionPerformed( ActionEvent e )                    
            {                                                                
              myint = 250;                                                  
              myint = Integer.parseInt( e.getActionCommand() );              
             }                                                              
         }                                                                  
      );                                                                    
                                                                             
      // create a list with the items in the colorNames array                
      randomList = new JList( randomNames );                                
       randomList.setVisibleRowCount( 1 );                                  
      randomList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );  
                                        /*                                    
//Following code creating problem...
      randomList.addListSelectionListener(                                  
        new ListSelectionListener() {                                      
            public void actionPerformed( ActionEvent e )                    
            {                                                              
              mychoice = "one";                                            
              mychoice = e.getActionCommand();                              
                                                                           
            }                                                            
         }                                                              
      );

//Above code creating problem..                               */                          
                                                                         
                                                                         
      buttonPanel = new JPanel();                                        
      buttonPanel.setLayout( new GridLayout( 1, 5 ) );                  
      buttonPanel.add( t1 );                                            
      buttonPanel.add( b1 );                                            
      buttonPanel.add( b2 );                                             buttonPanel.add( b3 );                                              
      buttonPanel.add( new JScrollPane( randomList ) );                  
                                                                         
                                                                         
      Container c = getContentPane();                                    
      c.add( myPanel, BorderLayout.CENTER );                              
      c.add( buttonPanel, BorderLayout.SOUTH );                          
                                                                         
      setSize( 800, 450 );                                                
      show();                                        }                                                            
                                                               
   public static void main( String args[] )                    
   {                                                            
      CustomPanelTest app = new CustomPanelTest();              
                                                               
      app.addWindowListener(                                    
         new WindowAdapter() {                                  
            public void windowClosing( WindowEvent e )          
            {                                                       System.exit( 0 );            
            }                              
         }                                  
      );                                    
   }                                        
}                                                                                    class CustomPanel extends JPanel {                                
   public final static int CIRCLE = 1;                              
   public static int total;                                          
   CustomPanelTest myclass;                                          
   Random r = new Random();                                          
   int xval, yval;                                                  
                                                                     
   public void paintComponent( Graphics g )                          
   {                                                                
        super.paintComponent( g );                                  
     for (int i=0; i<CustomPanelTest.myint; i++)                    
      {                                                              
        xval = r.nextInt() % 800;    
       yval = r.nextInt() % 450;                              
                                                             
       if (xval < 0)                                          
       xval -= 2*xval;                                        
                                                             
       if (yval < 0)                                          
       yval -= 2*yval;                                        
                                                             
       g.fillOval( xval, yval, 5, 5 );                        
                                     
      }                              
                                     
   }                                  
                                     
   public void draw( int s )          
   {                                  
      repaint();                      
   }                                  
}                                    
                                        //**********************************                                                                                                                                                                                                          
                                                                         
Avatar of tooki
tooki

ASKER

Adjusted points to 250

 ListSelectionListener doesnot contain
 any method called "actionPerformed()"
 You did not implement "valueChanged()"
 method. That's why you are getting
 this error.

list.addListSelectionListener( new ListSelectionListener(){
   public void valueChanged( ListSelectionEvent e ){
      if(e.getValueIsAdjusting())
         System.out.println( ((JList)e.getSource()).getSelectedValue());
   }
});
Avatar of tooki

ASKER

Hi,
Thank you very much for helping me. Yes, ListSelectionListener does not conatin any method named "actionPerformed()", but this is one function, I have declared, and it has been defined there itself. Please see the ActionListener() code for the JButton b1(the ActionListener of b1 works perfectly), where similarly I have declared a local function named "actionPerformed()". Also I have not used any method named "valueChanged()", however please IGNORE the above code, and following is the simplified code for the entire file. This code works PERFECTLY when the ActionListener part of the JList is OMITTED! The error with the ActionListener(while included) is the same, that I got previously.
Thanks alot...

Here is the complete code:
//***********************

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class CustomPanelTest extends JFrame {
   private JPanel buttonPanel;
   private CustomPanel myPanel;
   private JButton b1;
   private JList randomList;  
   String mychoice = "random";
   private static int myint=0;

   private String randomNames[] =
   { "one", "two", "three",
     "four", "five", "six" };
                                                                         
   public CustomPanelTest()                                              
   {                                                                      
      super( "CustomPanel Test" );                                        
                                                                         
      myPanel = new CustomPanel();   // instantiate canvas                
      myPanel.setBackground( Color.green );                              
                                                                         
                                                                       
      b1 = new JButton( "Draw" );                                          
      b1.addActionListener(                                                
         new ActionListener() {                                            
            public void actionPerformed( ActionEvent e )                  
            {                                                              
               if (myint == 0)                                            
               myint = 250;                                                
               myPanel.draw( CustomPanel.CIRCLE );                        

}
}                                                                    
      );                                                                      
                                                                             
                                                       
                                                                         
                                                                             
      // create a list with the items in the colorNames array                
      randomList = new JList( randomNames );                                  
       randomList.setVisibleRowCount( 1 );                                    
      randomList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );    
                                        /*                                    
//Following code creating problem...
      randomList.addListSelectionListener(                                  
        new ListSelectionListener() {                                        
            public void actionPerformed( ActionEvent e )                    
            {                                                                
              mychoice = "one";                                              
              mychoice = e.getActionCommand();                              
                                                                           
            }                                                            
         }                                                                
      );

//Above code creating problem..                               */                            
                                                                         
                                                                         
      buttonPanel = new JPanel();                                        
      buttonPanel.setLayout( new GridLayout( 1, 2 ) );                                                                
      buttonPanel.add( b1 );                                                                                            
      buttonPanel.add( new JScrollPane( randomList ) );                    
                                                                           
                                                                           
      Container c = getContentPane();                                      
      c.add( myPanel, BorderLayout.CENTER );                              
      c.add( buttonPanel, BorderLayout.SOUTH );                            
                                                                           
      setSize( 800, 450 );                                                
      show();                                        }                                                            
                                                                 
   public static void main( String args[] )                      
   {                                                            
      CustomPanelTest app = new CustomPanelTest();              
                                                                 
      app.addWindowListener(                                    
         new WindowAdapter() {                                  
            public void windowClosing( WindowEvent e )          
            {
System.exit( 0 );            
            }                                
         }                                  
      );                                    
   }                                        
}

class CustomPanel extends JPanel {                                
   public final static int CIRCLE = 1;                                                                                                                                                                                                                                                                                        
   public void paintComponent( Graphics g )                          
   {                                                                  
        super.paintComponent( g );                                    
        g.fillOval( 10, 10, 5, 5 );                        
   }                                  
                                       
   public void draw( int s )          
   {                                  
      repaint();                      
   }                                  
}

//********************************
ASKER CERTIFIED SOLUTION
Avatar of Jod
Jod

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 version of the code will get you the selected value of the list - you will see it printed out - and put it into mychoice as a string.

//***********************

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class CustomPanelTest extends JFrame {
   private JPanel buttonPanel;
   private CustomPanel myPanel;
   private JButton b1;
   private JList randomList;  
   String mychoice = "random";
   private static int myint=0;

   private String randomNames[] =
   { "one", "two", "three",
     "four", "five", "six" };
                                                                           
   public CustomPanelTest()                                                
   {                                                                      
      super( "CustomPanel Test" );                                        
                                                                           
      myPanel = new CustomPanel();   // instantiate canvas                
      myPanel.setBackground( Color.green );                                
                                                                           
                                                                       
      b1 = new JButton( "Draw" );                                          
      b1.addActionListener(                                                
         new ActionListener() {                                            
            public void actionPerformed( ActionEvent e )                    
            {                                                              
               if (myint == 0)                                              
               myint = 250;                                                
               myPanel.draw( CustomPanel.CIRCLE );                          

}
}                                                                    
      );                                                                      
                                                                               
                                                         
                                                                         
                                                                               
      // create a list with the items in the colorNames array                  
      randomList = new JList( randomNames );                                  
       randomList.setVisibleRowCount( 1 );                                    
      randomList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );    
                                                                             
      randomList.addListSelectionListener(                                    
        new ListSelectionListener() {                                        
            public void valueChanged( ListSelectionEvent e )                      
            {                                                                
              mychoice = (String)(((JList)e.getSource()).getSelectedValue());
              System.out.println(mychoice);
                                                                             
            }                                                              
         }                                                                
      );                                                                            
                                                                           
      buttonPanel = new JPanel();                                          
      buttonPanel.setLayout( new GridLayout( 1, 2 ) );                                                                  
      buttonPanel.add( b1 );                                                                                              
      buttonPanel.add( new JScrollPane( randomList ) );                    
                                                                           
                                                                           
      Container c = getContentPane();                                      
      c.add( myPanel, BorderLayout.CENTER );                                
      c.add( buttonPanel, BorderLayout.SOUTH );                            
                                                                           
      setSize( 800, 450 );                                                  
      show();                                        }                                                              
                                                                 
   public static void main( String args[] )                      
   {                                                              
      CustomPanelTest app = new CustomPanelTest();                
                                                                 
      app.addWindowListener(                                      
         new WindowAdapter() {                                    
            public void windowClosing( WindowEvent e )            
            {
System.exit( 0 );              
            }                                
         }                                    
      );                                      
   }                                          
}

class CustomPanel extends JPanel {                                  
   public final static int CIRCLE = 1;                                                                                                                                                                                                                                                                                        
   public void paintComponent( Graphics g )                            
   {                                                                  
        super.paintComponent( g );                                    
        g.fillOval( 10, 10, 5, 5 );                          
   }                                    
                                       
   public void draw( int s )            
   {                                    
      repaint();                        
   }                                    
}
//********************************

 :-)))))

 tooki , you have to include the
 following checking condition inside
 your valueChanged() function.
 Because valueChanged() func. will
 be called twice for a single click.
 ( 1st time for mouse down , 2nd
   time mouse up )

 
 if(e.getValueIsAdjusting())
Lets get a few concepts right,
/**** Event Model ****/
continued...
Avatar of tooki

ASKER

Hi Jod, sankars98, Jod......

Thank you all very very much.....Thanks a lot for your great explanation...It works....
Still one small question:
Can you tell me why it prints "twice" on the STDOUT the value of "mychoice" for the statement: System.out.println(mychoice);
Also:
In the Public class of the program, I have written:
setSize(800, 450); However, when I execute the .class file and change the size of the screen by using mouse, how can I can I capture the changed size for oth in the x-axis and y-axis dircetion?
(It is helpful for me, because, I am drawing in my drawing area, JPanel and the size of what I draw is based on the setSize value. If I can get the new size, I can probably dynamically change the figure I draw... ..)

Thanks alot....
tooki
Avatar of tooki

ASKER

Hi Jod, sankars98, sgoms....

                     Thank you all very very much.....Thanks a lot for your great explanation...It works....
                     Still one small question:
                     Can you tell me why it prints "twice" on the STDOUT the value of "mychoice" for the statement: System.out.println(mychoice);
                     Also:
                     In the Public class of the program, I have written:
                     setSize(800, 450); However, when I execute the .class file and change the size of the screen by using mouse, how can I can I capture the changed size for oth in the x-axis and y-axis
                     dircetion?
                     (It is helpful for me, because, I am drawing in my drawing area, JPanel and the size of what I draw is based on the setSize value. If I can get the new size, I can probably dynamically change
                     the figure I draw... ..)

                     Thanks alot....
                     tooki
See sankars comment further up, as the reason it is printing twice is my fault - you should have the code like this:

   public void valueChanged( ListSelectionEvent e )                      
   {                                                                  

     if(e.getValueIsAdjusting())  {
       mychoice = (String)(((JList)e.getSource()).getSelectedValue());
       System.out.println(mychoice);
     }                                                                      
   }                                                              
     
....was getting tired so just posted code once it was working with no extras...
Oh yes, and to get the size of the JPanel you are drawing in to just do this:

class CustomPanel extends JPanel {                                  
   public final static int CIRCLE = 1;                                                                                                                                                                                                                                                                                          
   public void paintComponent( Graphics g )                            
   {                                                                    
        super.paintComponent( g );  
        int w = getWidth(); // width of panel
        int h = getHeight(); // height of panel                                  
        g.fillOval( w/2, h/2, 5, 5 );                          
   }                                    
                                         
   public void draw( int s )            
   {                                    
      repaint();                        
   }                                    
}

This will always draw you an oval in the middle of the panel - whatever size you scale it to.

So just scale your graphics relative to the current width and height of the component you are drawing in.

See! a nice short answer for a change...
Avatar of tooki

ASKER

Hi Jod....

Thank you very very much...
tooki