JScrollPane Movement Restriction Query? - java

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.

Related

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

Extending (sliding down) JTable row

I use jPanel as my cell and the table has just one column. It looks like this ( JTable: Buttons in Custom Panel in Cell ).
By default, the panel associated with the row (celll) contains a date when it was created. i want to implement an ActionListere method that will extend( slide down) the jpanel in which the action occured to see the whole jPanel data.
Hope you get what i mean.
Basically, you want to do the same thing that JList.ensureIndexIsVisible(int) but with a table.
Component has a method called "scrollRectToVisible(Rectangle aRect)" which should just about do what you need.
So, using your JTable, you can get the cellRect(int width, int height, boolean includingSpacing) and pass it off to the "scrollRectToVisible"
You're going to have to do some clever work to find out the row of the action, but I assume that you are using a table cell editor which has already provided you with this information

Resize JTable in JScrollPane by dragging

How can I implement the functionality of being able to resize a JTable by dragging a corner by mouse (to see more rows during a single view) which is embedded in a JScrollPane? Is their any other easy alternative way? Thanks a lot.
Add the JScrollPane to the ComponentResizer class.

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.

Java Swing Scrollpane in NetBeans

I have Java application which adds JTextFields # runtime to JPanel. Basically user clicks a button and new JTextField is added, clicks again added again...
Each new JTextField is directly below the previous one. Obviously I run out of space pretty soon so I'm trying to use JScrollPane and thats where the hell begins, because it just doesnt work no matter what I try.
Right click on JPanel and Enclose in Scroll Pane. Didnt work.
After reading some examples I realized I must have JPanel as an argument for JScrollPane constructor. Which I did via right clicking on ScrollPane and CustomizeCode. Because apparently auto-generated code is protected in NetBeans and I cannot just change all those declarations, etc. manually. Still doesnt work.
I did try to set PreferedSize to null for JPanel and/or JScrollPane, didnt help.
JScrollPane is a child of lets call it TabJPanel (which in turn is a tab of TabbedPane). I tried to mess with their relationships, basically trying every possible way of parentship between JFrame, JPanel(holding textfields), TabJPanel and JScrollPane, but nothing worked.
I also made VerticalScrollBar "always visible" just in a case. So I see the scrollbar, it's just that populating that JPanel with JTextFields does not affect it.
When there are too many JTextFields I they go "below" the bottom border of JPanel and I cannot see them anymore.
Code for adding new JTextFields is like this, in a case it's relevant.
JTextField newField = new JTextField( columns );
Rectangle coordinates = previousTextField.getBounds();
newField.setBounds(coordinates.x , coordinates.y + 50, coordinates.width, coordinates.height);
JPanel.add(newField);
JPanel.revalidate();
JPanel.repaint();
Sorry for a long post I'm just trying to provide as much info as possible, because being newbie I dont know whats exactly relevant and whats not. Thanks in advance :)
As there is another answer now, I'm adding my suggestion too.
This sounds exactly like a problem to use a JTable with a single column. JList is not yet editable (and might never be).
JTable would handle the layout problems for you, and you can easily access the values via the table.
Use your own TableModel (a simple Vector should be sufficient in your case), and add values to it.
An option you have is to utilize a LayoutManager, instead of setting the bounds directly on the components. To test this, a simple single column GridLayout with the alignment set to vertical should prove the concept.
panel.setLayout(new GridLayout(0,1));
zero in the rows param allows for rows to be added to the layout as needed.
I do this way to add a scrollpane, create a panel and fill it with few components, then create a scrollpane in the component you want to add it, cut and paste the panel in which all your details will fall in and resize the scrollpane.Because the components take a larger space than the one visible right click on the scrollpane and select design this container, there you can increase the size of the scrollpane and add as many components as you have.

Categories