Skip to content
Advertisement

java swing component cannot be resolved

I took the following code from a tutorial:

import javax.swing.*;
import java.util.Date;

public class SwingGUI {

    public static void main( String[] args )
      {
        JFrame f = new JFrame( "test" );
        f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        f.setSize( 1500, 900 );
        JLabel l = new JLabel( String.format( "%%tT", new Date() ) );
        f.add(l);
        f.setVisible( true );
      }
}

The f.add(l); is highlighted and two errors are shown:

  • The method add(Component) in the type Container is not applicable for the arguments (JLabel)

  • The type javax.swing.JComponent cannot be resolved. It is indirectly referenced from required .class files

Being relatively new to java, I do not really understand what Eclipse is trying to tell me. What can I do to make it work?

edit: The code runs without the line f.add(l);, so the problem is not that JFrame or JLabel are not found. After fiddeling around a little I got rid of the first error, but the second still remains. The component cannot be resolved, because it is indirectly referenced. What does that mean?

Advertisement

Answer

Obviously the problem did not lie in the code, because it worked for everyone else. So I decided do remove and re-install java and eclipse, et voilĂ ! Now it works. Thank you all for your feedback.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement