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.
Related
I have a JDialog and I have it parented to a JFrame. I can move the dialog outside of the JFrame and I want to remove it.
Visual example of the Problem
What I want to happen: Fix
You can add a component listener to the JDialog to implement the componentMoved and componentResized event and when the bounds of the new position is outside your wanted bounds of the associated JFrame you can call the setBounds method to relocate the JDialog...7
Yes you can, please follow this instruction for futher information. Keep Coding! :)
In my application one frame is there on which one button is there. If this button is clicked then second frame will open and it will take some data from the client. And after submitting (Submit Button on Second frame) these data, frame will close.I use JFrame for first frame.
Now what I use for the second frame.
These type of question previously asked on this site but lot of people say that use JDesktopPane, JDialog and other things. I am confused so please clearly specify what is the right way. currently I am using JFrame for first and second frame. But I know this is bad idea to use two Jframe in one application and second frame also not work in that way which I want.
If your answer is to use JDialog then please mention how I customize this.
I am attaching the look of second frame which help you guy to understand what I want in my application.
Use JDialog and make it modal. So when you call it you can process all the results just after cloging it in the same method.
If you are using two frames then in the second frame use setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); instead of setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
I would like to create a "wizard" on a JDialog using the CardLayout, triggered by user pressing the New button from the menubar. In Netbeans I have created a JDialog through which I have a series of jPanels in CardLayout format. In my "New" menu item I wrote the following code to initiate the jDialog as follows,
CardLayout cl = (CardLayout) jDialogNew.getLayout();
cl.preferredLayoutSize(jDialogNew);
cl.show(jDialogNew, "card1");
However, the compiler comes up with the following error,
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
java.awt.BorderLayout cannot be cast to java.awt.CardLayout
If anyone is out there that can take me through creating a wizard on "Netbeans" I'd be eternally grateful
Your jDialogNew has a BorderLayout set as its layout and and not a CardLayout, meaning that when you call getLayout() to try to fit it into a variable that cant hold a BorderLayout an exception is thrown. The classes are different so you cannot cast from one to another, causing a ClassCastException.
A possible solution to this is to set your own layout for the jDialogNew. I dont have code infront of me so I cant check myself, but try looking for a method like setLayout(), and pass in a new layout of your choice.
you can do with following
create JFrame -> Add "CARD LAYOUT"
add JPanels to project. Design JPanels. Customize init code of JFrame. Insert JPanels with this.add(jpanel name). for all jpanels setVisible(false) - then setVisible true which jpanel you want to start with.
The way I did it in Netbeans was very easy! All I had to do was to was to introduce a separate JFrame in my resources package (being a part of my overall package) and in that JFrame I created a JPanel with the CardLayout, under which I created all my other JPanels relating to that top JPanel. Now having the JFrame I could set my fixed canvas plus everything else I needed to construct and activate my CardLayout "Wizard" dialogue box! Then I had to call the new JFrame from with my application whenever the event was triggered. It made life a whole lot easier and it works just great!
I'm developing a desktop application using NetBeans IDE.
1) I'm opening a new JFrame after a button click. While I'm closing any of the opened frames, both are getting closed.
2) And I want to update a new Tabbed JPanel on a JFrame after button click.
Thanks in advance.
Probably you are using the wrong argument. I guess for the JFrames that you want to close without closing the entire application you are doing:
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
While to actually close only the desired frame without closing rest of the frames you need to do:
jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
1) Why did you create lots of Top-Level Containers on Runtime, create only one JFrame and other would be JDialog(Modality) or JWindow(un-decorated), re-use that,
2) really no battery included in my Magic Globe today, for better and sooner hepl you have to post here SSCCE
I want to update a new JTabbedPane on a JFrame after the button click.
You can just update via a reference to the tab's JPanel and call repaint(), but sooner or later you should consider the Model–View–Controller pattern.
I have a JPanel inside a JFrame. I have registered a KeyListener, based on which I want to update the JPanel. The problem I am having is that I cannot get the focus on the JPanel and therefore my KeyListener won't work. I already know that the KeyListener is functional because I registered it with the JFrame and it worked fine. My code goes something like this at the moment:
myFrame.setFocusable(false);
myPanel.setFocusable(true);
myPanel.addKeyListener(myKL);
myFrame.add(myPanel);
Has anyone encountered a problem like this before? Is there something I am missing in regards to this?
P.S.: I do not have any components inside the JPanel I just draw an Image on the background, so I need the focus to be on the JPanel itself and not on something inside it.
Although you're indicating that the panel can be focusable, the panel isn't asking for focus. Try using myPanel.requestFocus();.
Use setFocusable(true) and then requestFocusInWindow(). But the latter must be done after the window containing the panel is made visible, for which you will likely need to register a window listener and do the requestFocusInWindow() in the window activated handler code.
Note: Specifically after the window is visible, not just after calling setVisible(true).
I sometimes face a similar problem. I've noticed that in some cases it is better to make or request focus on a specific control within the panel that is within the frame (e.g., the input box to which you want keyboard input to go), rather than request focus for the pane itself.
Try
panel.setFocusable(true);
panel.setRequestFocusEnabled(true);
// some code here
panel.grabFocus();
Try something like this:
myFrame.addFocusListener(new FocusAdapter() {
/**
* {#inheritDoc}
*/
#Override
public void focusGained(FocusEvent aE) {
myPanel.requestFocusInWindow();
}
});