Disable horizontal autoscroll in JTable - java

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

Related

JXTable width to adjust according to content

I have a JXTable that is created like this:
JScrollPane scrollPane = new JScrollPane();
contentPanel.add(scrollPane, BorderLayout.CENTER);
tbl = new JXTable();
tbl.setColumnSelectionAllowed(true);
tbl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tbl.setEditable(false);
scrollPane.setViewportView(tbl);
The problem is that when it gets filled, all the columns are squashed to fit into the table. I expected the columns to be adjusted according to the width of the column header or column value (which ever is broader) and then for a horizontal scroll bar to be displayed that will enable me to scroll left or right.
How do I bridge this problem?
You should use the packAll method: this resizes columns to fit the viewport; if horizontal scrolling is enabled, columns will get their preferred width.

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

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

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.

retrieve changed contents of a jtable contained in a jscrollpane

I have a Jtabbedpane that holds a JscrollPane which in turn holds a Jtable.
I would like to access the dynamically changed contents of the JTable via the JScrollPane. I have tried the following but I am stuck up at this point.
JScrollPane c = (JScrollPane)MyTabbedPane.getSelectedComponent()
Now c, the scrollPane has the changed table.
I saw that c.getComponents is returning two viewports and two scrollbars but not the Jtable.
Please tell me how can I access the JTable that the JScrollPane holds.
JViewport viewport = scrollPane.getViewport();
JTable table = (JTable)viewport.getView();
try this,to get table in JScrollPane...
jScrollPane.getViewport().getComponents()
Hope this helps...

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