![two muppets][1]
I want to make a chat history in java swing.
I have a JScrollPane. I append dinamic multiple JPanels in him.
I use BoxLayout but I have a problem:
- When appending the first JPanel it are height 100% of JScrollPane.
- When appending the second JPanel, both are height 50% of total JScrollPane.
I want to make each JPanel have fixed height (40px).
What layout should I use or what should I do?
First off, I'd consider using a JList and not a grid of JPanels.
Your JList cell may easily display what looks like a JPanel view by simply using the right renderer.
And you'd lesson GUI override doing this.
If you must use JPanels, I'd put them in a GridLayout or BoxLayout container (JPanel)
Add this container to a BorderLayout using container (JPanel), to its BorderLayout.PAGE_START position
And then add this final container to the JScrollPane's viewport.
Related
Is there any way to tell a JPanel using CardLayout where to add a component?
Let' s say I have one of these panels in the center of a frame, and I want to display 3 components inside that panel. Would this be possible?
Sure, it's easy. Just put a JPanel as one of the cards, then position the components in the panel using whatever layout works best.
So I'm making a program as a project for school.
In this program I have a panel inside a scrollpane.
When I click a button a panel with info is added to the panel inside the scrollpane.
I can keep adding as much of these panels as I want.
I set the layout of the panel to FlowLayout.
I disabled the horizontal scrolling and set the width of the panel to the width of the scrollpane so I neatly get two of those 'forms' next to each other before it starts a new row of panels.
Problem is the vertical scrolling doesn't activate so I can see only 1,5 rows of panels. (see picture)
http://i1281.photobucket.com/albums/a518/Bas_Van_den_Steen/Screenshot2014-05-22191813_zps44483b9b.png
I suspect this has something to do with the height of the main panel I had to define if I wanted to set a width.
Ideally there should be an option to set the height to 'automatic', but there isn't.
I know scrolling works because when I enable horizontal scrolling and don't set any dimensions for the panel it just keeps adding forms in a single row which I can scroll through.
I think I might need to use another LayoutManager (but I don't have any experience setting those up) or change some of the settings of the scrollpane or main panel.
Can someone help me with this?
I set the width of the panel to the width of the scrollpane so I neatly get two of those 'forms' next to each other before it starts a new row of panels. Problem is the vertical scrolling doesn't activate
A FlowLayout is designed to display components horizontally and the preferred size is always based on a single row of components.
Use a different layout manager. Maybe a vertical BoxLayout, or GridBagLayout or GridLayout depending on your exact requirement.
Read the section from the Swing tutorial on Using Layout Managers for more information and working examples.
set the width of the panel to the width of the scrollpane
You should not be manually setting the preferred width of you panel. As I mentioned earlier that is the job of the layout manager. Maybe the GridLayout is closes to what you need.
I have a JPanel inside of a JScrollPane which is nested inside some other containers. My JPanel uses a modified FlowLayout from SO user jxd in this question.
This may be information overkill but the full nesting of the panel in question is as follows:
JPanel (ModifiedFlowLayout) > JScrollPane > JPanel (GridBagLayout) > JTabbedPane > JPanel (GridBagLayout) > JSplitPane > JPanel (BorderLayout) > JFrame.
The problem is that when I call pack() on my JFrame the JScrollPane/JPanel expands horizontally to fill the entire remaining screen space (across multiple monitors). The space used is more than is needed to display all of the components in the JPanel. I tried using setMaximumSize() on my JPanel but it seems to be ignored in this scenario.
Ideally I would like the panel to have it's size dictated by space left after sizing the components that surround it. Can/how can this been done?
The preferred size of a FlowLayout will try to display all components on a single row. I don't know how the ModifiedFlowLayout works but you can check out Wrap Layout which does the same thing.
However, when using Wrap Layout you can use the setSize(...) method to make a suggestion as to what the initial width should be. Initial wrapping of components should then be based on that size.
I've started doing some experiences with Swing in Netbeans.
I've created two panels and inside each one I've inserted one JLabel.
How can I define the vertical position of my label inside the frame?
Since I'm unable to align the two labels (one on each panel) I would like to set each one to some vertical alignment.
Layouts with layout padding and component borders.
See:
Laying Out Components Within a Container
How to Use Borders
you can use
setBounds(x,y,width,height);
JLabel label = new JLabel();
label.setBounds(10,10,40,20);
I'm building a Swing application in Java using NetBeans and I have a problem with layout. My main frame contains a JScrollPane which contains a JPanel called contentPanel which in turn contains a JPanel called listPanel. The listPanel is empty when the program starts, but when the user interacts with the program an unpredictable number of smaller JPanels are added to it. I've used the NetBeans GUI-builder to snap the top edge of listPanel to the top of contentPanel, and the same with the bottom edges.
The problem I have is that when more components are added to listPanel the vertical scrollbar doesen't appear on my scrollpane. The verticalScrollBarPolicy of my scrollpane is set to AS_NEEDED and its viewportView is set to contentPanel. What I think I need to do is to make contentPanel grow when more items are added to listPanel.
The problem I have is that when more components are added to listPanel the vertical scrollbar doesen't appear on my scrollpane.
The scrollbar will appear when the preferred size of the component added to the scrollpane is greater than the size of the scrollpane. When you add components dynamically you need to tell the scrollpane something has changed. So you basic code should be:
panel.add( subPanel );
panel.revalidate();
Or, because you are adding a panel to the sub panel, you may need to revalidate the scrollpane (I don't remember):
panel.add( subPanel );
scrollPane.revalidate();
The key is the revalidate() which tell the layout manager to recalculate its size.
Use a different LayoutManager. One that will allow for vertical growth like BoxLayout. Also remember that you can use multiple layouts and nest them inside of each other for different effects.