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.
Related
I want to add JLabel to JPanel (say VisualPanel,which is inside a JFrame) based on the action-performed event (JButton) occurred on another JPanel(say JobPanel) (separate class but add to the JFrame).
How to get the VisualPanel object inside the JobPanel to add the JLabel?
I tried importing the JFrame into JPanel and get the VisualPanel instance but somehow i am getting into infinite recursion.
My question is my design approach correct?. If not how should i go about it?
if my design is correct any suggestion in the right direction is highly appreciated. Thank you.
Give VisualPanel a CardLayout with two panels: one panel that has the JLabel and one that doesn't. Give your JButton an ActionListner that will show() the card you want. Then you can use setSelectedIndex() to select the VisualPanel tab. See the tutorial for examples.
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?
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.
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;)
I have used the Netbeans GUI Designer to come up with a JFrame and I wish to pass in two JPanels to the 2 dummy JPanels already placed inside the JFrame using the Netbeans GUI Designer.
However, the code below doesn't work and the JFrame comes up blank. Why is that so?
public Summary_Page(JPanel jp1,JPanel jp2) {
initComponents();
this.jp1=jp1;
this.jp2=jp2;
this.setVisible(true);
}
If this method is inside the class which extends JFrame, then just add the following line inside the Summary_Page() method:
this.add(panel);
You need to add panel to the parent component. If your class extends JFrame for example, use
this.getContentPane().add(panel);
(there is no need to set it visible). JFrame - is a window that has header and buttons(exit, minimize) but you want to add components in white rectangle so you getting content pane at first. If you have another parent component instead of JFrame you can add component this way:
this.add(panel);
Then if you want to add more then one component you need to use one of Layout Managers.