In my application, I have two swing components on top of each other, that look something like this picture. The problem is that the orange component needs a vertical scrollbar, but I want the right edges of the components to stay exactly aligned (and the width can vary as the user makes the app wider or narrower). If I use something like a grid layout, the scrollbar takes up space and then the scrollbar lines up with the right edge of the red component.
I'm thinking I might need to use a scrollbar component and add that separately and use it to control the orange component. Is there a way to attach a scrollbar to another component like that? I would think it would be difficult without using a scrollpane.
I'm also open to any suggestions on how else to approach this.
It shouldn't be too hard to implement the approach you suggested. Wrap the orange component in a JScrollPane, but configure the scroll pane to hide both scrollbars. Then, set the scroll model for the vertical scroll bar in the scroll pane to the scrolling model from the standalone scrollbar. Even though the scroll pane scroll bar is hidden, it will still scroll if the models are linked. See my answer in this question for some code - it's a different application but similar principle.
Alternatively, you could use a JScrollPane with a visible vertical scrollbar, and add a spacing component next to the red component to keep it aligned. I'm thinking you could use a GridBagLayout with two columns. The first row holds the red component and the spacer, and the second row holds the scroll pane with the orange component, which spans both columns. Then, you just have to get the width of the scroll bar component from the JScrollPane and set the preferred width of the spacer to the same value. A drawback with this strategy would be that it could be difficult to keep the spacer size updated if the scrollbar width changes (due to a UI change, for example).
Related
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 need to implement ui for list of contacts like in skype. An contact represented by custom class(JContact) which derived from JPanel. I tried to use different layouts but not received expected result. Main frame has next structure.
JFrame -> JPanel(contactsPanel)-> JScrollPane(scrollContacts)->JPanel(contactPanel)
scrollContacts.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollContacts.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
If use FlowLayout for contactPanel (see FlowLayout in image)
Strange behavior i think, because expected what each contacts will one under one because indicate HORIZONTAL_SCROLLBAR_NEVER for scrollContacts.
If use Grid or Box layout. Layout automatically re-size my panels, it's look very ugly. see Grid&Box layouts image.
Expected result see "expected" image
----SEE IMAGE----
I'm not native speaker, so please sorry for my bad English.Thank you for attention!
Quoting the Swing tutorial:
The FlowLayout class puts components in a row, sized at their preferred size. If the horizontal space in the container is too small to put all the components in one row, the FlowLayout class uses multiple rows
So the result you get is expected.
A GridLayout object places components in a grid of cells. Each component takes all the available space within its cell, and each cell is exactly the same size. If the GridLayoutDemo window is resized, the GridLayout object changes the cell size so that the cells are as large as possible, given the space available to the container.
So the result you get is also expected.
When a BoxLayout lays out components from top to bottom, it tries to size each component at the component's preferred height. If the vertical space of the layout does not match the sum of the preferred heights, then BoxLayout tries to resize the components to fill the space.
So the result you get is also expected.
But, a box layout can contain glue components to avoid that.
I would thus use a vertical box layout, and add a vertical glue as the last component. Read the tutorial.
I have a screen in gwt where a portion of the screen has a scroll panel. There is a header bar at the top and the rest of the screen is in a scroll panel.
Problem is I can only get the scroll bars to appear if I set the absolute height of the scroll panel. The content in the scroll panel is bigger than the scroll panel but the scroll bars don't appear unless I specify the size of the scroll panel absolutely. The problem with this is it does not take into account the size of the browser window...
Thanks, this answer helped me gwt-layoutpanel-size.
Basically, the bottom line is that if you want the scroll panel to resize and maintain the scroll bars all your parent containers must implement RequiresResize so that the scroll panel can listen for the event and act accordingly.
Your flexibility is severely limited when requiring this behaviour as you can only put the scroll panel inside elements that implement RequiresResize/Provides Resize which are the *LayoutPanels...
I'm using JXMultiSplitPane (from SwingX 1.6.2) to implement a three-pane horizontal interface. In the center pane is a JTabbedPane with two tabs: one with a JTextArea (in a JScrollPane, of course) used for entering Markdown code and the other a JEditorPane (again, in a scroll pane) for displaying a rendered HTML preview. When the user switches to the preview pane, the text in the editor is processed and displayed in the preview pane.
My problem is that if I enter text in the editor with long lines, and then switch to the preview, the center pane will expand. Sometimes it's just by a little bit, other times it'll take up more room than is actually on the screen. But if I move one of the resize handles manually, everything will snap back in place.
I've found only two ways to deal with this before it happens:
Manually resize one of the panes before entering any text.
Give the center pane a weight of 1 in the MultiSplitLayout model.
I can't use the second one since it will expand the center pane to take up almost the whole window by default.
Is there a way to fix this?
Update
After a little more testing, even technique (2) doesn't keep the size constant; switching between the two tabs changes the size of the center pane slightly.
I now believe that the problem is partly with the tabbed pane. The JTextArea and the JEditorPane do not have the same size and that JTabbedPane is resizing when I switch between them (since I'm resetting the JEditorPane text every time. This wouldn't be a problem except that JXMultiSplitPane will keep automatically resizing the center pane until the user forces a specific size by resizing manually.
So I should be able to fix the issue by making the size of the JTabbedPane fixed, but still able to be resized by the handle bars. Any tips on doing that?
The MultiSplitLayout is .. a LayoutManager, so you have to understand how it works (me too, not overly familiar with it myself :-)
The basic layout happens according to the component's prefSize, the weights are for distributing excess/missing space relative to the pref. By default, the dividers are "floating", that is they are positioned between the components as layouted by the basic mechanism. The moment a user touches a divider, dividers are "not-floating", comp sized to fit in-between the dividers. That's the reason for you not seeing the size-greed after moving the divider once. So one ways out is to
setup the JXMultiSplitPane as usual, add the components and realize the frame
fix the dividers after the manager has done its initial layout
String layout = "(ROW " +
"(LEAF name=selector weight=0.15)" +
"(LEAF name=center weight=0.7)" +
"(LEAF name=list weight=0.15)" +
")";
JXMultiSpitPane pane = new JXMulitSplitPane((MultiSplitLayout.parseModel(layout))
// add components and realize the frame
...
pane.getMultiSplitLayout().setFloatingDividers(false);
Alternatively, give more weight to the weights - force the layoutManager to use them for the layout itself (instead of only for the distribution of excess/missing space). A side-effect is that the prefSize of the comps might be set (by the layout, which is a no-no-never, but who's perfect ;-)
pane.getMulitSplitLayout().setLayoutByWeights(true);
Not sure which way I would prefer or if/how that could be made easier in the multisplit ..
How would I go about making the length of the tabs automatically resize based on how much room is left in that row of tabs.
Picture:
As you can see the tab's width is based off the text in the tab.
If you need me to explain what I want better then just ask me because I don't know if I made it clear enough.
You can use a custom component and set it's preferred size. For example, in ButtonTabComponent of TabComponentsDemo:
label.setPreferredSize(new Dimension(...));
You have to choose an appropriate dimension based on other aspects of your layout, so it won't be automatic.
I want to define a size for the actual tabbed pan.
The size of the JTabbedPane is a function of the dimensions and LayoutManager of the Container to which it has been added. In the example cited, the default layout of the frame's content pane is BorderLayout, and add(pane) adds it to the center by default.
To accomplish your goal, I see two approaches:
Divide the current width of the enclosing Container among the existing tabs and repaint the tabbed pane, as shown in this example.
Develop your own implementation of TabbedPaneUI and interpret SCROLL_TAB_LAYOUT accordingly.