Java Swing - user alerts - java

I am trying to build a user alert mechanism by bringing up the window to the front and then flashing the icon on the screen for the user. I have two questions with regards to this approach:
How can you find the current window you are at in Java and then de-minimize it and bring to front?
Is there a mechanism in Java that would enable me to simply show the icon for a second or two and then hide it, in the middle of the screen? If not, what would be the way to achieve that?
Thanks a lot for any replies.

How can you find the current window you are at in Java and then de-minimize it and bring to front
Window[] allWindows = Window.getWindows();
returns arrays of all Top-Level Containers from current JVM e.g. J/Frame, J/Dialog(JOptionPane), J/Window,
you can to test for (example) if (allWindows[i] instanceof JFrame) {
then WindowState returned WindowEvent
by bringing up the window to the front and then flashing the icon on the screen for the user
use undecodated JDialog (works toFront, toBack) with
create only once time
setDefaultCloseOperations(HIDE_ON_CLOSE)
use Swing Timer for hide JDialog
Is there a mechanism in Java that would enable me to simply show the icon for a second or two and then hide it, in the middle of the screen? If not, what would be the way to achieve that?
have look at Java Translucent Window, put there Icon to the JLabel (or to the JButton)
use Swing Timer for flashing by hiding Icon or swithing bewtween two or more Icons (three or four is good)

I think the simplest way to get the window ancestor is :
SwingUtilities.getWindowAncestor(yourComponent);

Related

Java Swing - What's the proper way of doing a stage based GUI?

I'm currently developing the menu of my application and I would like to know what's the proper way of coding a "stage based menu". What I mean by stage based menu is that, user clicks a button, the entire interface changes to the next "stage". Here are the pictures I designed in Photoshop in order to explain my idea:
First picture would be the first stage and the second picture the second.
Each round looking thing is a JButton
So far I got the main menu (fist picture) made on eclipse using WindowBuilder, made it as a JPanel and then I instantiate it on the window class.
My idea was to have an event listener listen for clicks on each button and then once the even is triggered, have the JPanel variable on the frame change to the next "stage". So I was wondering if this is actually the proper way of doing this or are there any better ways?
You could use a Card Layout to make it easy to swap panels.
Check out the section from the Swing tutorial on How to Use Card Layout for more information and examples.

New window inside a main window?

I am a beginner and I am trying to make a text editor and I want to create a pop up window for text format when I press a menu button where I can put all things like font face, font size , font style etc. Can you tell me how I can make this new window? Thanks for your patience!
For example Notepad:
I think what you're after is a dialog of some kind.
Take a look at How to Make Dialogs for more details.
What I would do is design the basic UI onto a JPanel. I would then add this JPanel to an instance of a JDialog (possibly even using a JOptionPane) and show this dialog, making sure to make it modal, so you can easily retrieve the values set by the user.
This means that you can decide how best to show the user interface or even show it in a number of different ways as it's not constrained to a single top level container
You can simply create a brand spanking new JFrame and it will still be counted as the same application.
Tip: Use Eclipse Window Builder

Specifying the target screen of a lone JOptionPane

I have an application that uses the JOptionPane.show* methods to inform the
user about various conditions before displaying the applications main window.
On a multi screen setup these always show on the first screen. This is usually only
a minor annoyance, but becomes a problem when the 0 screen is off or disconnected.
Normal windows can be placed correctly using the GraphicsConfiguration obtained
via MouseInfo, but I can't find a way to pass that to JOptionPane. I can
not either use the main window to anchor the dialogs, because there is no main
window at that stage of the application startup. Among the possible dialogs is
a warning about obsolete java versions, so displaying the main window before the
errors is not option since the user's java runtime may not even be capable of
displaying the main window.
Is there a way to specify the target screen without reimplementing a major part
of JOptionPane?
You can create a JDialog out of a JOptionPane, and then display the dialog any location that you desire.
As per the JOptoinPane API:
JOptionPane pane = new JOptionPane(arguments);
pane.set.Xxxx(...); // Configure
JDialog dialog = pane.createDialog(parentComponent, title);
// here set the dialog's location
dialog.setVisible(true);
Edit: Alternatively, you could simply create your own JDialog window de novo as per Andrew's great comment (that now no longer exists?).

How to put JFrame into existing JPanel in Java Swing?

I have an open-source java swing application like this:
http://i47.tinypic.com/dff4f7.jpg
You can see in the screenshot, there is a JPanel divided into two area, left and right area. The left area has many text links. When I click the SLA Criteria link, it will pop-up the SLA Criteria window. The pop-up window is JFrame object.
Now, I'm trying to put the pop-up window into right area of the JPanel, so that means no pop-up window anymore, i.e. when I click the SLA Criteria link, its contents will be displayed at the right area of the JPanel. The existing content of the right area of JPanel will not be used anymore. The concept is just same like in the java api documentation page: http://docs.oracle.com/javase/6/docs/api. You click the link in the left frame, you'll get the content displayed at the right frame.
The example illustration is like this:
(note: it's made and edited using image editor, this is not a real screenshot of working application)
http://i48.tinypic.com/5vrxaa.jpg
So, I would like to know is there a way to put JFrame into JPanel?
I'm thinking of using JInternalFrame, is it possible? Or is there another way?
UPDATE:
Source code:
http://pastebin.com/tiqRbWP8 (VTreePanel.java, this is the panel with left & right area divisions)
http://pastebin.com/330z3yuT (CPanel.java, this is the superclass of VTreePanel and also subclass from JPanel)
http://pastebin.com/MkNsbtjh (AWindow.java, this is the pop-up window)
http://pastebin.com/2rsppQeE (CFrame.java, this is the superclass of AWindow and also subclass from JFrame)
Instead of trying to embed the frame, you want to embed the frame's content.
There is (at least) one issue I can see with this.
The menu bar is controlled by the frame's RootPane.
Create you're self a new JPanel. Set it's layout to BorderLayout.
Get the menu bar from the frame (using JFrame#getJMenuBar) and added to the north position of you new panel.
Get the frames ContentPane and add it to the center position of the panel.
There is undoubtedly countless other, application specific issues you will run into trying to do this...
No, you don't want to "put a JFrame into a JPanel" and your illustration above doesn't demonstrate this either. Instead it's showing a subordinate window on top of (not inside of) another window. If you absolutely need to display a new subordinate window, I'd recommend that you create and display a JDialog. The tutorials will explain how to do this, or if you get stuck post your code attempt and we'll help you work with this.
Edit 1
You state:
I need to convert from the pop-up window style into the jpanel content style. It's just like the java api documentation page style: docs.oracle.com/javase/6/docs/api When you click the text in left frame, it doesn't show any pop-up, right? The content is displayed at right frame directly. So that's basicly my goal. The source code is quite big. I will try to paste the source code if possible.
What you are looking for is to simply implement a MouseListener in a JList or JTable, and when responding to the click get the content based on the selection made. This has nothing to do with placing a JFrame in a JPanel and all to do with writing the correct program logic. Again, display it in a modal JDialog -- but that's all secondary to your writing the correct non-GUI logic. You're really barking up the wrong tree here. Forget about JFrames, forget about JPanels for the moment and instead concentrate on how you're going to extract the SLA Criteria data when it is clicked on.
Edit 2
I think I see what you're trying to do -- instead of JFrames and JDialogs, use JPanels and swap them using a CardLayout which would allow you to swap views.
I had skimming the source codes, I saw that the AWindow.java has internal panel (APanel.java) to hold the window's content, and it also has a public method to return the content panel object (getAPanel()). With this, I can use it for fetching the window's contents into other container.
Finally, I decided to use JTabbedPane in the right area of VTreePanel for displaying the pop-up window's contents.
You cannot put a Jframe into a JPanel. Instead you should try to create a separate panel that has functionalities like your JFrame and embed that into your JPanel.
Since you can put a JPanel into another JPanel but not a JFrame into another JPanel

How to create multiple screens in Java?

I have been playing around with trying to get a menu screen for my game. I have only been able to figure out how to paint a new Screen on top of an existing one; meaning that the actual game is still running behind the title screen. I don't need actual code but just a description of how I would go about this.
One way to display a menu would by by using a JDialog outside of your main application window. Take a look at the How to Make Dialogs tutorial for more information.
Another possibility would be to use JInternalFrame for your game and menu so they can be wrapped in a larger application frame. These are explained nicely in How to Use Internal Frames

Categories