Fixed number of item in JScrollPane/JPanel in java - java

I have created a JPanel inside which there is a JScrollPane. I want to display only 3 items there and exceeding these, I can scroll down to view them.
I have used the code below to create the JPanel and the JScrollPane
JScrollPane slide_scroll = new JScrollPane();
slide_scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
slide_scroll.setBounds(1158, 11, 196, 528);
contentPane.add(slide_scroll);
JPanel scrollPanel = new JPanel();
scrollPanel.setLayout(new GridLayout(5,1));
slide_scroll.setViewportView(scrollPanel);
But after adding a 6th item, all the items are displayed in a tabular way instead of a single column. I want that, when the 6th one is added, I use the scroll bar to be able to view it

From the documentation for GridLayout:
When both the number of rows and the number of columns have been set to non-zero values, either by a constructor or by the setRows and setColumns methods, the number of columns specified is ignored. Instead, the number of columns is determined from the specified number of rows and the total number of components in the layout. So, for example, if three rows and two columns have been specified and nine components are added to the layout, they will be displayed as three rows of three columns. Specifying the number of columns affects the layout only when the number of rows is set to zero.
Your layout will always have five rows. The fact that you specified one column is irrelevant.
If you want your layout to always have one column, use new GridLayout(0, 1).

Related

Last column of JTable is getting shrunk

If the JTable is less than the panel size I want to use AUTO_RESIZE_ALL_COLUMNS.
If the table has more columns then I want to keep all the columns to the maximum width with a scrollbar.
But when I try to use the below code, the last column is getting shrunk.
I want to know if I'm missing something.
//Adjust columns according to max width(header,Content)
TableColumnAdjuster tca = new TableColumnAdjuster(table);
tca.adjustColumns();
//Adjust the column to max width with double click if the column is shrinked
new ResizeColumnListner(table);
JScrollPane sp = new JScrollPane(table,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS , ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
table.getParent().addComponentListener(new ComponentAdapter() {
#Override
public void componentResized(final ComponentEvent e) {
if (table.getPreferredSize().width < table.getParent().getWidth()) {
table.setAutoResizeMode(table.AUTO_RESIZE_ALL_COLUMNS);
} else {
table.setAutoResizeMode(table.AUTO_RESIZE_OFF);
}
}
});
The table will be auto resized depending mode value. There are 5 modes:
AUTO_RESIZE_OFF: Don't automatically adjust the column's widths at all. Use a horizontal scrollbar to accomodate the columns when their sum exceeds the width of the Viewport. If the JTable is not enclosed in a JScrollPane this may leave parts of the table invisible.
AUTO_RESIZE_NEXT_COLUMN: Use just the column after the resizing column. This results in the "boundary" or divider between adjacent cells being independently adjustable.
AUTO_RESIZE_SUBSEQUENT_COLUMNS: Use all columns after the one being adjusted to absorb the changes. This is the default behavior.
AUTO_RESIZE_LAST_COLUMN: Automatically adjust the size of the last column only. If the bounds of the last column prevent the desired size from being allocated, set the width of the last column to the appropriate limit and make no further adjustments.
AUTO_RESIZE_ALL_COLUMNS: Spread the delta amongst all the columns in the JTable, including the one that is being adjusted.
From your question , it is not clear how you adding the JScrollPane object to the frame window's content pane. Here is an Exploring Swing's Table Component article to read and get understanding how everything works together.

How can I implement JSplitPane.TOP and BOTTOM constanstants in when adding a JScrollPane to the JSplitPane?

I have two panels , one is JTree in a JScrollPane and the other is JTable in a JScrollPane. How can I place them in adjacent position using the JSplitPane.TOP and JSplitPane.BOTTOM constants so that they must equal space in the main panel?
By using a JSplitPane, you can place one component above the other with JSplitPane.VERTICAL_SPLIT. Create your split pane like below:
splitPane = new JSplitPane (JSplitPane.VERTICAL_SPLIT, jtreeScrollPane, jtableScrollPane);
If you want to give them the same space, you can use:
splitPane.setResizeWeight (0.5);
Check the How to Use Split Panes official tutorial for details.
You can also set a GridLayout as your mainpanel's layout in order to give your scrollpanes the same size.
If you want to put your scrollpanes one above the other you can set 2 rows and 1 column, if you want to align them horizontally you can use 1 row and 2 columns.
See the How to use GridLayout tutorial for further details.

Resize a Jtable Vertically based on it's content

I have a view in my java project that needs to resize a table.
I have n number of Jtables inside a JScrollPane. Some of them doesn't have enough rows to fill the total size of the table. The best solution I think would be to resize the table to fill to the number of rows that were allocated.
1) The table has a dimension of 4x4 but if there is not enough rows (i.e, less than 4) it will have a gap, which I don't want.
Since this table is static and won't be changed how can I resize this table programmatically based on the number of rows that it will have, like having 1, 2 or only 3 rows and not have this gap?
PS: I know how many rows it will have, I just don't know how to resize the table height programmatically during runtime.
Since this table is static and won't be changed how can I resize this table programmatically based on the number of rows that it will have,
After adding all the data to the table you can use:
table.setPreferredScrollableViewportSize(table.getPreferredSize());
Then when the table is added to the scroll pane the size of the scroll pane will be the preferred size of the table.
To make the JTable stretch to fit the height of the view, you can call:
table.setFillsViewportHeight(true);
from the javadoc:
Sets whether or not this table is always made large enough to fill the height of an enclosing viewport.

JTable and JScrollPane

I have a really huge JTable and I want to add scroll bars to the JFrame so that I can scroll to see the rest of the table instead of resizing the frame.
Problem is, when I add the table and scroll-bars, the width of the columns is shrunk to fit the size of the window. Is there any way to keep the width of the columns a constant and use a scroll-bar to view the rest of the table?
"Is there any way to keep the width of the columns a constant and use a scroll-bar to view the rest of the table?"
In this case a better solution than set a fixed (constant) width for columns is set JTable's autoResizeMode property to AUTO_RESIZE_OFF in order to avoid columns be auto-resized to fit the table's visible area:
JTable table = new JTable(15, 25);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scrollPane = new JScrollPane(table);
This snippet will produce a 15 rows and 25 columns table that will look like this (note the horizontal scroll bar):
However this approach won't be helpful if the sum of the columns width in your table is less than the table's width. In that case it should be better to let the default AUTO_RESIZE_SUBSEQUENT_COLUMNS policy.

Need help understanding some row constraints behaviour

In a Java project I am working on, my constraints were originally set to:
JPanel panel = new JPanel();
String layoutConstraints = "fill";
String columnConstraints = "[right]rel[grow]";
String rowConstraints = "[]10[]";
panel.setLayout(new MigLayout(layoutConstraints, columnConstraints, rowConstraints));
From what I understand of the documentation the last gap and constraint will be repeated for every row in excess of the specification. (cf. "If there are fewer rows in the format string than there are in the grid cells in that dimension the last gap and row constraint will be used for the extra rows.")
So for all I understand, this should mean the row constraints would in fact be []10[]10[]10[] and so on.
My problem comes when I have a JTextArea in a JScrollPane with rows set to 3:
JLabel label = new JLabel("Text area");
JTextArea text = new JTextArea("Test", 3, 40);
JScrollPane scroll = new JScrollPane(text);
panel.add(label);
panel.add(scroll, "grow,wrap");
When used with a row constraint of []10[] is only 1 row high, yet when I change
String rowConstraints = "[]10[]";
to
String rowConstraints = "[]";
it suddenly uses the full 3 rows height that I specified for the JTextArea.
Have I misunderstood how the row constraints work or is there something else at work here that I totally failed to see/understand?
In both cases you are using only one layout row (i.e. row managed by MigLayout) to which you put JLabel (first column) and JScrollPanel (containing JTextArea, second column).
You can easily visualize your MigLayout rows and columns structure by simply adding "debug" to layout constraints:
String layoutConstraints = "fill,debug";
And launching application. I really recommend using this setting while working on your layouts.
Rows property that you set for JTextArea have nothing to do with layout rows. This property only tells JTextArea component what size it will try to have, as defined in javadocs:
java.awt.TextArea has two properties rows and columns that are used to determine the preferred size. JTextArea uses these properties to indicate the preferred size of the viewport when placed inside a JScrollPane (...)
But JTextArea rows doesn't correspond by any means to rows in container's (JPanel in your case) layout.
Please, learn more carefully (and experiment!) what exactly JTextArea row property means.

Categories

Resources