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;)
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 JTabbedPane (say myTabPane) having one tab (lets take only one tab for clarity sake). While creating the JTabbedPane, I added a JPanel (say panel_A) to this tab. I have a button on this JPanel. The tab displays my JPanel perfectly with the button on it. So far so good.
I have defined a listener on the button which creates an instance (say panel_B) of another class extending JPanel. This JPanel has got a different set of components on it. I want panel_B to super-impose panel_A. That is, JTabbedPane's tab should show panel_B and hide panel_A.
Please note that I am able to display panel_A OR panel_B when I "bind" the respective panel (one of them) to the tab during creation of the JTabbedPane. However, I want a selective display (or binding, whichever is possible) of only one of the panels with a button-click (ie. at runtime).
How can this be achieved?
Thank you!
This will do what you need:
myTabPane.removeTabAt(0);
myTabPane.addTab("B", panelB);
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.
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.
I have a JPanel that I want to add some components. in particular JButtons to at runtime based on the content of a user supplied file.
I can add compontents to panel if I call it from the constructor of the JFrame derived form class, even after everything else have been constructed, but If I read the file first and then add components to the panel the call succeds but the added components are never shown.
Does anybody know how I force Java to do as I want?
Call the method validate() on the JPanel after you have added the JButtons to it.