I have a main class in java which call a JFrame Two.
But before call this JFrame Two, my main check a condition and if it's true, it call a JFrame One.
So, my main don't extends JFrame.
I want that my JFrame One stop my main until it been closed, then my main could call my JFrame Two.
I tried to make my JFrame One as a JDialog modal, but my main still running (probably because it isn't a JFrame ?)
Here a simplified part of my code :
File file = new File(PTA);
if (!file.exists()) {
FrameOne fo = new FrameOne(); //FrameOne Extends JFrame
}
FrameTwo ft = new FrameTwo();
So like you can see, my main will always call FrameTwo.
But i want that until FrameOne was closed, the main class stop running, so it don't call FrameTwo until FrameOne was closed.
I don't really know how a JDialog works, i tried to convert my JFrameOne in a JDialog with "setModal(true)" but my main class still running too.
Please can you help me ? My project is blocked by this problem...
Thanks for your attention and your help.
Regards,
Maxime OZENNE.
i tried to convert my JFrameOne in a JDialog with "setModal(true)" but my main class still running too.
It doesn't work that way, you and you can't make a JFrame modal this way (the JFrame doesn't even have this method). Instead you're going to have to use a JDialog and not a JFrame, and create the dialog so that it's modal. Myself I avoid creating classes that extend JFrame or the like and instead gear my code towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.
So on that note, I'd do something on these lines:
MyPanel myPanel = new MyPanel();
// assuming that the parent window JFrame is referenced by "myFrame"
JDialog myDialog = new JDialog(myJFrame, "My Dialog", ModalityType.APPLICATION_MODAL);
myDialog.add(myPanel);
myDialog.pack();
myDialog.setVisible(true);
If this doesn't help, then please create and post your Minimal, Complete, and Verifiable example program, and let us help modify it.
Related
I've searched and could not find the answer I need to do the following: I have two java files: one JFrame, one JPanel. I configured a button in the JFrame to open up the JPanel from within the main frame with a new size of 800,800. Now, I want to close the JPanel and go back to the original JFrame (the one that originally was at size 500,500 with an image). It seems simply straightforward, but I've created an instance of the main frame from within the JPanel and set the jPanel to (this.setVisible(false)). I created a new jFrame object and set its visibility to true. What happens is, a new instance of the JFrame appears alright, but the JFrame at 800,800 with no image still appears as well. I've tried several configurations of getContentPane(), setContentPane() and even tried passing a JFrame parameter to the constructor of the JPanel. I'm not sure where I am going wrong with this, but any help would be much appreciated. All I want is the original JFrame with the original size and image displayed. Thank you in advance.
private void jButton_closeActionPerformed(java.awt.event.ActionEvent evt) {
this.setVisible(false);
mainMenuFrame = new MainMenuFrame();
mainMenuFrame.setVisible(true);
invalidate(); validate();
repaint();
}
you could open and close the jpanel from within your JFrame. the button would be also added to the jframe instead of the jpanel. for easier accessing use the jpanel as member variable
Everytime I open a JFrame from my main JFrame It appears in front of my main JFrame. I know how to open it in different positions of the screen but that doesn't solve the problem.
I have no idea how to open it behind the main JFrame.
Any help would be appreciated.
You can use toBack() method after setVisible(true) method to open New JFrame behind the main JFrame.
Example code:
newFrame.setVisible(true);
newFrame.toBack();
I have a multiple JPanels which are put in a dialogue and after many hours, I am still unable to find the frame in which the JPanels are stored in. I was wondering if there is a method which would return the JFrame (end goal is to call setDefaultCloseOperation() on the JFrame). I was thinking getParent() would do this however I am still unable to call setDefaultCloseOperation no matter how many layers of parents I go through.
There is a utility method for it:
SwingUtilities.getWindowAncestor()
If you add your JPanel to a JFrame, it will be obviously a JFrame instance:
JFrame f = (JFrame) SwingUtilities.getWindowAncestor(panel);
Note: getWindowAncestor() and windowForComponent() provide the same functionality.
Window window = SwingUtilities.windowForComponent(...);
I realize it's pointless to include a button that would do the exact same thing as the 'x' in the window, but I've already worked out the placement of my buttons on my GUI and found having the exit button made things much easier if nothing but a placeholder. And I like the practice.
Ok anyways moving on.
I have a parent JFrame (main class actually) that I'd like to keep running open the entire time the program is being run. This isn't my issue. My problem is when opening a child JFrame. I instantiate it in the main class (it's adding a panel component I created) and I just can't figure out how close said JFrame from within the Panel. Is there an easy way of doing so? I already have the WindowConstant set to Dispose on Close.
What I've done so far is created a method getExit() which returns a boolean value of true. I then have in the main class where the JFrame was instantiated an if/else if statement telling it to set the JFrame visible if exit is False, and dispose of it if true. Doesn't do anything. I'm guessing that's because either it's not bothering to check at all or I coded it poorly.
Any advice?
EDIT: Clarifying what my code is so far without posting it (600 lines of crap to get through). I have my main class Driver(). It's the fairly straight forward main JFrame 'form'.
Said class has several buttons to open up a new JFrame that performs a simple function. We'll name one of those classes (only have 3 total for the Secondary JFrames) Panel(int type) It extends JPanel.
I have a constructor set up depending that takes the int type and hides certain components in my Panel (tried to maximize the panel by combining similar functions). I have a button on the panel that is an exit button. But because the class itself is not a JFrame and does not instantiate itself, I can't dispose of it there. I have to find a way to do so in the main class.
That is my issue.
See Closing an Application. You can use the ExitAction for your JButton and it will be just like clicking the close button of the frame.
I want one JFrame to have a method like this:
private void someEvent(java.awt.event.ActionEvent evt){
//initialize another JFrame
//set the new JFrame to be visible
//set this JFrame to be disabled
}
Which is possible, but I also want the main JFrame to perform something when the newly created JFrame is disposed. I don't want to pass in the main JFrame to the new JFrame however. Is this possible?
Instead, use CardLayout to switch between the two desired content panes. There's an example here.
Don't have one JFrame create and display another JFrame. Instead the second window should be a JDialog, either modal if you want the first window frozen until the second has been dealt with or non-modal if otherwise. If modal then the first window's code will resume once the JDialog has been disposed of, and the code flow will start right after the setVisible(true) call on the dialog. If non-Modal, you'll likely want to add a WindowListener to the dialog.
For example, please check out my code here, here, and here.