-3

I would like to display a message onto the screen upon the button press. The message should not have any window and should be displayed at the center of the screen over any content that would be there. Kind of like here. How would I go about achieving such an effect?

I have tried searching everywhere, but the nature of the question would not yield results. Thanks to anyone for their help in pointing me to right direction.

UPDATE: For those wondering. This is the project I am working on, thanks MadProgrammer for your help. You saved my eyes.

Quillion
  • 6,346
  • 11
  • 60
  • 97
  • What do you mean @RyanJ. It is possible with JavaSE too. – afzalex Oct 20 '14 at 19:23
  • @RyanJ can you show an example with at least JavaFX? I want to try it. – Quillion Oct 20 '14 at 19:23
  • Create a window using JavaFX SceneBuilder or some other editor and set the `initStyle` of the stage to `StageStyle.UNDECORATED` before you show it. Picture an error dialog or similar in any windows program that's missing the border and the title bar/buttons. That's what you'll see if you go this route. – Ryan J Oct 20 '14 at 19:28
  • Can someone explain why this is getting downvoted and voted for closing? – Quillion Oct 20 '14 at 19:37
  • Probably because you haven't shown that You've made any effort to find a solution yourself (this is speculation, since I didn't downvote; but there's nothing in your question to indicate this). – AntonH Oct 20 '14 at 19:51
  • @ryan what a load of tosh, of course you can do it Swing – MadProgrammer Oct 20 '14 at 20:06
  • @AntonH Trust me this is not homework question. But I can't start anywhere because I do not know how. I know how to make JFrame and such, but I wanted just text on screen – Quillion Oct 20 '14 at 20:17
  • It's getting downvoted because if you did a 5 second google search you would have found [How to Create Translucent and Shaped Windows](http://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html) tutorial or taken the time to search SO, you would found any number of questions on the same subject...I know I've done a half dozen answers on the subject myself... – MadProgrammer Oct 20 '14 at 20:34
  • @RyanJ With Swing, you can make a transparent window... – MadProgrammer Oct 20 '14 at 20:34
  • @MadProgrammer so I'm misinformed, you've made your point. Now I know, thanks. – Ryan J Oct 20 '14 at 21:34

3 Answers3

3

I can think of at least three ways to do this in Swing...

GlassPane

If you simply want to show content over the current frame, then you can use the current frames glass pane...

GlassPane

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class OverlayTest {

    public static void main(String[] args) {
        new OverlayTest();
    }

    public OverlayTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }
            
                final OverlayPane overlay = new OverlayPane();
                JButton show = new JButton("Show");
                show.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        overlay.setVisible(true);
                    }
                });
            
                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setGlassPane(overlay);
                frame.setLayout(new GridBagLayout());
                frame.add(show);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class OverlayPane extends JPanel {
    
        private JLabel label;
    
        public OverlayPane() {
            setLayout(new GridBagLayout());
            label = new JLabel("1");
            label.setFont(label.getFont().deriveFont(Font.BOLD, 96f));
            add(label);
            setOpaque(false);
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }
    
    }

}

See how to use How to Use Root Panes

JLayedPane

If you want to display the content over a particular component within the current frame, then you could take advantage of the JLayeredPane, which acts a lot like a glass pane for components...

See How to Decorate Components with the JLayer Class for more details

Undecorated Frame

Transparent Frame

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class OverlayTest2 {

    public static void main(String[] args) {
        new OverlayTest2();
    }

    public OverlayTest2() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                final JFrame masterFrame = new JFrame("Testing");

                final OverlayPane overlay = new OverlayPane();
                JButton show = new JButton("Show");
                show.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {

                        JFrame frame = new JFrame();
                        frame.setUndecorated(true);
                        frame.setBackground(new Color(0, 0, 0, 0));
                        frame.add(new OverlayPane());
                        frame.pack();
                        frame.setLocationRelativeTo(masterFrame);
                        frame.setVisible(true);

                    }
                });

                masterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                masterFrame.setGlassPane(overlay);
                masterFrame.setLayout(new GridBagLayout());
                masterFrame.add(show);
                masterFrame.pack();
                masterFrame.setLocationRelativeTo(null);
                masterFrame.setVisible(true);
            }
        });
    }

    public class OverlayPane extends JPanel {

        private JLabel label;

        public OverlayPane() {
            setLayout(new GridBagLayout());
            label = new JLabel("1");
            label.setFont(label.getFont().deriveFont(Font.BOLD, 96f));
            add(label);
            setOpaque(false);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}

See How to Create Translucent and Shaped Windows

Now, if you want the window be filled, then remove the line frame.setBackground(new Color(0, 0, 0, 0));, this is what makes the frame transparent.

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • OverLayTest2! That is exactly what I desired. Thank you so much MadProgrammer. I have never known that one can make, and I couldn't find a single example of such a window because I was googling wrong terms. I will post my program that I made later on as proof that I actually did attempt to do things. – Quillion Oct 20 '14 at 20:36
  • It's a shame you can only favourite questions, not answers. Because I would if I could. – AntonH Oct 20 '14 at 20:53
  • @AntonH Fav the question, so you can find answer again, that's what I do ;) – MadProgrammer Oct 20 '14 at 20:59
  • @Quillion Next time, if up you've done some research, let people know what you've done, "I've been boggling for things like see through window in Java" etc... – MadProgrammer Oct 20 '14 at 21:00
  • @MadProgrammer I know, but sometimes the question isn't great, but an answer is, and I would like to show that I value the answer more than the question that provided the answer. – AntonH Oct 20 '14 at 21:01
  • @AntonH Post a request on Meta-Stackoverflow ;) – MadProgrammer Oct 20 '14 at 21:09
  • @Quillion That should "goggling", but you get the idea ;) – MadProgrammer Oct 20 '14 at 21:09
  • @MadProgrammer So Jon Skeet can have another thing to be first in? :P – AntonH Oct 20 '14 at 21:12
1

This is a tough question to answer because it doesn't really show what you've tried to do, but I will attempt to put my comment suggestion to light and show you a simple example that might be closer to what you want.

Create a simple FXML file (for JavaFX) that defines a pane with a label on it. Give the label an ID and assign a controller to it.

Sample file (TestDialog.fxml), defining a simple pane with a label:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<AnchorPane minHeight="111.0" mouseTransparent="false" opacity="1.0" prefHeight="111.0" prefWidth="244.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="MyController">
  <children>
    <Label fx:id="textLabel" layoutX="22.0" layoutY="25.0" minHeight="13.0" prefHeight="61.0" prefWidth="200.0" text="SOME TEXT" textFill="BLACK">
      <font>
        <Font size="36.0" />
      </font>
    </Label>
  </children>
</AnchorPane>

In your controller class, define a function that will allow you to close the pane. You may or may not want more, depending on your needs.

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.stage.Stage;

import java.net.URL;
import java.util.ResourceBundle;
public class MyController implements Initializable {
    @FXML
    private Label textLabel;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

    }

    public void close() {
        ((Stage)textLabel.getScene().getWindow()).close();
    }
}

Then, in your main code, display the window. This example opens it as a top-level, but you can use it as a child stage too.

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        FXMLLoader myLoader = new FXMLLoader(getClass().getResource("TestDialog.fxml"));
        Parent root = (Parent)myLoader.load();
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.initStyle(StageStyle.UNDECORATED);  // this style sets the stage to have no border or buttons/title bar
        stage.setResizable(false);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

The result looks like this

Dialog with no borders

*Note that this code, as written, does not provide a way to close the dialog. That's an exercise left up to you. This was simply an example used to show you the effect.

Ryan J
  • 8,275
  • 3
  • 25
  • 28
  • @MadProgrammer I have asked him to show me. This is what I desired but with transparent background. Thanks Ryan :) I will take it from here. Didn't know FX could be so handy. – Quillion Oct 20 '14 at 20:20
  • I will admit that most of my expertise is with FX and my comment about not achieving this with Swing was a bit tongue-in-cheek, so it would appear FX was my pitch. It was just a suggestion that was proven to be one of several ways to accomplish the task at hand. – Ryan J Oct 20 '14 at 21:29
  • Initializing the stage style to [StageStyle.TRANSPARENT](http://stackoverflow.com/questions/14972199/how-to-create-splash-screen-with-transparent-background-in-javafx), the scene fill to transparent and the background color of the root node to transparent (as demonstrated in [How to create Splash screen with transparent background in JavaFX](http://stackoverflow.com/questions/14972199/how-to-create-splash-screen-with-transparent-background-in-javafx)) will get a closer result to the image linked in the question. – jewelsea Oct 20 '14 at 21:44
  • @jewelsea I actually didn't get that same behavior when I tried, but I admittedly didn't pursue that. Thanks for the tip. – Ryan J Oct 20 '14 at 21:48
  • @Ryan, probably you were unaware of [RT-38938: Document that Modena uses a non-transparent background by default](https://javafx-jira.kenai.com/browse/RT-38938). – jewelsea Oct 20 '14 at 23:12
1

You could create an HBox and center it with a transparent background color (-fx-background-color: transparent;) and in your initialize function, hide it. On a button press, you can show it and either give it a timer and hide it at the end of the timer, or put a small "x" in the top right or bottom, or wherever, with a setOnAction(event -> { myText.hide() }) kind of thing. Just a thought that maybe this got too complicated when all you wanted to do was display some text.

Jon Sansoucie
  • 521
  • 4
  • 18