Changing BorderLayout in Java Swing for more appealing UI - java

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!

Related

How to align a JTextField that it is always on the far right side of the window?

I have a normal content pane in Java (IDE: Eclipse) and a JTextField.
Now I want the text field to always be on the far right, even if the size of the window changes. Is this somehow possible?
One way to is nest panels with different layout manager.
The default layout manager of the content pane of the frame is a BorderLayout (unless Eclipse changes it, in which case you should set it back to a BorderLayout.
Then you can:
create a JPanel with a FlowLayout that is right aligned.
add the text field to this panel.
add this panel to the BorderLayout.PAGE_START of the content pane.
add you main panel containing other components to the BorderLayout.CENTER.

What layout should I use for chat history

![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.

Which Swing layout manager should I use?

I am creating a Swing dialog window like below:
The header and footer areas have fixed size. The area 1 and 2's size are not fixed. They can have several lines of content and the line number can increase at runtime.
And in future, I may need to add area 3.
How should I use Swing layout managers to achieve this UI?
ADD 1
The area 1 and 2 are independent of each other.
In each of them, I want to place several panels vertically. Each panel hold a line of string. The panel number can vary at runtime.
Use a BorderLayout for the main layout.
add the "header" to the NORTH.
add the "footer" to the SOUTH.
Create another panel and add it to the CENTER. Then set the layout manager of this panel to an appropriate layout manager. We can't advise you what layout manager to use for this panel since we don't know how the two components area related. Then add "area1" and "area2" to this panel.
The key to this answer is that you don't need to use a single layout manager, you can nest panels with different layout managers.
I'd use a BorderLayout for this one:
header in PAGE_START
footer in PAGE_END
area1 & area2 (each in a JScrollPane) in a JSplitPane in the CENTER

How to create a rectangle with multiple images inside?

What is the best way to display let's say rectangle (3x5) with icons 20x20 px.? I want to change the image file of every pic icon later (= it's not just static pictures). I tried to make JFrame full of JPanels, but i was able to display only one panel at a time. I don't want to use GridLayout, because I need just small rectangle inside a frame. Any ideas how to do it? Couldn't find any tutorial or solution. I'm completely new to GUI developement. Thanks
You do want to use a GridLayout. Your problem is that the JFrame you put the icons into uses a BorderLayout by default (and really, you shouldn't change the layout of a top level component).
What this means is that, if you add multiple panels to the frame, without using one of the NORTH, EAST, SOUTH, WEST constraints, only one of the panels will be visible and take up all the space. If you use a GridLayout for that one panel you get, the icons will be stretched, because the panel receives all the space due to the frame's BorderLayout. An alternate layout that doesn't stretch its contents is FlowLayout, but the layout to use depends heavily on your context.
To display the icons, a JLabel is handy. Use an ImageIcon for the label's icon. You can later use setIcon() on the label to choose a new icon.
overall, my approach would be this:
use a JFrame which has a BorderLayout
to the frame, add a JPanel to the frame. The default layout is a FlowLayout, which will prevent the stretching
to the panel, add a JPanel with an appropriate GridLayout
to that panel, add the JLabels, each having an appropriate ImageIcon

Vertical scrolling of panel doesn't work java

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.

Categories

Resources