Results 1 to 8 of 8
  1. #1
    peterhabe is offline Member
    Join Date
    Jul 2010
    Posts
    43
    Rep Power
    0

    Default Changing JScrollPane default viewing position

    How do you set the JScrollPane's default view on an object to the botton left corner, instead of the top left corner?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    One way is to set the JScrollPane JViewport's view position via its setViewPosition method. Another possible solution if it is holding a JTextArea or something similar is to set the text area's caret position via its setCaretPosition method.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    And another way is to get a reference to the JScrollPane's vertical JScrollBar and set its value to (maximum - extent). If you need to doubly ensure that the pane is scrolled to the bottom left, you wold also set the horizontal scroll bar's value to zero.

    Yet another way (and this is what I would recommend) is to invoke scrollRectToVisible on the component held in the scroll pane's viewport with a rectangle placed at the bottom left of the component:
    Java Code:
    component.scrollRectToVisible(new Rectangle(0, component.getHeight(), 0, 0));
    db

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    As always, thanks Darryl!

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Aw, Pete, humble as usual. You already knew those :)

    As in most things Swing, there's more than one way to skin a cat.

    db

  6. #6
    peterhabe is offline Member
    Join Date
    Jul 2010
    Posts
    43
    Rep Power
    0

    Default

    I can't get anything of the thing you guys suggested to work, probably because im a bit of a noob. Can one of you guys quickly help me out. Heres a little compilable program.
    Java Code:
    import java.awt.Dimension;
    import javax.swing.JFrame;
    
    public class Run {
        public static void main(String args[]){
            ProjectileProgramFixedSize runProgram = new ProjectileProgramFixedSize();
            runProgram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            //setting frame
            runProgram.setSize(800,600);
            runProgram.setMinimumSize(new Dimension(800,600));
            runProgram.setVisible(true);
        }
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class ProjectileProgramFixedSize extends JFrame{
        //class objects
        ProjectileAnimationJPanel projectileAnimationJPanel = new ProjectileAnimationJPanel();
        //variables
        JScrollPane projectileAnimationJScrollPane = new JScrollPane(projectileAnimationJPanel);
        JPanel scrollPanesJPanel;
        
        //constructor
        public ProjectileProgramFixedSize(){
            //housekeeping
            super("Projectile Program");
            
            this.add(projectileAnimationJScrollPane);
            moveToBottomLeft();
        }
        public void moveToBottomLeft() {
            JViewport viewport = projectileAnimationJScrollPane.getViewport();
            Component view = viewport.getView();
            viewport.setViewPosition(new Point(0, view.getHeight()));
        }
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class ProjectileAnimationJPanel extends JPanel{
        //variables
        float meter, xPositionMeter, yPositionMeter, zoom, previousElevation,yPosition;
        int elevationTimesMeter, previousElevationTimesMeter, counter, loopCounter, width, height;
        //variables for drawing image
        int groundHeight, sideOfElevation, eightthOfHeight, ballDiameter, ballRadius;
        //class object
        //constructor
        public ProjectileAnimationJPanel(){
            this.setPreferredSize(new Dimension(800,600));
        }
        //paint method
        public void paintComponent(Graphics g){
            Graphics2D g2 = (Graphics2D)g;
            super.paintComponent(g2);
            this.setBackground(Color.BLUE);
            
            meter = 3;
            zoom = 1;
            
            //setting for drawing image    
            width = Math.round(getWidth()*zoom);
            ballDiameter = 30;
            eightthOfHeight = 100;
            elevationTimesMeter = Math.round(50*meter);
            groundHeight = getHeight()-50;
            sideOfElevation = 50;
            
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            
            g2.setColor(Color.GREEN);
            g2.fillRect(0, Math.round(groundHeight-elevationTimesMeter), sideOfElevation, elevationTimesMeter);
            g2.setColor(Color.GREEN);
            g2.fillRect(0, groundHeight, Math.round(width), eightthOfHeight);
            g2.setColor(Color.BLACK);
            g2.drawLine(sideOfElevation, groundHeight, width, groundHeight);
            g2.drawLine(0, groundHeight-elevationTimesMeter, sideOfElevation, groundHeight-elevationTimesMeter);
            g2.drawLine(sideOfElevation, groundHeight-elevationTimesMeter, sideOfElevation, groundHeight);    
        }
    }
    Last edited by peterhabe; 10-07-2010 at 09:44 PM.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Give the class that has the JScrollPane, here it's ProjectileProgramFixedSize, a public method, say moveToBottomLeft(), and in this method do one of our suggestions above. You can easily get the JScrollPane's viewport by calling getViewport on the JScrollPane and you can easily get the Component held within the viewport by calling getView on the viewport. With this information you can easily use my suggestion and call getViewPosition or use Darryl's recommendation and call scrollRectToVisible on the component held in the viewport.

    Then you can call this method on the ProjectileProgramFixedSize object after it has been rendered (after pack or setVisible(true)).

    e.g.,
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class ScrollToView {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createGui();
                }
            });
        }
    
        private static void createGui() {
            HoldsScrollPanel holdsScrollPanel = new HoldsScrollPanel();
    
            JFrame frame = new JFrame("ScrollToView");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(holdsScrollPanel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
    
            holdsScrollPanel.moveToBottomLeft();
        }
    }
    
    @SuppressWarnings("serial")
    class HoldsScrollPanel extends JPanel {
        private static final Dimension COLOR_PANEL_SIZE = new Dimension(700, 700);
        private static final Dimension SCROLL_PANEL_SIZE = new Dimension(500, 500);;
        private ColorPanel colorPanel = new ColorPanel();
        private JScrollPane scrollpane = new JScrollPane(colorPanel);
    
        public HoldsScrollPanel() {
            colorPanel.setPreferredSize(COLOR_PANEL_SIZE);
            scrollpane.setPreferredSize(SCROLL_PANEL_SIZE);
            add(scrollpane);
        }
    
        public void moveToBottomLeft() {
            JViewport viewport = scrollpane.getViewport();
            Component view = viewport.getView();
            viewport.setViewPosition(new Point(0, view.getHeight()));
        }
    }
    
    @SuppressWarnings("serial")
    class ColorPanel extends JPanel {
        private static final Color[] COLORS = { Color.blue, Color.red,
                Color.magenta, Color.green };
        private static final int BAND = 40;
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            int w = getWidth();
            int h = getHeight();
    
            Paint paint = new GradientPaint(0, 0, COLORS[0], BAND, BAND, COLORS[1],
                    true);
            g2.setPaint(paint);
            g2.fillRect(0, 0, w, h);
    
            paint = new GradientPaint(w, 0, COLORS[2], w - BAND, BAND, COLORS[3],
                    true);
            g2.setPaint(paint);
            g2.fillOval(BAND, BAND, w - 2 * BAND, h - 2 * BAND);
    
        }
    }
    Luck!
    Last edited by Fubarable; 09-29-2010 at 12:54 AM.

  8. #8
    peterhabe is offline Member
    Join Date
    Jul 2010
    Posts
    43
    Rep Power
    0

    Default

    Your program works fine, but i can't get it to work for mine. I've updated my previous post with the little program i can't get it to work for.

Similar Threads

  1. Changing position of JMenuBar
    By LianaN in forum New To Java
    Replies: 8
    Last Post: 09-17-2010, 01:41 PM
  2. Replies: 3
    Last Post: 02-06-2010, 04:28 PM
  3. Changing JTableModel does not change JScrollpane size
    By stelzergil in forum New To Java
    Replies: 3
    Last Post: 10-19-2009, 03:14 AM
  4. Changing the default fonts via UIManager
    By fletcher in forum AWT / Swing
    Replies: 4
    Last Post: 09-26-2009, 07:48 PM
  5. Replies: 2
    Last Post: 01-29-2009, 08:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •