minimizing the jframe window with a jbutton click - java

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.

Related

Closing a Frame from a Panel

Let me first explain the situation,
I have a class with a JPanel called panelclass.
It's method getPanel() returns the JPanel.
In a JFrame class called frameclass, I create a new object of panelclass, got its panel and added it to the frame pane.
What I am trying to achieve is, when a button in paneclass is clicked, It should close this JFrame ie.frameclass.
I donot know how a panelclass can communicate back to the frameclass to close.
I tried this.dispose() and super.dispose() but was not successful even after extending JFrame
Is there a simpler way?
Please do help.
There are a few was to achieve this, but the simplest is probably through the use of SwingUtilities.getWindowAncestor(Component)
This will return the Window that the component was added to or null if it has no parent window. From there you can simply call Window#dispose to close the frame.
when a button in paneclass is clicked, It should close this JFrame
See Closing an Application. I prefer using something like the `ExitAction' described there. The reason is that your application will behave just like the user clicked on the close button of the frame which means that if you have any WindowListeners added to the window they will be invoked.

Why is componentShown() not called?

I have a CardDetailsPanel class which contains several JLabels and JTextFields. This class in contained in a AddCardsPanel and is initialized as follows:
cardDetailsPanel = new CardDetailsPanel(true);
add(cardDetailsPanel, java.awt.BorderLayout.CENTER);
I also have a JLabel that contains instructions. I want to update this label when the CardDetailsPanel first appears and when focus changes to each JTextField. I have found the addFocusListener() method that will work for the later. However, my compenentShown() method isn't working for the former:
addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentShown(java.awt.event.ComponentEvent evt) {
formComponentShown(evt);
}
});
(Okay, I know this is ugly. It was generated by NetBeans.)
private void formComponentShown(java.awt.event.ComponentEvent evt) {
this.frame = (BaseballFrame) this.getParent().getParent().getParent().getParent().getParent().getParent();
}
(Yah, this is even uglier. I'll deal with the chained getParent() calls later. I want to do other things here as well.)
So why doesn't my listener get called? And how do I write a listener that will perform some actions whenever my CardDetailsPanel appears on the screen?
Use an AncestorListener as described in dialog focus.
When a JDialog (or JFrame for that matter) is made visible, focus is placed on the first focusable component by default. There may be times when you want to change this behaviour. The obvious solution would be to invoke the requestFocusInWindow() method on the component you wish to receive focus. The problem is that this doesn’t work all the time.
...
The problem is .. a component can’t request focus unless it has been added to a “realized” dialog. A realized dialog basically means that the Swing JDialog has been added to a peer component that represents a dialog on the underlying OS. This occurs when you invoke the pack() or setVisible(true) methods on the JDialog.
And that is where the ancestor listener comes in handy. For a component in a modal dialog, it will be fired once the component becomes visible, and is realized & focusable.
Edit:
The above comment applies to components in any Swing container, including JFrame and JPanel.

How to get parent for JDialog from JPanel

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();

Change jFrame to jDialog?

I've been trying to change a jFrame to a jDialog so it inherits the icon of the main window but I don't have a clue how to do that. I tried setting it's code from public class jSemestriala extends javax.swing.JFrameto public class jSemestriala extends javax.swing.JDialog but that didn't change the icon of the window. Any ideas? I'm using NetBeans 7.0.1
The JDialog takes its icon from the owner frame.
You have two options:
Create an invisible JFrame, set your icon to it and set that frame as owner of the dialog.
Create the Dialog, get the owner, and set the icon to it.
I would choose the first option, it seems saver to me. The second one makes use of the (shared) owner of the dialog. This could cause side effects.
For further reading.
But if you already have an main frame, you simply need to set it as owner in the constructor of the dialog.
You need to specify the "main window" frame as the owner of the JDialog:
// ownerframe is a JFrame;
JFrame ownerframe = new JFrame();
JDialog dlg = new JDialog(ownerframe);
JDialogs have owner frames. The frame is either created for you if you call the constructor new JDialog(), in which case the frame is invisible; or you supply it to the dialog in its constructor using new JDialog(ownerframe).
create a new jdialog
hard copy the components from design view of jframe(you can use navigator window to copy all in clearly. ofcourse just copy the components under jframe)
paste it on jdialog(again use navigator window . ofcourse paste under jdialog)
hard copy the source code from jframe's source code window
paste it on jdialog's source code
do not touch the automaticly created codes
if you need it, add them again by using design window.
fix the errors in source code window of new jdialog by using your eyes and hands :)
remember all the time: be careful about choosing the type of class form.
sorry for my english.
it could take a long time but it will work.

How to make a resizable JDialog?

I'm using JOptionPane.showOptionDialog to show a JDialog. I would like to know how:
set the dimension of the dialog (for now I'm using setPreferredSize() method on the given panel but I know that such method shouldn't be used).
make the showed dialog resizable.
My code looks like:
JPanel panel; //my JPanel built with dialog contents
int ret = JOptionPane.showOptionDialog(myFrame,
panel,
"titel",
JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
options,
options[1]);
I know that I could obtain the desired result building a JDialog this way:
JDialog dialog = new JDialog(panel);
dialog.setResizable(true);
dialog.setSize(800,600);
dialog.setVisible(true);
The problem with the last solution is that I can't get the return value.
EDIT:
in response to #camickr observations:
Why do you need to set the preferred size? If you build the panel
properly is should be displayed at its preferred size.
I'm not sure of having fully understood Swing on this point. The problem is, for example, that I'm displaying through a JDialog a ChartPanel built with JFreeChart. Now, I suppose that panel has it's own preferred size, but I want to see it bigger. How can I do that without explicitly use setPreferredSize()?
Read the JOptionPane API. Search for "Direct Use". It shows you how to
directly access the dialog used by the option pane and you can
I read it but I can't find the right method to understand which button (Ok or Cancel) has been pressed on the JDialog.
This hack using a HierarchyListener to get access to the JOptionPane's also works:
http://blogs.oracle.com/scblog/entry/tip_making_joptionpane_dialog_resizable
// TIP: Make the JOptionPane resizable using the HierarchyListener
pane.addHierarchyListener(new HierarchyListener() {
public void hierarchyChanged(HierarchyEvent e) {
Window window = SwingUtilities.getWindowAncestor(pane);
if (window instanceof Dialog) {
Dialog dialog = (Dialog)window;
if (!dialog.isResizable()) {
dialog.setResizable(true);
}
}
}
});
Why do you need to set the preferred size? If you build the panel properly is should be displayed at its preferred size.
Read the JOptionPane API. Search for "Direct Use". It shows you how to directly access the dialog used by the option pane and you can
With you second approach why are you setting the size? Again just pack() the dialog and it will be displayed at the panels preferred size.
What do you mean you can't get the return value? You have access to any method of your custom panel. So you can just invoke the getXXX() method when you receive control again. Just make sure the dialog is modal and the code after the setVisible(true) will block until the dialog is closed.
If you want to go the second way completely, you have to create and position your own "YES" and "NO" buttons somewhere (since a raw JDialog is just an empty modable frame). Therefore, you need to attach a MouseListener to both buttons and handle click events. On a click, you will know what button was pressed, and you'll just have to call dispose() on the dialog to close it.

Categories

Resources