import java.awt.Frame;
import java.awt.SplashScreen;
import javax.swing.JOptionPane;


public class NewFrame extends Frame
{

    public NewFrame() 
    {
        // TODO Auto-generated constructor stub
        SplashScreen splash = new SplashScreen(1000);
        System.out.println("This is working");

    }

/**
     * @param args
     */
    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        //SplashScreen splash = new SplashScreen(2000);
        NewFrame newframe = new NewFrame();


    }

}

This is showing me error

The constructor SplashScreen(long) is not visible

And this is the Splash screen code

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;

class SplashScreen extends JWindow {
  private int duration;

  public SplashScreen(int d) {
    duration = d;

    JPanel content = (JPanel) getContentPane();
    content.setBackground(Color.white);
    int width = 450;
    int height = 115;
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (screen.width - width) / 2;
    int y = (screen.height - height) / 2;
    setBounds(x, y, width, height);  
     content.add(new JLabel("Welcome to Company"), BorderLayout.CENTER);
    //Color oraRed = new Color(156, 20, 20, 255);
     Color oraRed = new Color(190, 10, 10, 255);
    content.setBorder(BorderFactory.createLineBorder(oraRed, 1));

    setVisible(true);

    try {
      Thread.sleep(duration);
    } catch (Exception e) {
    }
    setVisible(false);
  }
  public static void main(String[] args) {
    SplashScreen splash = new SplashScreen(10000);
  }
}

Constructors are public by default ,aren't they?

Dani AI

Generated

Short answer: the code in Post #1 is resolving the simple name SplashScreen to the platform class java.awt.SplashScreen, not the custom SplashScreen shown later. The AWT SplashScreen class is a platform-controlled type that cannot be directly instantiated (it is accessed via getSplashScreen()), so the compiler error about a constructor being “not visible” is coming from that name clash rather than a mysterious Java bug. (docs.oracle.com)

Cause and fixes: a single-type import of java.awt.SplashScreen brings that name into the compilation unit and can hide other types with the same simple name; the JLS explains how single-type imports and name shadowing work. Removing the platform import, renaming the custom class (e.g. AppSplash), or placing the custom class in a named package and importing that package will resolve the collision. Also remember that a constructor with no modifier is package-private; make the constructor public if it must be called from a different package. (docs.oracle.com)

Better pattern for a Swing splash: do GUI work on the Event Dispatch Thread (EDT) and avoid Thread.sleep inside constructors (sleeping the EDT freezes the UI). Use SwingUtilities.invokeLater for GUI creation and javax.swing.Timer to hide the splash after a delay. Example pattern (keeps GUI responsive and avoids blocking):

SwingUtilities.invokeLater(() -> {
  JWindow splash = new JWindow();
  splash.getContentPane().add(new JLabel("Welcome to Company"));
  splash.pack();
  splash.setLocationRelativeTo(null);
  splash.setVisible(true);

  Timer t = new Timer(1500, e -> { splash.setVisible(false); splash.dispose(); });
  t.setRepeats(false);
  t.start();
});

This follows the Swing concurrency guidance and the recommended use of Swing timers. (docs.oracle.com)

Quick checklist (correlates with ’s observations and the posted code from ): confirm which SplashScreen type is being referenced; remove or change the problematic import; make the constructor public if it must be called from another package; prefer EDT + Timer over Thread.sleep for splash timing.

No, they are package access by default, justlike ordinary methods.

The error message refers to a constructor with a long parameter, but the only constructor takes an int.

Creation or modification of Swing components should normally be done on the Swing thread. Sleeping during a constructor while changing a swing component's visibility, all on the main thread is asking for trouble...

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.