Java JFrame Size showSplashWindow(String message, Font messageFont, int duration, Dimension windowSize, Window frameOwner)

Here you can find the source of showSplashWindow(String message, Font messageFont, int duration, Dimension windowSize, Window frameOwner)

Description

Displays a splash window for a specific amount of time with a set of rendering parameters for the message, font type, window size and frame owner.

License

Apache License

Parameter

Parameter Description
message - the text to display
messageFont - the font of text
duration - how long to show the window in milliseconds
windowSize - the size of the window
frameOwner - the parent window or owner, null if none

Declaration

public static void showSplashWindow(String message, Font messageFont, int duration, Dimension windowSize,
        Window frameOwner) 

Method Source Code


//package com.java2s;
/*// w ww  .  ja  va  2  s.c o m
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;

import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.Timer;
import javax.swing.border.EtchedBorder;

public class Main {
    /**
     * <p>Displays a splash window for a specific amount of time with a set of rendering parameters
     * for the message, font type, window size and frame owner.</p>
     * 
     * @param message
     *    - the text to display
     * @param messageFont
     *    - the font of text
     * @param duration
     *    - how long to show the window in milliseconds
     * @param windowSize
     *    - the size of the window
     * @param frameOwner
     *    - the parent window or owner, null if none
     */
    public static void showSplashWindow(String message, Font messageFont, int duration, Dimension windowSize,
            Window frameOwner) {
        JLabel saved = new JLabel(message);
        saved.setHorizontalAlignment(JLabel.CENTER);
        saved.setOpaque(true);
        saved.setFont(messageFont);

        final JWindow window = new JWindow(frameOwner);
        window.add(saved, BorderLayout.CENTER);
        window.setSize(windowSize);
        saved.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
        center(window);
        window.setVisible(true);

        Timer timer = new Timer(duration, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                window.setVisible(false);
                window.dispose();
            }
        });

        timer.setRepeats(false);
        timer.start();
    }

    /**
     * <p>Centers an given Window to the user's screen.</p>
     * 
     * @param w
     */
    public static void center(Window w) {
        int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
        int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;

        int windowWidth = w.getWidth();
        int windowHeight = w.getHeight();

        if (windowHeight > screenHeight) {
            return;
        }

        if (windowWidth > screenWidth) {
            return;
        }

        int x = (screenWidth - windowWidth) / 2;
        int y = (screenHeight - windowHeight) / 2;

        w.setLocation(x, y);
    }
}

Related

  1. restrictWindowMinimumSize(final JInternalFrame frame, final Dimension minSize)
  2. setMinMaxSizeFrame(JFrame frame)
  3. setMinSizeFrame(JFrame frame)
  4. setSizeBasedOnResolution(final JFrame frame)
  5. setSizeWithinScreen(JFrame frame, int preferredWidth, int preferredHeight)