Results 1 to 6 of 6
  1. #1
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default problem positioning a JPanel in a JScrollPane

    Hello,

    I'm wondering if anyone's ever encountered the following problem:

    I have a JFrame that contains a JScrollPane that contains a JPanel in its viewport. The JScrollPane doesn't seem to want me to set the location of the JPanel to anything beyond -603. That is, for example, if I do this:

    MyPanel.setLocation(-800,-800);

    It will reset its location to (-603,-603).

    This happens either upon the call to MyFrame.pack() or upon the call to MyFrame.setVisible(true).

    The dimensions of all objects are as follows:

    JFrame: 800x600 (but the call to pack() changes this to 500x500)
    JScrollPane: 400x400 (preferred size - conforms to this when displayed)
    JPanel: 1000x1000 (preferred size - conforms to this when displayed)

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

    Default

    I think that your best bet for this, as for most layout problems, is to create and post an SSCCE. Please see the link in my signature if you're not familiar with this process.

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

    Default

    OK, I've had time on my hands and created an SSCCE which I'll post here:

    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    
    public class ScrollPosition {
       private static final int MAX = 1000;
       private static final Dimension PANEL_SIZE = new Dimension(MAX, MAX);
       private static final Dimension VIEWPORT_SIZE = new Dimension(400, 400);
       private static final Dimension CONTENT_SIZE = new Dimension(800, 600);
    
       private static void createAndShowUI() {
          // create an image to display in the inner component
          BufferedImage bImage = new BufferedImage(PANEL_SIZE.width, 
                   PANEL_SIZE.height, BufferedImage.TYPE_INT_RGB);
          Graphics2D biG = bImage.createGraphics();
          biG.setStroke(new BasicStroke(2));
          for (int i = 0; i < MAX / 10; i += 2) {
             Color c = (i % 10 == 0) ? Color.red : Color.blue;
             biG.setColor(c);
             biG.drawLine(0, i * 10, MAX, i * 10);
             biG.drawLine(i * 10, 0, i * 10, MAX);
          }
          biG.dispose();
          JLabel label = new JLabel(new ImageIcon(bImage));
          
          JPanel myPanel = new JPanel(new BorderLayout());
          myPanel.add(label);
          
          JScrollPane scrollpane = new JScrollPane();
          JViewport viewport = scrollpane.getViewport();
          viewport.add(myPanel);
          viewport.setPreferredSize(VIEWPORT_SIZE);
          viewport.setViewPosition(new Point(603, 603));
          
          JFrame frame = new JFrame("Scroll Position");
          Container contentPane = frame.getContentPane();
          contentPane.setPreferredSize(CONTENT_SIZE);
          contentPane.setLayout(new FlowLayout());
          frame.getContentPane().add(scrollpane);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }

    The key for me is to not set the position of the JPanel held by the JScrollPane via setLocation since what you really want to do is change the view that the JScrollPane has on the JPanel, and to do this, you set the JScrollPane's JViewport's vew position:

    Java Code:
    scrollpane.getViewport().setViewPosition(new Point(603, 603));
    Last edited by Fubarable; 07-30-2010 at 08:53 PM.

  4. #4
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default

    Thanks Fubarable, but it seems setViewPosition() has the same limitations. I added one more line of code to the one you posted (it's at the end of createAndShowUI()):

    Java Code:
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    
    public class ScrollPosition {
       private static final int MAX = 1000;
       private static final Dimension PANEL_SIZE = new Dimension(MAX, MAX);
       private static final Dimension VIEWPORT_SIZE = new Dimension(400, 400);
       private static final Dimension CONTENT_SIZE = new Dimension(800, 600);
    
       private static void createAndShowUI() {
          // create an image to display in the inner component
          BufferedImage bImage = new BufferedImage(PANEL_SIZE.width, 
                   PANEL_SIZE.height, BufferedImage.TYPE_INT_RGB);
          Graphics2D biG = bImage.createGraphics();
          biG.setStroke(new BasicStroke(2));
          for (int i = 0; i < MAX / 10; i += 2) {
             Color c = (i % 10 == 0) ? Color.red : Color.blue;
             biG.setColor(c);
             biG.drawLine(0, i * 10, MAX, i * 10);
             biG.drawLine(i * 10, 0, i * 10, MAX);
          }
          biG.dispose();
          JLabel label = new JLabel(new ImageIcon(bImage));
          
          JPanel myPanel = new JPanel(new BorderLayout());
          myPanel.add(label);
          
          JScrollPane scrollpane = new JScrollPane();
          JViewport viewport = scrollpane.getViewport();
          viewport.add(myPanel);
          viewport.setPreferredSize(VIEWPORT_SIZE);
          viewport.setViewPosition(new Point(603, 603));
          
          JFrame frame = new JFrame("Scroll Position");
          Container contentPane = frame.getContentPane();
          contentPane.setPreferredSize(CONTENT_SIZE);
          contentPane.setLayout(new FlowLayout());
          frame.getContentPane().add(scrollpane);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
    
          // Try this:
          viewport.setViewPosition(new Point(-50, -50));
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    This should demonstrate that the ScrollPane has some kind of built-in constraint on where its contents can be positioned. It seems not to want anything outside its content to be visible through the viewport. Thus, even though you can tell it to position the view at (-50, -50), it will set it to (0, 0) instead.

    In any case, I ultimately won't want anything outside the panel to become visible, so even if there's no way around this I don't need a solution. I just wanted to confirm my guess: that this is a built-in feature of ScrollPanes. Otherwise, I have a bug in my program and shouldn't ignore it.

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

    Default

    Spam reported.

    db

  6. #6
    ornaricildern is offline Member
    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Default The swordfish also is attainable on a sandwich, as really is a grilled ahi tuna.

    3333
    <a href=http://steveshepardart.com><b>michael kors outlet</b></a> The vibrant important ruby gemstone is Fuscia pink in colour, faceted oval formed and surrounded by diamonds.
    <a href=http://www.scottishbordersnewways.co.uk/cheapuggboots.html><b>cheap ugg boots</b></a> Diesing distinguishes concerning five many different types of valid reason that run in societies: technological, economic, authorized, political and social.
    <a href=http://www.scottishbordersnewways.co.uk/cheapuggboots.html><b>cheap ugg boots</b></a> Planning to transform or beautify your property or company? You'll want to pay a visit to this wholesale distributor of builders' surplus and manufacturers' discontinued ceramic, glass and porcelain tiles, and help you save fifty for each cent off retail fees.
    <a href=http://uggbootssale.scottishbordersnewways.co.uk><b>ugg boots sale</b></a> Soho will be the superb environment for these suppliers to put into play a young line, or take a additional fashionable and edgy procedure.
    <a href=http://www.gamespeopleplaypv.com><b>cheap jerseys</b></a> She may perhaps unquestionably take advantage of putting on a Gothic Ballerina outfit or perhaps Pirate Ballerina costume to show off her feisty individuality.
    <a href=http://www.timmymovie.net/page/2/><b>louis vuitton outlet</b></a> I explained to them that two months ago they reported they failed to want the tarp, just a photo so I acquired rid of it.

Similar Threads

  1. Replies: 1
    Last Post: 05-09-2010, 06:34 PM
  2. Replies: 3
    Last Post: 02-06-2010, 03:28 PM
  3. JScrollPane problem
    By KArelVH in forum AWT / Swing
    Replies: 6
    Last Post: 04-27-2009, 09:40 PM
  4. problem with Jscrollpane
    By ravrajesh.ap in forum AWT / Swing
    Replies: 5
    Last Post: 01-03-2009, 10:38 PM
  5. jscrollpane problem
    By monkey04 in forum AWT / Swing
    Replies: 2
    Last Post: 01-19-2008, 05:23 AM

Posting Permissions

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