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.
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 have 2 classes, one is an instanceof JPanel (called xContain) and the other isn't extending anything (called xShape). There is way too much code to paste here and I don't want to be spoon fed, so I'm going to carefully explain it.
I'm trying to call .addActionListener() on an instance of my xShape. However, it won't work since it doesn't have that method. However, this works when I do it to an instance of JButton or Timer object and I found out that's because the method is inherited from class javax.swing.AbstractButton.
What do I have to inherit to my xShape class so I can add an action listener to it?
If your class is supposed to work as a button or menu (as #HovercraftFullOfEels mentioned), then you can declare your class to inherit from AbstractButton:
public class Test extends AbstractButton
and override the addActionListener method:
#Override
public void addActionListener(ActionListener l)
{
super.addActionListener(l);
// ...
}
Edit:
The imports you need are:
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
ActionListeners are for button and menu type objects. If your class is neither of these, then don't try to force a square peg into a round hole.
Instead consider using a MouseListener, not an ActionListener, adding the MouseListener to the container that is displaying your XShape, and make sure that your XShape class has a contains(Point p) method. This way you can have objects of the type check if button press points are contained within them, and thus react.
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.
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() ).