JTable Autoscroll
I am adding a label to a JTable and adding mouseMotionListener event to t开发者_StackOverflowhe label.The label moves across the table on mouseDragged event.The table contains more than 50 columns and only 15 columns will be visible at any point of time.Now if I want to move the label to the other side of the same row I can't move the label without moving the scroll bar manually.Is it possible to move the scrollbar automatically simply on dragging the label?
JTable table = new JTable(model);
JLabel label = new JLabel();
label.setBounds(100,50,80,20);
table.add(label);
label.addMouseMotionLister(new MouseMotionListener()
{
public void mouseDragged(MouseEvent arg0)
{
label.setBounds(label.getX()+arg0.getX(),Y,width,height);
}
Invoke scrollRectToVisible(...) on the table after you move the label. You should be able to use the bounds of the label as the Rectangle for the method.
Edit:
Converting the above suggestion in english to Java code I would try something like:
table.scrollToRect( theBoundsOfTheLabel );
精彩评论