How to keep vertical scrollbar always at the end of the jtable - java

What i want is i want to have a scrollbar always at the last row.sometimes i have morethan 60 rows.that time i need to have a scrollbar at the end of the 60th row.
I manually added rows and columns in jtable in my program.
Any way is there to do this?
Please give some simple example.
i am beginner to jtable.

on your button click listener add this code:
JScrollBar vertical = scrollPane.getVerticalScrollBar();
vertical.setValue( vertical.getMaximum() );
and you will have your scroll panel always to the bottom

Whenever you update your table, you could do:
JTable tbl; // your table
tbl.scrollRectToVisible(new Rectangle(0, tbl.getHeight() - 1, tbl.getWidth(), 1));

Related

Is there an function like "changeSelection()" to select row but on top of the table not on bottom?

I have a jTable with the tree which automatically selects the corresponding row from previous action. But changeSelection scroll and select row as last visible on bottom. For me is uncomfortable because is tree and is much better to be scrolled and selected row as first visible on top of the table.
I am try table.scrollRectToVisible(table.getCellRect(row,column, true)); same result and tried custom set scrollBar value but I don't know right value because panel can change size.
I expect that automatic scroll to row and that row will be marked on top of the table. Now automatic scroll to row and is marked on bottom of the table.
The scrollRectToVisible(...) method only scrolls the JViewport of the JScrollPane to make sure the Rectangle is visible. So when you scroll down. It will display at the bottom. If you are scrolling up it will display at the top.
An easier way to control the scrolling is to set the JViewport position yourself:
JViewport viewport = (JViewport)scrollPane.getViewport();
Rectangle rectangle = table.getCellRect(row, column, true);
Point point = new Point(rectangle.x, rectangle.y);
viewport.setViewPosition( point );

Disable horizontal autoscroll in JTable

I have a JTable that is filled dynamically.
It has only two columns, the first is a boolean value and the second is a string.
The problem is that when I select a row in the string column, the table autoscrolls to the right hiding the first column.
How do I change this behaviour?
If I disable autoscroll, the user is not able to scroll to the bottom through the arrow keys.
Any ideas?
Thanks in advance.
Call:
jscrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
which will make sure no horizontal JScrollBar is created for the JScrollPane.
Perhaps also need to ensure the JScrollPane fits the JTable horizontally via:
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane jscrollpane = new JScrollPane(table);

JScrollPane Movement Restriction Query?

I have JTable of order nxn with the JScrollPane size being
JScrollPane jsc = new JScrollPane(table);
jsc.setPreferredSize(new Dimension(500,700));
The Scenario is this:: whenever I edit the last cell (it contains a JTextField) of the JTable by dragging down the ScrollBar and come out of Focus, the ScrollBar slides back the original position, i.e the beginning position. I would like the ScrollBar be in the place where it was set by the user before and after editing.... how can this be achieved?
Thank You In Advance....
you can able to determine scrollToVisible(JTable table, int rowIndex, int vColIndex), example here
Because JTable is a Scrolling-Savvy Client, it implements the Scrollable interface. You may get better results using the method setPreferredScrollableViewportSize() instead of calling setPreferredSize() directly on the scroll pane.

Vertical scrollbar covering the last column data of jtable(last column data is right aligned)

I am using a jtable which have 8 rows by default. When I add a new row using a button click a vertical scrollbar comes into picture and covers up my data of last column in jtable which is right aligned. How can I overcome this?
Specify setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS) on the JScrollPane. This will leave room for the scroll bar when (if) it becomes necessary.
Sounds like you need to give your table a little bit more space to account for the addition of a scroll bar. You will need to possibly mess with the preferredSize.

Is it possible to have a JPanel over JTable

I have a JTable whose associated TableModel could be initially empty. Therefore, it currently shows a JTable with its columns and no rows.
In order to fill this JTable, I want the user to drag and drop elements from another component. The problem is that I would like to hint the user that he/she should drag elements to this table, with some message like "Drag xxx here to add a row".
I thought that I could achieve this by putting a panel over the JTable , but I don't think it is possible with any java layout.
Does anyone know how to do this? Or should I stick to a CardLayout to switch to/from the hint and the JTable?
Thanks a lot
Take a look at OverlayLayout, I think it might do what you want.
A couple of suggestions:
Add a tooltip to the JTable using setToolTipText(String). You'll need to add it to the surrounding JScrollPane in order for the tooltip to be displayed when the user hovers on the empty viewport.
Add a titled Border; e.g. scrollPane.setBorder(BorderFactory.createTitledBorder("Drag items here"));
I would do it like this...Create a JPanel to contain the JTable on top and a JLabel on the bottom. Something like this...
JPanel container = new JPanel( new BorderLayout() );
container.add( table, BorderLayout.CENTER );
JLabel label = new JLabel( "Drag XXX here to add a row" );
container.add( table, BorderLayout.SOUTH );
Then make both the label and table drop targets. You might need to play with the label to make it look and behave right. Set opaque to true possibly? Set its background to white to match the table? Also maybe play with the borders of all three to make them look integrated with each other. You could remove the label after the first row is added if that is the behavior you want.

Categories