Reload main JFrame from another JFrame - java

I have a main JFrame containing a JPanel with some stuff in it. Among other things, another JPanel with a list of sub-JPanels and JComponents in it.
What I want to do is to refresh the window (or only the list) by pressing a button in another JFrame.
I tried to do it by
Mainclass.frameIWantToRefresh.invalidate();
Mainclass.frameIWantToRefresh.validate();
Mainclass.frameIWantToRefresh.repaint();
But it doesn't work and I have no idea how to do it in another way.
Any clues?

Related

Open elements and struct of jFrame in another like a menu

i have 5 jFrames in my java project. And i want to make like a Main Menu.
I mean, i want that the program starts with a jFrame and when i click a button insteand of open the jFrame, all the elements like labels, buttons and tables are being shown in my principal jFrame.
And if i click other button the main frame will clean and charge other jframe.
It is possible? im programming with java jdk 8 and netbeans.
Thanks
Edit:
I think who marked duplicate didn't understand my question. I don't want to open or close the frame, or other frames, I want to load the structure and components of several in the same frame. Please read my question before you start complain that is duplicated
i have 5 jFrames in my java project.
And that's a problem.
And i want to make like a Main Menu. I mean, i want that the program starts with a jFrame and when i click a button insteand of open the jFrame, all the elements like labels, buttons and tables are being shown in my principal jFrame. And if i click other button the main frame will clean and charge other jframe.
Yes this can be solved by getting the contentPane (usually a JPanel) from the JFrame whose content you want to display within the currently displayed JFrame, and making it the contentPane of the displayed JFrame, for example:
// create the new JFrame, the one whose content you wish to display
NewJFrame newJFrame = new NewJFrame();
// get its contentPane
Container newContentPane = newJFrame.getContentPane();
// add this content pane into the displayed JFrame
displayedJFrame.setContentPane(newContentPane);
// revalidate and repaint the JFrame so that its new data is well displayed
displayedJFrame.revalidate();
displayedJFrame.repaint();
// displayedJFrame.pack(); // and you might need to do this if sizes are way off
But this extra gymnastics is bulky, prone to bugs and unnecessary. You are painting yourself in a corner by having your class extend JFrame, forcing you to create and display JFrames, when often more flexibility is called for. In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this. More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.
For this situation what I recommend is that you do that, that your GUI classes create JPanels, and that you add the ones that you want to swap to a JPanel that uses a CardLayout. And then whenever you want to show a different "card", call show(...) on the CardLayout object, passing in the JPanel that uses it, as well as the String key that was used when adding the "card" JPanel to the CardLayout-using JPanel. This is all well-explained in the CardLayout Tutorials.
Other useful links:
For rationale on why to avoid manually swapping please see: What's so special about CardLayout vs manual adding/removal of JPanels?
For using a CardLayout to help control a "multi-page" application with multiple classes, please see: How to Integrate Multi-page Java Desktop Application from Multiple GUI Classes

ActionListener to refresh JFrame from a Jpanel

I'm a java beginner, and I'm now on the GUI.
My problem is that to make my gui I've made several panel in different classes like connexion panel, menu panel etc... and for each panel I've a button which should interact with my JFrame window. So I don't know for example how the button in the menu will interact with my JFrame window (which is in a different class) to refresh it and then to put another panel to replace it .If someone can help please?
i'm a java beginner , and i'm now on the GUI , my problem is that to
make my gui
please to read offical Oracle tutorials Using Swing Components before asking a question here in Creating a GUI With JFC/Swing
i've made several panel in different class like connexion panel , menu
panel etc.... and for each panel i've a button which should interact
with my JFrame window .So i don't know for exemple how the button in
the menu will interact with my JFrame window (which is in a different
class )to refresh it and then to put another panel to replace it
seems like as CardLayout is best solution without unwanted side effects

Having a JButton open a new JPanel

I am creating a program which currently has 3 classes. These include the JFrame class in addition to the 2 other JPanel classes. Does anyone know how I can make a JButton in one JPanel class remove that specific JPanel and have the JFrame add in a new JPanel (from another class)?
For example, let's say that the JFrame class is called frame, and the two JPanel classes are called panel1 and panel2. If a button is clicked in panel1, how do you tell the frame to remove panel1 and add panel2?
Well it depends from what you mean with the "remove" word.
A good approach will be to use the MigLayout using the hidemode property. The initial state of your form will contain both panels in the JFrame ,but the second panel will be invisible.
When you press the button of the first JPanel, it will set the visibility of this panel to false, and the visibility of the second panel to true.
On the other hand, if you want to remove completely from the frame the first panel, give a name to it using the method panel.setName(panelsName), and then retrieve every sub-component of the JFrame and put them in a collection.
Then iterate through this collection, and check for the name of every component.
If component.getName().equals(panelsName) then use the frame.remove(component) method to get rid of it. After that, it is possible that you have to call validate and repaint in your frame, though I am not sure if it is necessary. Just try it out;)

Display JFrame inside JPanel in java

Is this possible to add a JFrame into a JPanel in Java.
i m using a java program which is giving output as a frame which i wanted to display inside another program's JPanel on click of a button.
How can i do this?
Read about JInternalFrame. I think it's the way to go here.
http://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html
You could call getContentPane() on the JFrame to extract its main contents as a JPanel (usually) and without the menu bar and decorations, and display that as a JPanel though best would probably be to update the original program so that it produces a JPanel and not a JFrame.

Need to create a WIZARD on Netbeans (jDialog vs CardLayout)?

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!

Categories

Resources