简体   繁体   中英

Unable to show JFrame of library

For my Java application I wanted something similar to the Android Toast. To share it with my co-workers and to re-use that code piece in the future, I created my Toast-project as an own project, to be a library that can be imported.

I am not having issues with the toast-project itself. In fact it works like expected, when being tested directly in Eclipse in its main-method.

After that positive feedback I exported it as a library (jar), and imported it in a class of my main project (has a Swing GUI). I called the specific method, which should show the toast, but nothing happens. No error message, just nothing. I added some println 's to see if I reach the code, what I definitely did. So the called method from my library should be executed, but somehow it doesn't. The toast is a simple, undecorated and translucent JFrame . Hence the toast-code is quite simple:

    // class extends JFrame
    public Toast(String msg, int x, int y, int width, int height, Font font, Color background, Color foreground) {

    setUndecorated(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setAlwaysOnTop(true);
    setBackground(new Color(0f, 0f, 0f, 1f / 3f));
    setBounds(x, y, width, height);
    contentPane = new ToastPane(background);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    SpringLayout sl_contentPane = new SpringLayout();
    contentPane.setLayout(sl_contentPane);
    JLabel label = new JLabel(msg);
    label.setFont(font);
    label.setForeground(foreground);
    sl_contentPane.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.WEST, contentPane);
    sl_contentPane.putConstraint(SpringLayout.SOUTH, label, 85, SpringLayout.NORTH, contentPane);
    sl_contentPane.putConstraint(SpringLayout.EAST, label, 385, SpringLayout.WEST, contentPane);
    label.setHorizontalAlignment(SwingConstants.CENTER);
    sl_contentPane.putConstraint(SpringLayout.NORTH, label, 5, SpringLayout.NORTH, contentPane);
    getContentPane().add(label);
}

The method which is used to show the toast from the outside (from my main-project):

public static void showToast(String msg, int x, int y, int width, int height, Font font, Color background, Color foreground, int displayTime)
{
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd =  ge.getDefaultScreenDevice();
    boolean isPerPixelTranslucencySupported = gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT); 

    if(!isPerPixelTranslucencySupported)
    {
        System.out.println("Per-pixel translucency is not supported");
        System.exit(0);
    }

    Toast toast = new Toast(msg, x, y, width, height, font, background, foreground);
    toast.setVisible(true);

    closeToast(displayTime, toast);
}

The last method disposes the JFrame of the Toast after a certain amount of milliseconds:

private static void closeToast(int displayTime, Toast toast)
{
    final Timer timer = new Timer(displayTime, null);

    timer.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            toast.dispose();

        }
    });

    timer.start();
}

My main-project is calling the toast the following way:

public void showToast()
{
    EventQueue.invokeLater(new Runnable(){
        public void run()
        {
            try
            {
            System.out.println("Going to show the toast!");
            Toast.showToast(message, x, y, width, height, font, background, foreground, displayTime);
            } catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    });
}

The println is giving me the feedback, that I am reaching the line Toast.showToast(...) by printing out "Going to show the toast!" .

Does anyone know, why the main-method of the toast-project is showing the toast correctly, but importing it as a library does not?

I found and solved the problem.

The issue was using the static before the methods. Changing the methods from being static , modifying the code a little bit to fit the removing of the static -keyword did it.

While it worked fine when tested directly in the main-method of the Toast-project, the other project, where the Toast-project was imported, on the other hand did not show the toast correctly. Reason: Setting fields in the class-constructor, but trying to show the toast over a static method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM