I want a simple program that can load other programs.
So far, it works great, except that if I close one frame that all the frames get closed (exit with exit code 0). How can I prevent this from happening?
public class AllSketches extends PApplet {
public static void main(String[] args) {
PApplet.main("forMyself.AllSketches", args);
}
// here all sketches we want
P5_BackFaceCulling backFaceCulling;
public void setup() {
}
public void draw() {
}
public void keyPressed() {
if (key == '1') {
// this creates a new window,
// but when I close it then both windows gets closed!
backFaceCulling = new P5_BackFaceCulling();
backFaceCulling.main(new String[]{});
Frame f = backFaceCulling.getFrame();
if (f instanceof JFrame) {
((JFrame)f).setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
}
}
To make it more clear:
If I close one of the 2 then the program exits.
What I want is that if I close the frame on the right that it closes only the frame on the right. (If I close the one on the left then I don't care about the behaviour).
It is due to the setDefaultCloseOperation of Jframe. You have:
DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object.
HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking any registered WindowListener objects.
DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the frame after invoking any registered WindowListener objects.
EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. Use this only in applications.
You may be using EXIT_ON_CLOSE. Try DISPOSE_ON_CLOSE instead.
The sketch I loaded was with P3D.
therefor getFrame() was not returning a JFrame but some other kind of frame.
Related
My users like having multiple JFrames; it allows them to resize the different components and place them wherever they want on the screen. However, I have a request to make all the child windows come to the front together... in other words, lets say they maximize another window in front of all the windows, and then use the task bar to click on just one of the JFrames. How can I set it so that they all come to the front? Note: it is also possible to close the child windows; if they are actually hidden, I do not want them to come to the front. I have a class ApplicationModel that keeps track of whether a window is hidden or not.
Things I've tried:
Using windowActivated() and focusGained() to try to bring them all to the front. This usually results in an infinite loop. The problem is that my eventing framework sends these requests off the Event Dispatch Thread, so any sort of blocking with an AtomicBoolean doesn't last long enough.
The main problem is not that I can't make them come to the front... I have made them come to the front. The problem is that they KEEP trying to come to the front, as bringing a window to the front throws the focusGained and windowActivated events, which creates an endless loop...
Making one window the master, and making the others a JDialog. Unfortunately, either the windows are modeless (and therefore don't come to front with the master window), or they are modal, (and therefore block the master window).
How can I fix either of these problems, or is there an entirely different third solution?
You can use a boolean field as a flag to prevent the infinite loop:
private boolean movingAllFramesToFront;
public void windowActivated(WindowEvent event) {
if (movingAllFramesToFront) {
return;
}
movingAllFramesToFront = true;
List<Frame> frames = getAllApplicationFrames();
for (Frame frame : frames) {
if (!applicationModel.isHidden(frame)) {
frame.toFront();
}
}
event.getWindow().toFront();
event.getWindow().requestFocus();
EventQueue.invokeLater(new Runnable() {
public void run() {
movingAllFramesToFront = false;
}
);
}
Another thing you can try is the new autoRequestFocus property introduced in Java 1.7. I have never tried using it, but here's my understanding of how it works:
public void windowActivated(WindowEvent event) {
final List<Frame> frames = getAllApplicationFrames();
for (Frame frame : frames) {
if (!applicationModel.isHidden(frame)) {
frame.setAutoRequestFocus(false);
frame.toFront();
}
}
EventQueue.invokeLater(new Runnable() {
public void run() {
for (Frame frame : frames) {
if (!applicationModel.isHidden(frame)) {
frame.setAutoRequestFocus(true);
}
}
}
);
}
I have an application with a lot of windows and had a problem similar to yours. My workaround is:
#Override
public void windowActivated(WindowEvent e) {
if (e.getOppositeWindow() == null) {
//front every window
}
}
First I created a class "SlveFrame" (Slve being the name of my app), a child of "JFrame".
public class SlveFrame extends JFrame implements WindowListener {
static ArrayList<SlveFrame> frames = new ArrayList<SlveFrame>();
public SlveFrame () {
addWindowListener(this); / /to make JFrame fire WindowListener's method
}
/ /... every method added from WindowListener
#Override
public void windowActivated(WindowEvent e) {
if (e.getOppositeWindow() == null) { // return null if window is not from my (or Your) work
for (SlveFrame frame : frames) { // if you have no idea what this is, look for "for each loop java" in google
frame.toFront();
}
}
}
/**
* The use of SlveFrame is almost the same as Jframe
*/
#Override
public void setVisible (boolean b) {
if (b)
frames.add(this);
else
frames.remove(this); // may raise an exception if you're not careful
super.setVisible(b); // or your window will simply not be visible.
}
#Override
public void dispose () {
frames.dispose(this) // may raise an exception you'll want to handle
}
}
The trick being that WindowEvent.getOppositeWIndow() returns a Jframe if the JFrame (or child class) is from your own program, meaning that if you switch to another program or app (such as eclipse, Firefox or a text editor) then back to any of your windows, then a call to getOppositeWindow() will return a 'null'. A simple if (e.getOppositeWindow()) makes it fairly easy to determine whether your window gain focus in condition that would require you to bring every window to the front, or rather to let everything be.
The overriding of setVisible (boolean b) and dispose () are optional but allow the dev to use it as a regular window.
I hope i could be of some help. Sincerly ~a lama
I am making a Java gui project and it consists of two frames.
The problem is that when I call the secondframe from the firstframe, I have set it such that the firstframe visibility is set to false. The problem is how do I make the firstframe visible again by using a button from the second frame.
should i ditch this method and create a new jpanel instead??? Does jpanel have similar capabilities as jframe?
Consider using CardLayout. This way you can switch via multiple UIs without needing another frame. Here's how to use it.
Edit: As Guillaume posted in his comment, this answer from Andrew also covers how to use the layout.
Edit2:
As you requested a little more information about my latest post, here's how such a class may look like:
import javax.swing.JFrame;
public abstract class MyFrameManager {
static private JFrame startFrame,
anotherFrame,
justAnotherFrame;
static public synchronized JFrame getStartFrame()
{
if(startFrame == null)
{
//frame isnt initialized, lets do it
startFrame = new JFrame();
startFrame.setSize(42, 42);
//...
}
return startFrame;
}
static public synchronized JFrame getAnotherFrame()
{
if(anotherFrame == null)
{
//same as above, init it
}
return anotherFrame;
}
static public synchronized JFrame getJustAnotherFrame()
{
//same again
return justAnotherFrame;
}
public static void main(String[] args) {
//let's test!
JFrame start = MyFrameManager.getStartFrame();
start.setVisible(true);
//want another window
JFrame another = MyFrameManager.getAnotherFrame();
another.setVisible(true);
//oh, doenst want start anymore
start.setVisible(false);
}
}
This way you would only instantiate every JFrame once, but you could always access them via your manager class. What you do with them after that is your decision.
I also just made it thread-safe, which is crucial for singletons.
I have a application that launches other applications, something like a dock. The problem is that if the app that I'm launching (JFrame) has the EXIT_ON_CLOSE it will also close my main application.
I have no control what-so-ever over the applications that I'm launching. That is, I cannot expect the application to have a good behavior and use DISPOSE_ON_CLOSE.
What can I do to avoid this? I've tried already to use threads, but no luck. I also tried to put the main application thread in daemon, but no luck too.
I've tried putting a custom SecurityManager overwritting the checkExit method. The problem is that now even the main app can't exit. Also, it doesn`t "work" because applications that use EXIT_ON_CLOSE as their default close operation will throw a Exception and not execute (since Swing checks the Security Manager for the exit -- System.checkExit()), failing to launch :(.
if you want to close only the frame you see, it should be DISPOSE_ON_CLOSE
EDIT:
Try intercepting the EXIT_ON_CLOSE event.
frame.getGlassPane() or frame.getRootPane()
may be useful.
Also if you have right to execute methods of the other app, you can call setDefaultCloseOperation again setting it to DISPOSE_ON_CLOSE
It is a bit of a hack, but you can always use a SecurityManager to manage the other frames.
This simple example prevents a frame from exiting:
import java.awt.*;
import javax.swing.*;
public class PreventExitExample {
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
PreventExitExample o = new PreventExitExample();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationByPlatform(true);
f.setSize(new Dimension(400,200));
f.setVisible(true);
System.setSecurityManager(new PreventExitSecurityManager());
}
};
SwingUtilities.invokeLater(r);
}
}
class PreventExitSecurityManager extends SecurityManager {
#Override
public void checkExit(int status) {
throw new SecurityException("Cannot exit this frame!");
}
}
You can get list of all frames using
Frame's static method
public static Frame[] getFrames()
Iterate the list and check if a list member is instanceof JFrame change default close operation to DISPOSE_ON_CLOSE
Okay, so this might be a stupid question, but I'm new to Java and trying to teach myself things the right way before I develop any bad habits.
Anyway, I was writing a program last night that consisted of a custom class extending Frame and a custom class extending Canvas. The main() method is in the canvas class and I create an instance of the frame class there. The problem is that when the program detects a window close event, I can't dispose the frame because I seemingly have no way to access it from outside the main method. And if I try to define it outside of main(), then I can't use it within. So I ended up skipping dispose() and just using System.exit(0). Is this alright? Is it basically doing the same thing anyway? Or is this a problem I need to fix, and if so, any idea how?
Thanks so much for reading,
Cody
You can get a reference to the frame, from the source property of the event:
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e){
Frame frame = (Frame) e.getSource();
frame.dispose();
}
}
Alternatively, since this is an anonymous class (presumably) declared within the constructor, you also have access to the enclosing instance, so you can also write it as:
class MyFrameClass extends Frame {
public MyFrameClass() {
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
MyFrameClass.this.dispose();
}
});
}
}
Or you can make it simpler still (as your WindowListener does not have a method of its own called "dispose"):
public void windowClosing(WindowEvent e){
dispose();
}
Not a stupid question. Because of the garbage collector its not such a big issue, however, there are some times when you will want to execute some cleanup when a window closes. So some suggestions:
The Window Closing event should be handled from the Frame itself. For instance:
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
//code here to perform as the window is about to close.
}
});
And I would suggest that you create a separate class for your main method that will invoke the Frame etc.
This is used to close Jframe with an event handler.
current Jframe
public class LoginForm extends JFrame
{
LoginForm()
{
//Some code for Jframe and its components.
if(Condition)
disposewindow();
}
private void disposewindow()
{
WindowEvent closingEvent = new WindowEvent(LoginForm.this,
WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(closingEvent);
}
//you can can use for alternate of dispose()event and it post some event handler **Closing event** ,
// if we can use this closing event to open new window with conditions.
//It means closing child window with closing event, get this flag in main window to make main window as Disable or Enable state
}
//In parent window
#Override
public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
this.frame.disable();
}
I have a component (JPanel) inside a Window.
I always get false when calling panel.isShowing(),
when calling from a windowGainedFocus() event (when the parent window gets focus).
I assume that when the windowGainedFocus() event is called, the painting of the JPanel within this Window had not been finished yet.
I was trying to place that call isShowing() on the paint() method of this Window,
but I always get isShowing() = false.
Is there a way I could get an event when the JPanel is fully shown on screen and the isShowing() method will return true ?
Thanks
You should probably best approach this with a hierarchy listener on the panel itself:
panel.addHierarchyListener(new HierarchyListener() {
public void hierarchyChanged(HierarchyEvent e) {
if ((HierarchyEvent.SHOWING_CHANGED & e.getChangeFlags()) !=0
&& panel.isShowing()) {
//do stuff
}
}
});
If you don't want an event but have some specific code that needs to be run after your component has been drawn, you can override addNotify(), which gets called to make the component displayable. Example:
public void addNotify()
{
super.addNotify();
// at this point component has been displayed
// do stuff
}
You component will be fully displayed after you receive WindowListener.windowActivated. You will also run into timing problems and race conditions trying to assign focus before the windowActivated event.