problem with JTable or JScrollpane to which JTable is added
Hi All,
i did search in forum for my problem but could not found for what iam looking.
there fore the problem is.
in my project i have a JTable which is added to a JScrollpane.
the vertical scrollbar and horizontal scrollbars are set to as needed.
the table gets populated by the data from database.
the table has 2 columns
first column has preffered column width as 30 and other has 80 there by the window shows horizontal scrollbar (i have used JTable.setAutoReSize(JTable.SET_AUTO_RESIZE_OFF)
when i click with mouse on the second column then the table adjusts itself to show this column only.
i mean the scrollbar move to left to show the entire 2 column.making the first column to visible partially.
may i ask anybody to help me in fixing this problem.
i dont want the scrollbar to move to left when clicked on 2 column.
Thanks in advance
Ravi
I think you're talking about autoscrolling.Try calling setAutoscrolls(false) on your table.Hope this helps.
Thank you very much KPit worked.
Hi KP,
its me again !!
did you take DUKE Dollors ?
for the problem what i posted you asked me to setAutoScroll(false).
i did it,
but now iam getting another problem.
if i use arrow keys up,down,right left then vertical and horizontal scrollbars are not responding.
please help me in this regard also
> did you take DUKE Dollors ?
Users don't take them - they give them. But it looks like I got some so thank you!
> if i use arrow keys up,down,right left then vertical
> and horizontal scrollbars are not responding.
The autoscrolls property tells the table to scroll in order to follow the selection. When you press up/down/left/right on the table you're changing the cell selection which is what the scrollpane usually tracks. With autoscrolls set to talse it won't scroll with the selection anymore.
This is basically the same as when the user clicks on a cell - it selects the cell and the scrollpane adjusts. This was the behaviour you wanted to disable in your original post. What is the distinction between selecting a cell using the mouse and selecting a cell using the keyboard?
Do you only want it to autoscroll if the selection is a result of a key being pressed?
>What is the distinction between selecting a cell using the mouse and >selecting a cell using the keyboard?
>Do you only want it to autoscroll if the selection is a result of a key >being pressed?
1 There is no disticntion either select it by mouse or by keyboard.
but
what if, if the user wants to navigate the table using keyboard.
i mean lets say i have a table with more rows and columns so automatically i will get vertical and horizontal scrollbars.
now instead of using mouse lets say user used keyboard down arrow key to scroll. the selection of the row in the table scrolls but the scrollbar does not move at all.
which, dont you think an odd behaviour. usually in any windows application we can scroll up and down using arrow keys.
cant we achieve my first posting and second together to allow user to navigate in table using keyboard and also the scroll should not move to left when selected the second column (as in my first posting)
please,
suggest me
What would happen if there were ten columns and the user moved the selection right to a cell that was not shown on the scrollpane? Should it scroll to show that column?
Should it only scroll left/right when the keyboard is used but never when the mouse is used?
It's achievable but the selection/scrolling rules need to be better defined in order to implement them.
Thank you very much for the immediate response KP
>What would happen if there were ten columns and the user moved the >selection right to a cell that was not shown on the scrollpane? Should >it scroll to show that column?
yes it should show that column.
>Should it only scroll left/right when the keyboard is used but never >when the mouse is used?
No, it should do with mouse as well.
how ever i think since the table will have horizontal scrollbar when used mouse he will drag it to right and will be able to see the last column of the table.
>It's achievable but the selection/scrolling rules need to be better >defined in order to implement them
how to define the rules?
iam in the mid stage of my project !!
is it possible now?
if possible what i have to do so that single solution is applied for the tables in my application.
Thank You
>>Should it only scroll left/right when the keyboard is used but never >when the mouse is used?
> No, it should do with mouse as well.
Do you mean that when the user selects a cell using the mouse it should scroll to show it? Isn't this the behaviour that you originally wanted to avoid?
A change can probably be made by extending JTable and reusing that across all the tables in your application.
You could try something a little bit nasty like this which will disable autoscrolling when a key has been pressed. I've not tried it, though.
public class MyTable extends JTable {
// TODO: constructors
protected void processKeyEvent(KeyEvent e) {
boolean autoscrolls = getAutoscrolls();
try {
setAutoscrolls(false);
super.processKeyEvent(e);
} finally {
setAutoscrolls(autoscrolls);
}
}
}
Hope this helps.
Thank You for the help KP
but it did not work
>Do you mean that when the user selects a cell using the mouse it >should scroll to show it? Isn't this the behaviour that you originally >wanted to avoid?
yes, this is what i wanted to avoid
but in my table the selection happens row wise. i mean when we select using mouse it selects the entire row not just a cell.
and when we press down arrow key the selction goes to next and so on.
before when i did not setAutoScroll(false) up and down arrow keys were working fine
but once we did this it is now not working as expected.
so shall i rollback the setAutoScroll(false) and keep the keep the problem what i had in my first posting?
can i dare to say this as java limitation?
It's certainly not a limitation of Java.
Sorry, my previous example did the opposite of what I intended, disabling scrolling for keys. Try doing it for mouse events instead:
public class Test {
public static void main(String[] args) throws Throwable {
final JTable table = new JTable(10, 10) {
protected void processMouseEvent(MouseEvent e) {
boolean autoscrolls = getAutoscrolls();
try {
setAutoscrolls(false);
super.processMouseEvent(e);
} finally {
setAutoscrolls(autoscrolls);
}
}
};
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(table));
frame.pack();
frame.show();
}
}
If you need more advanced functionality you'll need to override the changeSelection method so that it calculates the area to scroll to differently - currently it just calculates the cell's bounds and scrolls to show that.
Hope this helps.