一个JTable 放在JScrollPane中,如果这个JTable有50行,而JScrollPane中一页只能显示20行,我想让JTable 从第25行开始显示,怎么做?

解决方案 »

  1.   

    我试了,它得到焦点但是不会自动显示到viewport里面,还是不可见
      

  2.   


    public class Test {

    public static void main(String[] args) throws InterruptedException {
    JFrame frame = new JFrame();
    frame.setSize(400,300);
    frame.setLayout(new BorderLayout());

    String rowdata[][] = new String[50][1];
    for(int i=0;i<50;i++){
    rowdata[i][0]=(i+1)+"";
    }

    JTable table = new JTable(rowdata , new String[]{"col"});
    final JScrollPane pane = new JScrollPane(table);
    frame.add(pane,BorderLayout.CENTER);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);


    JScrollBar bar = pane.getVerticalScrollBar();
    int unit = bar.getMaximum() / table.getRowCount();
    bar.setValue(unit * (25-1));

    }

    }
      

  3.   

    JScrollBar bar = pane.getVerticalScrollBar();
            int unit = bar.getMaximum() / table.getRowCount();
            bar.setValue(unit * (25-1));
    楼上说的没错,以前好象也是这么处理的,拉动滑块可以设置比例值