Ariyan
Ariyan

Reputation: 15158

Cancel button on a java splash screen

Is it possible to have a cancel button on SplashScreen so the user can cancel application start?
How?

Upvotes: 0

Views: 557

Answers (1)

Aniket Inge
Aniket Inge

Reputation: 25705

A splash screen is just a Borderless window(which you can create using JFrame). You can add a panel that will have a JButton which inturn calls System.exit(0); instead of loading the application.

   JFrame frame = new JFrame("");
    JPanel panel = new JPanel();
    JButton cancel = new JButton("Cancel Loading");
    cancel.addMouseListener(new MouseAdapter(){/*add listener, call System.exit(0);*/}); 
    panel.add(cancel);
    JLabel label = new JLabel();
    //set image of the label which will be your application's icon
    frame.getContentPane().add(panel);
    frame.setUndecorated(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 200); //400x200 splash screen
    frame.setVisible(true);

Upvotes: 1

Related Questions