check to see if a JFrame is open - java

I am trying to create a chat client for a program of mine and I would like to open a new window for each chat (A & B will not be in the same window as A & C) I have psudocode for what I am trying to do but unsure on how to write it.
Chat client:
inputstream.read()
Read who message from
If (chat window arleady open with person from)
{
Add Message to output section of the window
{
else if (chat window not open)
{
Open new chat window with person from
Display message in new chat window
}
To do this, the thing I have thought about is storing the IPs in an array and then whenever the client gets a message it would look through the array to see if the IP was stored. If it is stored, the window should already be open, otherwise the window needs to be opened. I can program this by using the defaultonclose command from JFrame to just call a removal of the IP when the window is closed, but here are the problems I am running into.
Once I search the array for the IP addresses, If the window is already open (say I have A B and C talking to D) how do I insure if C sends a message that it gets placed in the C window?
1.1 I have thought about spawning a new thread, naming the thread, and then when a message comes in pass it into the the thread that matches the name, but I am not sure about how to do that. I have read the names of threads before but never passed something into a specific thread.
Is there anyway to do this easier without downloading extra libs?

don't use JFrame for another window, because you can't to set parent v.s.owner, use JDialog instead
then JFrame.getOwnedWindows() returns all instances of JDialog(s) where is used JDialog(myFrame owner)
in all cases Window[] allWindows = Window.getWindows(); returns all instances from current JVM, returns all instances of (J)Frames, (J)Dialogs, (J)Windows or JOptionsPanes

Related

Invoke a new window after password verification in Java

I'm working on a RMI client server program. And for my client class I want when I start the client to have a window for verification of the password. If the password is wrong a pop-up message will appear, and if it's correct the current window needs to close and another window with options to work with the server should appear. The code for such an action would be something like that:
//Button for checking password - actionListener
if(!checkPass(btnLogin.getPassword())
JOptionPane.showMessageDialog(null,"ALERT MESSAGE",JOptionPane.WARNING_MESSAGE);
else
// do something so this window closes and a new window,
//with say one button for example, pop-ups
How should I do something like that? The current code is just the back-bone of the client with the remote methods and it also inherits from a jFrame which is currently the password checking window. Keep in mind, that I'm trying to keep all the client gui and functionality in one class. Should the two windows be jPanes and how should I deal with them. I'm lost here so any kind of help is welcomed :)
You want to try and separate and isolate responsibility. That is, the login view should do nothing more then gather the credentials from the user an validate those credentials. It should NOT be responsible for moving the user onto the next view, that responsibility belongs to a different part of you application (or controller)
Wrap the login component in a modal JDialog. From your main class, you can show this dialog, it will block until the dialog is closed (calling dispose on the dialog)
Once the dialog has been closed, and the current user verified,you can create your main application window as per normal
This is an example of a MVC based login dialog, while it might seem complicated, it would be a good lesson in separation of responsibility and introduction in to the Model-View-Controller paradigm

Close Application UI but keep main() code running

I'm trying to close my main application user interface, but leave code running in my main() function that launched the application. Right now the problem I have is on a Mac the program name remains in Mac's menu bar even though there are no windows shown.
So basically in the code that would exit the application I have:
private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
//System.exit(0);
this.setVisible( false );
// Do something here to finish closing application.
}
The main function that starts the application looks like:
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
// NewApplication is a javax.swing.JFrame
new NewApplication().setVisible(true);
}
});
while (true) {
// Watch for user to relaunch UI and do lots of other tasks.
}
}
If I used System.exit(0) it would stop the entire JVM completely and stop running the stuff in the while loop. I cannot figure out how to exit the main application UI, stop from showing in the menu bar, but still run the while loop stuff.
The reason I'm trying to do this is I need something that will run continuously and sometimes the user will need to run a user interface that interacts with the stuff that is running. The stuff inside the while loop checks to see if they are trying to launch the user interface again (among other functions) and would reload it. One option is to make one program that runs continuously and use inter-process communication to talk between the user interface and a non-UI program, but I would need to pass lots of data back and forth so I don't like that option.
It appears there is not an easy way of doing this. For those that have the same problem here are a few options:
1) It looks like other programs I have do this by using Mac’s task bar (in the upper right corner of the screen). The only way you access the program is through a menu on the task bar. Even when you have UI’s shown you get to the UI through the task bar. The downside of doing this is that when the UI is shown you can’t use Cmd+Tab to get over to the window. This is non-intuitive for Mac users. If you want to use this option you can start the java jar file with the command line option “-Dapple.awt.UIElement="true”” and that will prevent the program from showing a menu ALWAYS, and then you'll want to create a task bar icon so the user can get to your program.
See How to hide the Java SWT program icon in the Dock when the application is in the tray
2) Have 2 programs that run, one with a UI and another without. They can communicate using interprocess communication (IPC) using files, sockets, etc. If you don’t have much data to pass between the processes, this is a good solution.
3) You could probably use JNI to remove the menu on the application after all the UI’s close. But you’ll need to dig into Mac’s Objective C language. I can't confirm you can actually do this though.

Returning a dragged applet to the browser window with Javascript

I'm hoping to close the popped out applet to return it to the browser window where it was dragged out of. I'm doing this so that when the user moves from the applet's page, the applet doesn't remain open to them.
I've tried the destroy() method, the stop() method, as well as trying to call removeAll() on the container of the window, though I'm not sure I did that correctly.
Here's part of my code that gets called when the user wants to cancel the applet's execution. Instead of closing the applet, however, I just have it so the applet becomes invisible, though that leaves a big, blank window where the dragged out applet used to be and it's left to the user to close that.
function Cancel(){
document.appletName.cancel();
document.appletName.setVisible(false);
window.location="/*newURL*/";
}
I want that window to close on its own, but I'm not sure what to call.
I don't have access to the source code of the applet, unfortunately, so this has to be done from the browser.
So there is no way to do this in a "zen" sort of way, but having the applet output some sort of informative message that it needs to be closed as well as using setEnable(false) to make it so they can't interact with it forces the user to have to close the applet. Not the most user friendly, but definitely a good way to go if you have users that will insist on dragging the applet out of their window.

Java external display connected callback?

I'm writing a presentation app which is supposed to present on the 2nd display in extended mode.
For that I want to have a "Start presentation" button which reflects the actual state of external display connections: it should be gray and non-clickable if no external displays are connected. And it should be enabled if there is at least one connected.
I have seen the GraphicsEnvironment class but it only has getXXX methods. It's inefficient because I will have to check the state every few seconds and the state updates not immediately.
Is there a callback method on display connection or something like that for this purpose?
Thanks
Shuo

Notify minimised window of an event occurrence in an applet

I have a JApplet which is used for chat. I would like to make it possible that when the applet is minimised and a chat message is received by the user, the minimised window becomes orange (and thus shows the user that something has occurred).
How is it possible to make the applet do this?
Thanks,
Tim
You may have access to the system tray in an applet (I'm not sure). Have a look at the java.awt.SystemTray class - the in-tray lets you pop up messages to the user.
Alternatively you could attempt to cause the Window's toFront method to be called or to "maximize" using the setSize methods (again, I'm not sure what effect this has in an applet). I suspect that the toFront method will be a good bet
Another option I'd look at is raising a JDialog. The presence of this may cause the OS to draw attention to the minimized applet. You could listen to window events representing the screen un-minimizing to clear the dialog so that the user never knew it was there.

Categories

Resources