Results 1 to 3 of 3

Thread: JScrollPane

  1. #1
    hiddenpremise is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default JScrollPane

    Hi,
    I am having issues with a JScrollPane that I haven't been able to find a solution to online. I am new to Swing programming, but not programming itself.

    I have a subclass of an awt.Panel that draws a jpeg image from my favorite online web comic. I have a JScrollPane with the viewportView set as the panel that I have made. Images load great. They display inside the JScrollPane, the only problem is that it doesn't scroll if the image exceeds its bounds. If I resize the window, the rest of the image becomes visible as the JScrollPane resizes with the window size. I have tried calling repaint on the scroll pane when I update images and I explicitly use the setSize method on my panel to make sure that the scroll pane can find out what size of a view it is dealing with.

    I will happily post my sloppy code if necessary.

    Please help, I'm pulling my hair out like an idiot on this one.

    Thanks,
    Whit

    Thanks @Fubarable :) It works!
    Last edited by hiddenpremise; 12-25-2010 at 11:00 PM. Reason: THANKS

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

    Default

    First of all, don't mix AWT components (e.g., Panel) with Swing components (JScrollPane and everything else). Instead use only Swing components. Next, if I were going to display an image as you are doing, I'd create an ImageIcon with the Image and place the icon in a JLabel and then add the JLabel to the JScrollPane's view port. It's quite simple and should work well. Please let us know if you have any problems with this.

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

    Default

    For example:

    Java Code:
    import java.awt.Dimension;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class ShowComic extends JPanel {
       private static final String COMIC_PATH = "http://imgs.xkcd.com/comics/incident.png";
       private static final Dimension VIEW_SIZE = new Dimension(340, 340);
    
       public ShowComic() {
          URL comicUrl = null;
          try {
             comicUrl = new URL(COMIC_PATH);
             BufferedImage image = ImageIO.read(comicUrl);
             ImageIcon icon = new ImageIcon(image);
             JLabel label = new JLabel(icon);
             
             JScrollPane scrollPane = new JScrollPane(label);
             scrollPane.getViewport().setPreferredSize(VIEW_SIZE);
             add(scrollPane);
          } catch (IOException e) {
             e.printStackTrace();
             System.exit(1);
          }
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("XKCD");
          frame.getContentPane().add(new ShowComic());
          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();
             }
          });
       }
    }

Similar Threads

  1. JScrollPane
    By UJJAL DHAR in forum New To Java
    Replies: 12
    Last Post: 08-17-2010, 07:47 PM
  2. JScrollPane problem
    By KArelVH in forum AWT / Swing
    Replies: 6
    Last Post: 04-27-2009, 10:40 PM
  3. jscrollpane
    By kaemonsaionji in forum New To Java
    Replies: 3
    Last Post: 02-25-2009, 09:39 AM
  4. [SOLVED] JScrollPane - HELP!
    By terox13 in forum AWT / Swing
    Replies: 8
    Last Post: 05-10-2008, 04:58 AM
  5. help with JScrollPane
    By tommy in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:58 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
  •