2 Replies Latest reply on Oct 9, 2004 3:32 AM by camickr

    scroll Jtable

      I have a JTable inside a JScrollPane and want to set the focus to edit the last line of the table
      by API.
      OK, I use getEditableComponent() to set the focus but I can't get the scroiling to maximum value.
      It is however possible to do it setting the value of the verticalsrollbar to the maximum value but the JTable runs into some ugly displaying problems. There must be a way to get the viewport scrolled so that the editing component is visible

      any hints?
        • 1. Re: scroll Jtable
          Perhaps you've tried JViewport's scrollRectToVisible -- that doesn't work well for me.... but it's setViewPosition
          import java.awt.*;
          import javax.swing.*;
          import javax.swing.event.*;
          public class TableExample {
              public static void main(String[] args) {
                  final int ROWS = 100;
                  final JTable table = new JTable(ROWS, 6);
                  for(int i=0; i<ROWS; ++i)
                      table.setValueAt(new Integer(i), i, 0);
                  final JScrollPane sp = new JScrollPane(table);
                  JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, ROWS-1, 1));
                  spinner.addChangeListener(new ChangeListener(){
                      public void stateChanged(ChangeEvent e) {
                          JSpinner spin = (JSpinner) e.getSource();
                          int row = ((Number)spin.getValue()).intValue();
                          Rectangle bounds = table.getCellRect(row,0,true);
                  JPanel south = new JPanel();
                  south.add(new JLabel("Make sure this row is visible: "));
                  JFrame frame = new JFrame("TableExample");
                  Container cp = frame.getContentPane();
                  cp.add(sp, BorderLayout.CENTER);
                  cp.add(south, BorderLayout.SOUTH);
          • 2. Re: scroll Jtable
            There must be a way to get the viewport scrolled so that the editing component is visible