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.
Related
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.
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.
I want to make the table width at least fitting the enclosing scroll pane's width, and if it's getting larger, the horizontal scroll bar is used.
None of JTable's auto resize modes suits my need: AUTO_RESIZE_OFF does allow the table to be resized larger than the scroll pane's width, but it can't limit the table's minimal width; the other modes don't allow the table width to be larger than the scroll pane's width at all.
I have tried to set the minimal size as below but it didn't work:
JTable table = new JTable(myModel);
JScrollPane scrollPane = new JScrollPane(table);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// After the table is displayed.
table.setMinimalSize(scrollPane.getSize());
Any idea to achieve that?
I have tried to set the minimal size as below but it didn't work:
table.setMinimalSize(scrollPane.getSize());
JTable and JScrollPane can't returns reasonable Min, Max and PreferredSize
you can to fit (on applications startup) JScrollPanes dimension to JTable by using table.setPreferredScrollableViewportSize(table.getPreferredSize()); (carefully with number of row, otherwise you have to calculating a Dimension instead of JTable.getP...) for JFrame.pack(), on runtime must be validated by revalidate() and repaint() for container nested JScrollPane, don't do that
don't reinvent the wheel, to use Fixed Column Table of Table Column Adjuster by #camickr, which one isn't clear from your description, depends of your ...
a JTable's width >= the enclosing scroll pane's width?
couldn't be reversed this idea
I have a JScrollPane with a JTable in it. In the JTable I have initially 3 rows. Later on rows get added.
The default JTable with my 3 rows is ugly because JScrollPane calls getPreferredScrollableViewportSize from the client (JTable) and the return value is always the same. That's why my JTable/JScrollpane with 3 rows has this free space, JScrollpane andJTable do not have the same size.
My solution is to make a subclass JTable, override getPreferredScrollableViewportSize and return in this function getPreferredSize();. Now the whole JScrollPane has exactly the size of the 3 row `JTable!
When a row gets added I have the command ((JComponent) scrollPane.getParent()).revalidate();
The scrollpane grows with the table.
Everything works fine!
But now I want to set a certain layout for the container of the scrollpane (panel):
myjpanel.setLayout (new BoxLayout(contentPane, BoxLayout.Y_AXIS));
When I add this command my whole solution doesn't work anymore.
The scrollpane hasn't the size of the 3 row table, this free space is there again.
Why is this?
Does anybody have a solution?
CONTINUED
Thank you camickr for pointing me in the right direction. My solution is the following:
scrollPane = new JScrollPane (table) {
public Dimension getMaximumSize () {
double height = 0;
double width = super.getMaximumSize().getWidth();
height += columnHeader.getViewSize().getHeight();
height += viewport.getView().getPreferredSize().getHeight();
height += (getViewportBorderBounds().getHeight() * -1);
Dimension returnValue = new Dimension ((int) width, (int) height);
return returnValue;
}
};
It works now at the beginning.
But when a row is added to the jtable and I call revalidate(); on the jpanel (parent container of the scrollpanel) the jscrollpanel shrinks in height to 1 row + header!
Has anyone got an idea what is to do in this situation.
CONTINUED
Now I know the problem. It is the viewportBorder. In order to get the whole, exact height I have to total the height of the view (Jtable), the height of the column header and the height of the viewportBorder. The first time getViewportBorderBounds().getHeight() returns -3, next time - after a new row is inserted in the table model - the function returns 48. I do not understand why this function returns 48 now.
Another point is that camickr says that changing the size of the jscrollpane defeats the purpose of the scrollpane. I do not understand this. How can anyone be satisfied with a default jscrollpane size of 450 x 400 (http://www.javalobby.org/java/forums/t19559.html) even when the jtable has only 3 lines at the beginning. This unfilled extra space does not look very professional in my eyes. Has anyone got a better solution for this.
Thank you very much for your answers
CONTINUED
Now everything works!
At the beginning I simply set the jscrollpane border to 0 ... sp.setBorder(createEmptyBorder());
In this way I avoid having these strange return values of getViewportBorderBounds().getHeight()
Wolfgang
The BoxLayout allows the component to grow up to the maximum size of the component. I guess you need to override the getMaximumSize() of the scrollpane to return the preferred size.
When a row gets added I have the command "((JComponent) scrollPane.getParent()).revalidate();" The scrollpane grows with the jtable.
That kind of defeats the purpose of the scrollpane. The scrollbar is supposed to stay at a fixed size and then scrollbars will appear if necessary.
I had a similar problem - the basic upshot is that I wanted a JTable in a scroll pane that had a minimum and maximum size: when there was no data in the table, it should have a one-row-height pane background displayed (not a fake row, obviously), and when it was over 10 rows to limit the displayed table size to those 10 rows plus scrollbars. Any size between 1 and 10 should display the table at full height. The situation was a parent-child table, and this was the child table, so I had to trigger the child table resize depending on which parent table row was clicked. A fixed pane size looked ugly in that it took up unnecessary space to push other potentially viewable info under the child table out of view with just blank space.
Using the setPreferredScrollableViewportSize after updating the model with the child rows (and setFillsViewportHeight(true) of course) gave me almost everything I needed. Except that the viewport wouldn't redraw until I clicked on it, or took focus from the window then flicked back, it was most annoying.
The solution was super-simple after reading this post: I just had to call revalidate on the scrollpane's parent, as pointed out in the solution. Fixed it perfectly. Mostly I just wanted to share a use case where you want the scrollpane size to change dynamically up to a maximum size. Swing is so much simpler if everything is created at a fixed size forever... unlike pretty much every real-life use case I've ever encountered :-)
I have the following problem: I have a JTable inside a JScrollPane, and the table has 20 rows. However I want the JScrollPane to only display 10 row at a time (thus one has to scroll down to see the rest of the rows). I am using GridBagLayout to position the JScrollPane.
Any suggestion? Thanks.
table.setPreferredScrollableViewportSize(...);
You can get the preferred width by using the getPreferredSize() method of the table.
You will need to caclulate the height to be the height of the table header plus the height of the rows which can be determine by using table.getRowHeight().
If you know the height of the rows then you can put the JScrollPane in panel with an explicit height - getting the explicit height on the panel WRT the layout manager might be the hard part.