How do I go about setting a JFrame class relative to another JFrame class?
I know that if I create a JFrame in the same class as the one I want to center I call setLocationRelativeTo(this), but due to the class getting long I cut it down into smaller JFrame classes.
So far i'm having to use setLocationRelativeTo(null) to center it but this is not very good when I want the JFrame that is popping up to be ontop of the main JFrame.
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
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 have been working on a series of JPanel and I wanted to add a JFrame from another class to one of my panels. Can this be done.
No, you cannot add any Window to another component, doing so results in a RuntimeException. What you can do is add the contentPane from the JFrame to your JPanel.
I have two JFrames. Both are visible at same time.
One JFrame takes the whole screen..its just plain white. (it is acting as a background). And other JFrame is a small box with buttons/texts and other swing components.
The problem I get is when I click the big JFrame area, the JFrame box minimizes. So how do I specify java to make sure the JFrame box is always on top of the JFrame background?
Use a JInternalFrame
Make the JFrame box a JPanel box.
Your application should only have one JFrame.
JFrame is a TopLevel Component and therefore usually you don't put a JFrame into another. If you want to put your smaller jframe into your bigger I would subclass either JDialog or a JPanel.
In general, an application should only have a single JFrame. Other windows should be dialogs.
The problem I get is when I click the big JFrame area, the JFrame box minimizes.
When you use the dialog make sure you specify the frame as the owner of the dialog:
JFrame frame = new JFrame();
JDialog dialog = new JDialog(frame);
If the main frame is ever minimized, the dialog will also be minimized. When the frame is restored the dialog will always display on top of the frame.
use Jdialog with setModal(false) for your small window ,
probably you want something similar to gimp
look at gimp toolbox , only X at title , means its a Dialog.
hope that's help
first of all, this is more or less my first GUI and ive learned Java for not more then a week, so it might contain some serious programming errors.
What i have right now is:
Buttons and labels are part of OptionPanel and are on the left, DrawingPanel is about 5x5 px in size and is on the right.
What I am trying to do is a simple test, to get me more familiar with the GUI. The rectangle should be movable and re-sizeable by the user when clicking the respective buttons:
http://www.upload.ee/image/612005/JFrame2.jpg
Right now i have:
JFrame MainFrame - Makes JFrame (Not using the setSize function. using .pack() instead. not sure about it)
JPanel MergedPanel - FlowLayout - Adds JPanel OptionsPanel and JPanel DrawingPanel together and gets injected to JFrame MainFrame
JPanel DrawPanel - This JPanel is responsible of drawing the rectangle.
JPanel OptionPanel - FlowLayout - This JPanel is responsible of the buttons.
Help please.
You should never call setSize() in your code. In Java, you use layout managers to do the layout (read that tutorial).
Subclassing JPanel to implement different parts of which the UI is composed is a good practice, but should not be overdone (it's fine to have an UI class that adds 3 other plain JPanel instances to itself for layout purposes).
Check out MiG Layout : one can make pretty easily java layouts with that.