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.
Related
I would like to change my current layout to a new one, more advanced, but I have some issues with it. The current layout is as this:
All the displayed elements are in a panel (bottom panel, there is a top one which just includes JTable but is not relevant in this case) the checkboxes are in a JScrollPaneBox, which border layout is
BorderLayout.CENTER, then the buttons are in a Box and added to the panel with border layout BorderLayout.EAST, and the search field is added to the panel with borderlayout BorderLayout.SOUTH
But this layout is especially 'ugly' when the app is on fullscreen.
I would like to have a layout like this: https://wireframe.cc/Kb05km
How can I add the two labels and add a space between checkboxes and search field? Also how can I limit the maximum width of checkboxes' ScrollPaneBox and the search field?
Thanks!
I need to make a fixed sized for a GridLayout with 100 buttons located in the center portion of a BorderLayout. On the east portion of the border layout is another Gridlayout that keeps shrinking the center component whenever the text is longer then the size of the current JTextAreas located in the east. The JFrame is not resizable also.
Is there a way to get a fixed size for the center component while allowing the JTextArea to still expand?
"I need to make a fixed sized for a GridLayout with 100 buttons located in the center portion of a BorderLayout".
Sorry, but that's not going to work. BorderLayout doesn't work like that. You can nest JPanel containers with different Layout managers to get your desired effect.
"Gridlayout that keeps shrinking the center component whenever the text is longer then the size of the current JTextAreas located in the east."
You should wrap your text area in a JScrollPane, and setLineWrap(true) and setWrapStyleWord(true) on you text area. The last two will set it, so that the line typed wraps when it is reaching the right edge of the text area. Also If you are setting the size to the text area, don't. Instead, use the following constructor to set its size
JTextArea jta = new JTextArea(20, 50); <--- rows, and character columns
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
JScrollPane scroll = new JScrollPane(jta);
container.add(scroll); <--- make sure you don add jta anywhere else
Without more context to your querstion, these are really the only valid suggestions I can make.
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).
I'm making Minesweeper as a school project. It's close to completion, but the only problem now is setting JFrame's size. I just can't figure out a way to set frames to the size I want.
The program looks almost like a Swing version of the original Minesweeper on Windows XP.
The main frame's layout is flow layout. There's a top panel for the time, mines, and reset button. The top panel's using flow layout, and the bottom panel's using grid layout for the buttons.
I set the preferred size of the frame's content pane. Getting the width is easy (The numbers of fields in a row * my button size), but the problem is getting the height right. The frame always go down to the 2nd last row of the minefield.
I also tried pack() but it resizes it to the preferred size of the content pane, which isn't the right size to begin with. What can I do?
Don't have the JFrame (or better its contentPane) use FlowLayout since this won't give the JFrame the best size for its components. Instead why not have it use the default BorderLayout? Your mine cell's will probably have their getPreferredSize() method overridden and thus will direct the size of the enclosing containers. As always, call pack() on the JFrame after filling it with components and before calling setVisible(true) on it.
Set a preferred size for the buttons in the GridLayout and pack() the frame after adding them.
Don't try to manually set the size. You should let each component display at its preferred size and use the pack() method.
The main frame's layout is flow layout. There's a top panel for the time, mines, and reset button
I would use a BorderLayout. Create a top panel and add it to the NORTH.
Then create a panel for the grid and add it to the CENTER. If you have problems with the buttons in the grid resizing then try creating a JPanel as a wrapper panel. Add the buttons to this panel and then add this panel to the CENTER of the frame. The panel will retain its preferred size.
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...