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();
Related
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.
I faced this new thing today, and I didn't know why. When I want to show something in panel for example, I just add it to panel; but why I cannot add a table to scroll pane directly, and why I have to call the setviewportview() method? What does add() method do and what does setViewProtView() do?
Basically, you should not use JScrollPane#add.
JScrollPane has a single component already attached to it, a JViewport, this is what the JScrollPane uses to display any component added to the view port.
setViewportView is a convenience method for for JScrollPane#getViewport#setView
The basic concept comes down to the fact that from the scroll panes point of view, it will only show a single component, so add doesn't actually make any sense for it. The method is a consequence of extending from JComponent -> Container
Here is what I tried:
Dragged some JPanels onto a JFrame (using NetBeans inspector window).
In JFrame constructor, made all JPanels invisible using .setVisible(false), except the one I want to show first.
It works and I can easily go from one to another by using some buttons with actionPerformed and adding .setVisible(false) to the current card and .setVisible(true) to the one I want to see.
What I wanted to do now is to use CardLayout previous() and next(), similar to a browser's back/forward. I also would like to reach to a panel from different places, i.e., two panels can link to the same one, so previous panel wouldn't always be the same.
I tried using the following code in an actionPerformed inside JFrame class:
CardLayout cardLayout = (CardLayout) this.getLayout();
cardLayout.previous(this);
However, it doesn't work. What am I missing? Is this supposed to do what I'm looking for?
As you have set the layout of your JFrame to CardLayout, you will need to use the parent container when using its next() & previous() methods. For JFrame the parent container is the content pane. So change:
cardLayout.previous(this);
to
cardLayout.previous(getContentPane());
Declare a variable String previousCard in your JPanel. When you go from CardA to CardB set previousCard variable to "CardA" or whatever the card's name is. So after setting this for all transitions from one card to other, the back buttons will always do the same thing.
cardLayouot.show(getContentPane(), previousCard);
I know to minimize the jframe, I need to use setExtendedState(JFrame.ICONIFIED);
But what I am trying to figure out is how to get to the frame. This dialog is child of a parent dialog. Here is the Constructor.
public EdiBaseDialog(EdiDialogHandler edh, Frame parent, TCSession theSession) {
super(parent, false);
session = theSession;
createDialog();
}
So when I try to add setExtendedState(JFrame.ICONIFIED) command in my jbutton actionPerformed. Which is in a JPanel Method.
I do not know how to address the frame.
??.setState(JFrame.ICONFIED);
Call Dialog.getOwner() from within the dialog.
What you need to do is go up in the component hierarchy until you arrive at the Frame. There are already helper methods in Swing to do this. Try SwingUtilties:
SwingUtilities.getAncestorOfClass(JFrame.class, this);
(Where 'this' can be any component in the hierarchy)
Of course this will only be of use if your dialogs are forming a proper hierarchy (no dialogs using a NULL owner. If thats the case, you have to pass in the Frame through some method or constructor.
I have a JPanel that I want to add some components. in particular JButtons to at runtime based on the content of a user supplied file.
I can add compontents to panel if I call it from the constructor of the JFrame derived form class, even after everything else have been constructed, but If I read the file first and then add components to the panel the call succeds but the added components are never shown.
Does anybody know how I force Java to do as I want?
Call the method validate() on the JPanel after you have added the JButtons to it.