Cannot find symbol class (Java swing)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JanineXD
    New Member
    • Feb 2010
    • 18

    Cannot find symbol class (Java swing)

    Hey,

    I seem to have a problem on a Java Swing Program with ActionListener. I've tried to use getSource in our class but seem don't have an idea why it won't work when I tried to program at home. ( I'm using an earlier version of JCreator in school and a JCreator Pro here at home, Does that affect the program at all? )

    Here's the code:
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Group2 extends JFrame implements ActionListener{
    
    public void Group2(){
    		JLabel text = new JLabel("Enter a number to partition: ");
            JTextField input = new JTextField(15);
     		JButton submit=new JButton("Submit");
    		String listboxitems[]={"5","1111111","111111111","1111111111"};
    		JList listbox = new JList(listboxitems);
    		JLabel timeprocessed=new JLabel("It x milliseconds to process the output");
           	setSize(350, 250);
    		setTitle("Group 2 COPRO2 & DATASTRUCT Project");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
      		FlowLayout layoutManager = new FlowLayout(FlowLayout.CENTER);
            layoutManager.setHgap(10);
            layoutManager.setVgap(10);
            setLayout(layoutManager);
            getContentPane().setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    		getContentPane().add(text);
    		getContentPane().add(input);
    		getContentPane().add(submit);
    		getContentPane().add(listbox);
    		getContentPane().add(timeprocessed);
            submit.addActionListener(this);
    }
    public void actionPerformed(ActionEvent ae){
    if(ae.getSource()==submit){
    		timeprocessed.setVisible(true);}
    else{
    System.out.println("error");
    }
             }
            
        public static void main(String[] args) {
            new Group2().setVisible(true);
    }
        
    
    }
    The error text:
    Code:
    C:\Documents and Settings\User\Desktop\Group2.java:31: cannot find symbol
    symbol  : variable submit
    location: class Group2
    if(ae.getSource()==submit){
                       ^
    C:\Documents and Settings\User\Desktop\Group2.java:32: cannot find symbol
    symbol  : variable timeprocessed
    location: class Group2
            timeprocessed.setVisible(true);}
            ^
    2 errors
    Thank You!
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    This looks like a scope problem. Try defining the Objects submit and timeprocessed outside of the constructor and just initializing them inside:
    Code:
     //...
    
    [b]private JButton submit;
    private JLabel timeprocessed;[/b]
    
    public class Group2 extends JFrame implements ActionListener{
     
    public void Group2(){
            //...
            [b]submit=new JButton("Submit");[/b]
            String listboxitems[]={"5","1111111","111111111","1111111111"};
            JList listbox = new JList(listboxitems);
            [b]timeprocessed=new JLabel("It x milliseconds to process the output");[/b]
            //...
    }
    public void actionPerformed(ActionEvent ae){
    if(ae.getSource()==submit){
            timeprocessed.setVisible(true);}
    else{
    System.out.println("error");
    }
             }
        //...
    }
    Greetings,
    Nepomuk

    Comment

    Working...