Since a picture is worth a thousand words:
I have JTable inside a JScrollPane, and currently the table has 10 columns. Each column is given a minimum width (given by setMinWidth(...)).
First picture: When the JFrame is stretched beyond the sum of all of the minimum widths of all columns then the columns are stretched to fill the window: this is the desired effect (do not want empty space at far right of frame).
Second picture: However when the JFrame is set to a size smaller than this combined minimum size, the horizontal bar does not show up properly and it becomes impossible to view the far right columns. Currently I have JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS as the horizontal bar option hince why for both pictures the horizontal bar container (appropriate name?) is there, but the actual bar itself never shows up. If I have JScrollPane.HORIZONTAL_SCROLLBAR_OFF, the horizontal bar container never shows.
Vertical works fine btw.
I have tried many things, including disabling auto resizing and trying to set the column widths myself when the JScrollPane width exceeds the min width sum of the columns. It doesn't seem right that I would have to do that manually myself. I can provide my code if necessary, but I don't think that it is necessary because it's just a JScrollPane hosting a JTable.
The solution was to overwrite the getScrollableTracksViewportWidth() method:
#Override
public boolean getScrollableTracksViewportWidth() {
return (getPreferredSize().width < getParent().getWidth());
}
Related
When I make a JTable; I have the columns, and a single row, and then beneath that I have space for ~20 more rows, more if I increase the window size. I cannot have less as if I reduce the window size I just get a vertical scroll bar.
What I would like to do is make it so that the JTable size only goes to the last row.
I thought something like this:
newTable.setMaximumSize(new Dimension(Integer.MAX_VALUE, (newTable.getRowCount() + 1) * newTable.getRowHeight()));
But this does absolutely nothing. Using setPreferred size instead makes a white background about one row bigger, but the actual table is still the same size.
http://i.imgur.com/ClpD1Y3.png
I also tried the same things on the scrollpane that the JTable is in, setting the max / min sizes did nothing, setting the preferred size allowed me to make the table smaller by resizing the window, so I'm guessing that I may be able to do what I want using this and a layout perhaps? But I get class cast exceptions when trying to give a layout to the scrollpane. So I'm unsure what to do.
Set the preferredSize of the container that the JTable is in, ie. the scrollpane
I have a FlexTable that houses a ScrollPane, which contains a DataGrid.
THe ScrollPane is set to a 100% width, 450px height. The DataGrid is set to 200% width, and 450px height.
The horizontal scroll bar allows me to scroll the table horizontally as expected. However, the vertical scroll bar that lets me scroll through different rows only is visible if I'm scrolled all the way to the right. Is there a way to change this behavior so that the vertical scroll bar is always visible on the right hand side, like in C#'s DataGrid, for example?
I'm looking at the API for DataGrid and do not see anything useful. I have alwaysShowScrolLBars set to true.
My issue was that I shouldn't put a DataGrid inside a ScrollPane since GWT's DataGrid has scrolling built in. To get the desired scrolling behavior, i set the size of the data grid, and then teh size of the table (which is larger than the overall datagrid widget).
// full width, 55% height
int dataGridPixelHeight = (int) (Window.getClientHeight() * 0.55);
m_resultsDataGrid.setSize("100%", dataGridPixelHeight + "px");
// sets the actual inner grid to be wide, allowing horizontal scroll
m_resultsDataGrid.setTableWidth(200, Unit.PCT);
I have a TableViewer that can update the height to display x number of rows:
int height = tableViewer.getTable().getItemHeight() * numRowsToDisplay +
hBar.getSize().y + H_SCROLL_PADDING;
gridData.heightHint = height;
I notice a special case when I have one row of data. When the TableViewer is first loaded and displayed on the screen there is no horizontal scrollbar showing and I can see the row of data perfectly.
I then add a large value to a column which causes the column width to grow and the horizontal scrollbar appears. This is expected behavior. However, the horizontal scrollbar that appears covers about half of the row instead of showing up directly below the row (and adds a vertical scrollbar). Is there a way to "push" the horizontal scrollbar down so that it doesn't cover the row (and the vertical scrollbar doesn't show)? I tried using similar code to what is shown above, but with no success. Do I have to do anything with the parent composite to allow the tableViewer to grow like this?
I have a horizontal scrollbar that controls a large panel (with a very large width, and a very small height, thus an horizontal panel).
I want the start of the scrollbar (when the knob is at max left) NOT to start at the beggining of the panel it is scrolling, but rather in a specific place that I dictate. The same for the end of the scrollbar (when the knob is at max right).
I find that the scrollbar is always bound to the panel it is scrolling, and I can't figure out how to change its behaviour.
EDIT:
As an example, picture a normal web-page: when at the top of the page, the scrollbar knob is also at the top. When at the bottom, the scrollbar knob is at the bottom. I want to define new limits for the content, such that when the scrollbar knob reaches the top or bottom, the page is showing the limit I defined, instead of the real top and bottom.
As shown in How to Use Scroll Panes, you can use the component's scrollRectToVisible() method to scroll to an arbitrary Rectangle. There's an example here.
Addendum: As a Container, a JPanel is fairly fungible even if it has considerable nested content. One reliable way to swap content at a given level is via CardLayout, shown here, here and here.
I solved the problem by using the JScrollbar method setValues(), which allows me to set at the same time the maximum, minimum, value and extent of the scrollbar. By setting the maximum and minimum to the values I want, the scrollbar behaves as I wanted/expected.
The problem was that I was only setting maximum and minimum values (setMaximum, setMinimum), and since there is a strict policy at the model that minimum <= value <= value+extent <= maximum, that estrategy did not work.
Would it be possible to keep the large panel as a backing store and copy the region of interest into a panel which is actually realized in the scrollpane. This way you don't have to fight the behavior of the scrollpane.
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 :-)