Say I have a JFrame called childJframe.
If I create a new childJFrame from two different JFrames. How can I get which particular JFrame created the childJFrame.
Thus:
public class myPage1 extends javax.swing.JFrame{
// on a Button clicked
childJFrame cjf = new childJFrame();
cjf.setVisible(true);
}
And the Second class is
public class myPage2 extends javax.swing.JFrame{
// on a Button clicked
childJFrame cjf = new childJFrame();
cjf.setVisible(true);
}
How can I find out if cjf is an instance of myPage1 or myPage2?
The Window class, which JFrame descends from, has a getOwner() method that will return the "owner" Window for any child windows.
But having said that, child windows should be JDialogs, not JFrames as your application should have one and only one JFrame, and I believe that JFrames don't have owners, so that this method may return null. If you need to change "views" within the JFrame, use a CardLayout, and if you need to display child windows, use dialog windows such as JDialogs and JOptionPanes. Please read: The Use of Multiple JFrames, Good/Bad Practice?, for more on this.
But having said this, I do have to wonder if your question may in fact be an XY Problem where you ask "how do I fix this code" when the real solution is to use a different (read -- more "object oriented") approach entirely.
If your child windows must really be JFrame instances (I suppose ChildJFrame extends JFrame), I think the simplest solution consists in keeping track of the parent JFrame in the ChildJFrame instance. Since ChildJFrame is a custom class of yours, this is easy:
Add a JFrame (or Frame or Window) attribute to your ChildJFrame class;
Add to ChildJFrame a constructor that takes a parameter that will be assigned to the above attribute;
When you create a ChildJFrame instance from one of your JFrame-derived classes, just add this as a parameter.
Then you have everything you need to interact with the parent JFrame.
Related
I have a main class which extends JFrame, and its content is contained in other classes which extend JPanel.
Now, in those other classes, I want to use several panels to group the content in a good order.
Is it possible to use many panels( by creating several JPanel objects in that class) in this class that extends JPanel?
JPanel extends JComponent, which extends Container, so JPanel is a container, so it contain other Component.
So, yes, you can do this and in fact, depending on the context and requirements, is actually a good idea.
You could take a look at this example and this example
mKorbel makes a valid point. It is generally not recommended to extend from top level containers like JFrame, instead, use something like JPanel as you primary application interface (adding other containers and components to it as you see fit) and adding this frame to an instance of JFrame which you create
So, I need to create a class that extends JDialog, to remove JDialog code from the main functions code.
However, I don't know how to set it's parent JFrame after the JDialog has been created.
It's usually done by passing the JFrame to the constructor, but since it's a custom class, I don't know how to set it to be JDialogs parent. I couldn't find any method of type JDialog.setParent(JFrame); I hope you understand the problem.
Any suggestions?
You can set the parent in the JDialog's constructor or in a setter method. Then when you create the dialog, you'll know the parent by then and can pass it into the parameter. You can then pass the parameter into the super constructor.
As an aside, I've rarely ever had to extend JDialog, JFrame or any top level window but instead prefer to use them "out of the box" and make them when needed.
The parent Window (Frame or Dialog) is called owner in the APIs. You can only mention it in the constructor by calling the super class (JDialog) constructor.
Example:
public class SomeDialog extends JDialog {
public SomeDialog() {
// see other JDialog constructors for other ways to call this
super(parentWindow, title, ModalityType.DOCUMENT_MODAL);
...
}
}
You cannot set it later, after your custom JDialog has been created.
In my Swing app. I have a JFrame with few JPanels. One of it I use for placing another panels. And one of these - another panel - calls a JDialog. Constructor of dialog accepts Frame, String and Boolean as parameters. My problem is how to get parent (which is frame) from this panel?
SwingUtilities.windowForComponent(...) and SwingUtilities.getWindowAncestor(...) do not work in my case. Constructor with no parameters is not an option.
Every JComponent supports the Method getParent(). As the name of the method says, it returns you a reference to the parent of this component. Since JDialog, JPanel, JFrame etc. are Subclasses of JComponent, you can use it in your case.
But be aware that you have to do a type cast, e.g. :
JFrame parentFrame = (JFrame) myContenPane.getParent()
And depending on your layout, you may have to call getParent() multiple times, which is quite ugly.
Hope this helps.
To get current panel parent you can use the following method:
(JFrame)this.getRootpane().getParent();
In the GUI book we use in class there are many examples of how graphical user interfaces are made in Java. So many examples, that I'm very confused regarding which one should be used when it comes down to a big application.
So I've seen examples
in which the main class extends JFrame
where the JFrame object is created inside the main method
where the main class extends JFrame AND implements ActionEvent interface
where Listener classes are declared inside the main class
Sure, I can work with all of these, but right now, as I don't have any kind of experience, I don't see the benefit of using any of them. Is actually one of them the correct way to do it or it depends on my sittuation?
Thank you!
"Is A" or "Has A"? This is the question that should be asked when considering extending a class. If the new class "Is A" frame, extend frame, but if the class just needs a reference to a frame, don't extend.
In fact, if a custom component is required, extend a JComponent or JPanel, then add that to a frame, ..applet, window, JInternalFrame, dialog, constraint of a layout, part of a split pane..
Listeners
As to the listeners. Rather than traverse a huge if/else structure in the single actionPerformed() method to determine the required action, it is more optimal to either:
Create a listener for each control that needs it.
Create an instance of an AbstractAction that might be used for multiple controls ('copy' button, menu item etc.).
Summary
So (generally) for the:
JFrame, don't extend.
Listeners, create and add as needed.
Honestly, it depends on the situation. One basic rule when coding is to "code to abstract classes or interfaces".
So, in a nutshell, have a class extending (or implementing) a JFrame (or whatever interface or class) and/or have one doing the same thing with ActionListener.
It is all about the maintainability, flexibility and cleanness of your code.
Standard approach: use EventQueue in method main, that creates main form. In that case all your operations will be asynchronous
in which the main class extends JFrame
the main calss doesn't have to extend JFrame. if it doesn't you should create a JFrame object like you do with any other class
where the JFrame object is created inside the main method
If the MainClass extend JFrame it created inside the c'tor (in the super() ).
How do I call the same instance of a JFrame say A from 5 different JFrames if I need the display of A to be updated every time I call it??
One way is to provide a reference to the frame to each of the 'child processes' and a public method that will update the UI.
Or since it is better not to extend JFrame, a utility class that has a reference to the JFrame and provides the public method.
BTW: Most apps. would have only a single JFrame. The way to handle the situation you describe might be better implemented using JDialogs or JOptionPanes for the 'secondary' windows. Or to collect all the GUI elements together into the main frame: JDesktopPane/JInternalFrames, CardLayout, JTabbedPane..