How to add an existing JPanel to a TitledPane - java

I currently have a row of JPanels that I add to a JFrame. I need these panels to be able to expand and collapse as to minimize the clutter on the JFrame.
Right now, I just create the JPanels and add them to the JFrame one at the time.
public void addFolderSearch(FolderSearchComp fsc) {
folderCompPanel.add(fsc, folderCompPanel.getComponentCount());
remake();
}
I've tried adding these FolderSearchComps (the JPanels) to a TitledPane and then adding the TitledPane to the JFrame, but the titledpane.setContent() wont take the JPanel as a parameter.
Is there any way to add a JPanel to a TitledPane and then adding that TitledPane to the JFrame?

is there any swing component similar to the titledpane? or any other
workaround to expand and collapse the jpanel
There is a complete set of Swing components extension in SwingX library which includes titled pane, task pane and collapsible pane, just to mention the ones that you are looking for.
Take a look to this related Q&A: How to create expandable panels using swing?. You might also want to have a look to the demos: SwingLabs demos

Related

Adding content to a JPanel

I'm new to Java and actually designing the GUI for an application.
My main is a JFrame with 5 buttons and 1 panel which will have the "content", for the first button for example, I've designed a Jframe which has a JTabbedPane.
Now I would like to know how can I incorporate the content from that frame to the "content" panel when clicking on the button ?
I tried to use .add but I get:
java.lang.IllegalArgumentException: adding a window to a container
(seems we can't add Jframe to Jpanel).
I also tried the setVisible way but it doesn't meet what I need since it will hide the panel completely and I will get a tiny window with the buttons.
![Jframe content][1]
![Main Jframe with buttons and Jpanel to show the jframe content][2]
The code is generated by netbeans, and I forgot to mention that I did research on adding a Jframe into another Jframe but here isn't my problem at all.
I tried by changing the Jframe by JInternalFrame but clicking on button doesn't do anything.
Button has
contentPanel.add(new GestionUtilisateur());
So basically when you click on the "Gestion Utilisateur" button for example, you get that JTabbedPane that has to appear in the content area (which is blank here)
You should not be putting JFrames inside JPanels. If you have multiple panels you would like to display, depending on something like a button, look in to LAYOUTS.
In particular, it sounds like a CardLayout would work well for your needs. CardLayouts allow you to swap which panel is displayed in a frame by bringing it to the "front" of a list of panels. This would let you display your JTabbedPane on one button click, then click another to change the content pane.
JFrame can not be added in a JPanel.
use JInternalFrame
Make and hold references to JPanels containing your content. A JFrame is really just that, it's a frame (though you can add a single component to it).
You can't add a JFrame to a JPanel. If you want multiple components to be visible use layouts such as BorderLayout, GridBag, etc. Check out some of the Swing layout tutorials here.
Content should be designed as JPanel (you can design it with drag&drop just like JFrame) but if you really have to put a JFrame to JPanel for some reason, you can do it by
myJPanel.add(myJFrame.getContentPane());
however i would suggest modification of your program.

Enclose In option greyed out in Netbeans GUI builder

Building a Java GUI application using Netbeans IDE. I have created a JPanel in it. After adding a lot of fields, I want to also add a JscrollPane to it. I have found that if we right click on the pane we have an option to Enclose it in a container that can be Scroll pane or Split pane etc.
The problem is, that the enclosed button is greyed out when I right click on the panel. How should i fix this ?
I had the same issue. After experimentation, it appears you can only use Enclose In if the Layout is set to Free Design.
However, an alternative method I discovered to accomplish the same thing is to create the empty JPanel and the components you want inside it, all at the same level in the TopComponent. Then drag and drop the components you want into the JPanel, using the Navigator window that shows the component hierarchy.
For example, here I've created a JPanel and a JRadioButton as siblings, and then I drag and drop the JRadioButton inside the JPanel, so that the JPanel is the parent.
Result:

JSplitPane in NetBean GUI builder

I tried to drag a JSplitPane from the Palette in NetBeans but it always shows up on my JPanel as two separate panes with left and right buttons on them.
I could not do anything with the buttons. They were not even components on the Pane. What is going on? How could you make it like normal empty two split panes with nothing one them?
The buttons are just placeholders. First you resize the pane(including those buttons) to what size and position you desire. Then, just drag a JPanel (or any other component) from palette, and drop it on the any of the buttton. That button will be replaced by the JPanel (or component). Then you may do anything as you normally do with JPanel (or that component).

Java: JScrollPane didn't show scroll bars

I am developing a small desktop application in Java using Netbeans. On my jframe i have various pannels and one scroll panes. The purpose of this JScrollPane is to show some visual elements to its users. I achieve this by following the below steps in sequence:
Drag and drop JScrollPane at desired location of my JFrame
Adjust the size of JScrollPane according to my needs.
Write a new java class and extend that class with JPanel
Override the public void paintComponent(Graphics g) method
Then i add that panel to above JScrollPane,
using following code:
JPanel jpnl = new myClass();
jScrollPane2.setViewportView(jpnl);
jScrollPane2.repaint();
Now every thing is working fine as per my requirements, the only thing which is lacking is that when my drwaing is big then no sroll bars are shown at JScrollPane. This is my first application and i don't know much about Java, so any guidence regarding what is missing would be highly appreciated
Remember to add the required component to the JScrollPane object, and the scroll pane object to the panel. Also, it could be that you need to change the scroll bar policy: use scroll pane's setHorizontalScrollBarPolicy() and setVerticalScrollBarPolicy().
Consult the JScrollPane documentation for these methods.

Why does only the last JPanel I add to a JFrame resize?

So I am working on a GUI application using the swing framework. In short, I have 3 JPanels that act as different views of my application. Now the problem is that no matter the order I add the JPanels to my JFrame, only the final JPanel I add resizes when I switch to that view.
Some relevant bits of code:
When creating the window, I first create each individual JPanel, and add it to the JFrame:
JPanel newPanel = new SomeClassExtendingJPanel();
this.jframe.add(newPanel);
Next, whenever I switch between views of the application, I hide the panel that is currently active:
jframe.validate();
jframe.repaint();
oldPanel.setVisible(false);
And then activate the to be shown panel:
jframe.validate();
jframe.repaint();
newPanel.setVisible(true);
Does anyone know what could be wrong?
To resize the JFrame with each swap, you could call pack() on it, but this is kludgy having a GUI resize all the time. A better solution is to use the mechanism that Swing has for swapping views -- a CardLayout. This will size the container to the largest dimension necessary to adequately display all of the "card" components.
Check out the CardLayout tutorial and the CardLayout API for more on this.
JFrame's ContentPane has implemented BorderLayout by Default, and there is possible to put only one JComponents to the one Area,
you have to change used LayoutManager or put another JPanels to the other Areas

Categories