I am trying to use Java layout managers (I have no external libraries, and am using Java 7) to create a layout where data is provided in rows, where the rows have a fixed height, but the width fills to fit the window. An example is provided below:
The idea is that I will be adding rows throughout the run of the application, and each row will simply be added to the bottom list. If too many rows are added to the window, then a scroll bar will appear (that bit I have sorted).
My issue so far is that if I use a GridLayout for the main JPanel, it will make the rows fill to the entire window, and just make each row smaller as more rows are added. I want it so that regardless of how many rows are added, the rows height will always remain the same.
Each row consists of two columns that should be the same width. So for this I believe a GridLayout would work nicely, but I'm thinking that I need to add the GridLayout to another panel which essentially represents the entire row.
It is preferable that I use Swing layout managers to achieve the desired layout, but if other suggestions are posed, then I will definitely look into them. As numerous people seem to have found in other questions I've seen, Java's layout managers seem to be a bit lacking in some areas, unfortunately I am unable to tell when it is my knowledge that is lacking, rather than the language itself.
My first recommendation would be to use a
JTable
, while it might "seem" complicated, the API has been optimised to be very efficient and handle thousands of rowsIf that doesn't meet your needs, I'd consider using
GridBagLayout
if you don't want to use any 3rd party libraries, the trick is knowing how you might maniplate them to achieve the results you want, for example, the following uses three panels, the main "outter" panel which acts as the primary view and two inner panels. One to hold the content and another to act as a filler so the content is always pushed to the top of the view.Have a look at How to Use GridBagLayout for more details
"But, isn't there a easier way?" I hear you asking! Well, you could use the
VerticalLayout
from SwingLabs SwingX library (or aJTable
)