Hey - Sorry if this is an irritatingly simple problem, but I'm a newbie to the Java language and I can't see where I'm going wrong with this. I get an error with my code when trying to define 'label3' - "cannot find symbol - class Jlabel". It seems to work fine with 'label' and 'label2', but not 'label3' ! Why is this? I would really appreciate any help you could give...

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

// Class definition

public class IfElseDemo extends JPanel implements ChangeListener {

    // Local variable declarations
    private JLabel label, label2, label3;
    private JSlider slider, slider2;
    private int value = 1;
       
    // Constructor method declaration

    public IfElseDemo() {
        
        // Local variable declarations
        int minValue = 0;
        int maxValue = 100;
        int initValue = value;

       // Use the BorderLayout layout manager
       setLayout(new BorderLayout());

       // Instantiate a slider using Swing class JSlider  
       slider = new JSlider(JSlider.VERTICAL, minValue, maxValue, initValue);
       slider2 = new JSlider(JSlider.VERTICAL, minValue, maxValue, initValue);
       // Add the slider to the north portion of the JPanel
       add(slider, BorderLayout.WEST);
       add(slider2, BorderLayout.EAST);

       // Register the slider with Java
       slider.addChangeListener(this); 
       slider2.addChangeListener(this);
       // Instantiate labels reporting the current value of the slider
       label = new JLabel("Value: " + value + "%");
       label2 = new JLabel();
       label3 = new Jlabel ("Value: " + value + "%");

       // Add the labels to the panel
       add(label,BorderLayout.CENTER);
       add(label2.BorderLayout.WEST);
       add(label3,BorderLayout.SOUTH);
       
       // Create a new window using the Swing class JFrame and add this panel
       makeFrame();
    }
.....
   
}

Dani AI

Generated

Good catch by , , and : the error is just Java’s case sensitivity. Beyond that, there’s one more small fix in your snippet that will save you from a second compile error.

  • Use the correct class name for the third label.
  • Pass the layout constraint as a separate argument to add, not as a member of the label.

For example:

label3 = new JLabel("Value: " + value + "%");
add(label2, BorderLayout.WEST);

Why this matters:

  • Jlabel != JLabel. If the compiler cannot match the exact name, you get “cannot find symbol.”
  • BorderLayout.WEST is a static constant on BorderLayout. Writing label2.BorderLayout.WEST tries to access a field on the label that does not exist, which triggers another “cannot find symbol” (or a similar compile-time error).

Quick checklist for “cannot find symbol” in Swing code:

  • Exact casing for types and constants: JLabel, JSlider, JPanel, BorderLayout.WEST.

  • Imports present (yours are fine), but prefer explicit ones while learning: import javax.swing.JLabel; etc.

  • Implement required interfaces. Since you implements ChangeListener, ensure you have:

    @Override
    public void stateChanged(ChangeEvent e) { /* update labels here */ }

When you recompile, always fix the first error first; later errors often disappear once the initial typo or mismatch is corrected.

Recommended Answers

All 4 Replies

Java is case sensitive. Jlabel != JLabel

Check your spelling. Java is case sensitive.

you don't have a capital L in label3 = new JLabel

God Dammit! I KNEW it would be something as blatantly obvious as that.:$

Thanks very much guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.