Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JScrollPane.scrollRectToVisible does not scroll properly with a JTable

843805Sep 22 2005 — edited Nov 9 2005
I had some problems scrolling my JTable which is embedded inside a JScrollPane by using scrollRectToVisible(). So I created a test program to see if it was really a problem with scrollRectToVisible(), and apparently it is (or I'm doing something wrong, which is probably more likely :)

the program is attached at the end of this posting.

Besides scrolling the table by the JScrollPane's scrollbars, the application also allows to scroll via the two buttons "up" and "down". What these buttons do is that they take the view rectangle by JViewport.getViewRect(), move it up or down by 10 pixels, and call JViewport.scrollRectToView().

To my understanding, this should scroll the table up or down by 10 pixels, but from what I see it doesn't - it scrolls down by an arbitrary amount, no matter whether I press up or down. The program also prints the view rectangle it gets ("old rect") and the one it tries to make visible ("new rect").

I'm using JDK 1.5_05.

any clues?

here's the program:
/*
 * NewJFrame.java
 *
 * Created on 22. September 2005, 16:27
 */

package testviewport;

import java.awt.Rectangle;
import javax.swing.JViewport;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  upachler
 */
public class NewJFrame extends javax.swing.JFrame {
	
	/** Creates new form NewJFrame */
	public NewJFrame() {
		initComponents();
		
		Object[][] data = {
			{"Mary", "Campione",
			 "Snowboarding", new Integer(5), new Boolean(false)},
			{"Alison", "Huml",
			 "Rowing", new Integer(3), new Boolean(true)},
			{"Kathy", "Walrath",
			 "Knitting", new Integer(2), new Boolean(false)},
			{"Sharon", "Zakhour",
			 "Speed reading", new Integer(20), new Boolean(true)},
			{"Philip", "Milne",
			 "Pool", new Integer(10), new Boolean(false)},
			{"Mary", "Campione",
			 "Snowboarding", new Integer(5), new Boolean(false)},
			{"Alison", "Huml",
			 "Rowing", new Integer(3), new Boolean(true)},
			{"Kathy", "Walrath",
			 "Knitting", new Integer(2), new Boolean(false)},
			{"Sharon", "Zakhour",
			 "Speed reading", new Integer(20), new Boolean(true)},
			{"Philip", "Milne",
			 "Pool", new Integer(10), new Boolean(false)},
			{"Mary", "Campione",
			 "Snowboarding", new Integer(5), new Boolean(false)},
			{"Alison", "Huml",
			 "Rowing", new Integer(3), new Boolean(true)},
			{"Kathy", "Walrath",
			 "Knitting", new Integer(2), new Boolean(false)},
			{"Sharon", "Zakhour",
			 "Speed reading", new Integer(20), new Boolean(true)},
			{"Philip", "Milne",
			 "Pool", new Integer(10), new Boolean(false)},
			{"Mary", "Campione",
			 "Snowboarding", new Integer(5), new Boolean(false)},
			{"Alison", "Huml",
			 "Rowing", new Integer(3), new Boolean(true)},
			{"Kathy", "Walrath",
			 "Knitting", new Integer(2), new Boolean(false)},
			{"Sharon", "Zakhour",
			 "Speed reading", new Integer(20), new Boolean(true)},
			{"Philip", "Milne",
			 "Pool", new Integer(10), new Boolean(false)},
			{"Mary", "Campione",
			 "Snowboarding", new Integer(5), new Boolean(false)},
			{"Alison", "Huml",
			 "Rowing", new Integer(3), new Boolean(true)},
			{"Kathy", "Walrath",
			 "Knitting", new Integer(2), new Boolean(false)},
			{"Sharon", "Zakhour",
			 "Speed reading", new Integer(20), new Boolean(true)},
			{"Philip", "Milne",
			 "Pool", new Integer(10), new Boolean(false)},
			{"Mary", "Campione",
			 "Snowboarding", new Integer(5), new Boolean(false)},
			{"Alison", "Huml",
			 "Rowing", new Integer(3), new Boolean(true)},
			{"Kathy", "Walrath",
			 "Knitting", new Integer(2), new Boolean(false)},
			{"Sharon", "Zakhour",
			 "Speed reading", new Integer(20), new Boolean(true)},
			{"Philip", "Milne",
			 "Pool", new Integer(10), new Boolean(false)},
			{"Mary", "Campione",
			 "Snowboarding", new Integer(5), new Boolean(false)},
			{"Alison", "Huml",
			 "Rowing", new Integer(3), new Boolean(true)},
			{"Kathy", "Walrath",
			 "Knitting", new Integer(2), new Boolean(false)},
			{"Sharon", "Zakhour",
			 "Speed reading", new Integer(20), new Boolean(true)},
			{"Philip", "Milne",
			 "Pool", new Integer(10), new Boolean(false)},
			{"Mary", "Campione",
			 "Snowboarding", new Integer(5), new Boolean(false)},
			{"Alison", "Huml",
			 "Rowing", new Integer(3), new Boolean(true)},
			{"Kathy", "Walrath",
			 "Knitting", new Integer(2), new Boolean(false)},
			{"Sharon", "Zakhour",
			 "Speed reading", new Integer(20), new Boolean(true)},
			{"Philip", "Milne",
			 "Pool", new Integer(10), new Boolean(false)},
		};
		
		String[] rowNames = {
			"first name", "last name", "hobby", "kids", "married"
		};
		DefaultTableModel model = new DefaultTableModel(data, rowNames);
		table.setModel(model);
	}
	
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {
        jButton2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        upButton = new javax.swing.JButton();
        downButton = new javax.swing.JButton();
        scrollPane = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();

        jButton2.setText("jButton2");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jPanel1.setLayout(new java.awt.GridLayout(0, 1));

        upButton.setText("up");
        upButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                upButtonActionPerformed(evt);
            }
        });

        jPanel1.add(upButton);

        downButton.setText("down");
        downButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                downButtonActionPerformed(evt);
            }
        });

        jPanel1.add(downButton);

        getContentPane().add(jPanel1, java.awt.BorderLayout.EAST);

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        scrollPane.setViewportView(table);

        getContentPane().add(scrollPane, java.awt.BorderLayout.CENTER);

        pack();
    }
    // </editor-fold>

	private void downButtonActionPerformed(java.awt.event.ActionEvent evt) {
		JViewport viewport = scrollPane.getViewport();
		Rectangle vrect = viewport.getViewRect();
		Rectangle r = new Rectangle(vrect.x, vrect.y+10, vrect.width, vrect.height);
System.out.println("down pressed:");
System.out.println("old rect: " + vrect);
System.out.println("new rect: " + r);
System.out.println();
		viewport.scrollRectToVisible(r);;
	}

	private void upButtonActionPerformed(java.awt.event.ActionEvent evt) {
		JViewport viewport = scrollPane.getViewport();
		Rectangle vrect = viewport.getViewRect();
		Rectangle r = new Rectangle(vrect.x, vrect.y-10, vrect.width, vrect.height);
		viewport.scrollRectToVisible(r);
System.out.println("up pressed:");
System.out.println("old rect: " + vrect);
System.out.println("new rect: " + r);
System.out.println();		
	}
	
	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new NewJFrame().setVisible(true);
			}
		});
	}
	
    // Variables declaration - do not modify
    private javax.swing.JButton downButton;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JTable table;
    private javax.swing.JButton upButton;
    // End of variables declaration
	
}
Cheers,

Uwe
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 7 2005
Added on Sep 22 2005
5 comments
194 views