SWT TableViewer - Adjust row height when horizontal scrollbar appears - java

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?

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.

Why does the horizontal scrollbar never show up on JTable?

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());
}

DataGrid vertical scroll bar visible only when DataGrid is scrolled all the way to the right

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);

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.

How to make a JTable's width >= the enclosing scroll pane's width?

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

Categories

Resources