Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8472353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:02:47+00:00 2026-06-10T17:02:47+00:00

I’m trying to get scrollbars to work. The code displays the images I choose.

  • 0

I’m trying to get scrollbars to work.

The code displays the images I choose. And I’ve just read (on stackoverflow) about adding the image to a panel, then the panel to the scrollpane, and the scrollpane to the frame. So I tried doing that.

But I still can’t get the scrollbars to show. Even when the image is bigger than the JFrame window.

Can anyone help? I’ve tried revalidate, but it didn’t work so I took it out.

   import java.awt.*;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MakeResizedImage {
    private JFrame frame;
    private JFileChooser fc;
    private File file;
    private int r;
    private JTextField textField;
    private Image img;
    JScrollPane scrollPane;
    ImagePanel panel_2;

    public static void main(String[] args) {
        MakeResizedImage MRI = new MakeResizedImage();
        MRI.BuildJFrameGui();
    }

    private MakeResizedImage() {
    }

    private void BuildJFrameGui() {
        JPanel panel = new JPanel();
        panel.setLayout(null);

        frame = new JFrame("View/Resize a png, jpg or gif image");
        frame.setSize(400, 400);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        JPanel panel_1 = new JPanel();
        frame.getContentPane().add(panel_1, BorderLayout.NORTH);

        JLabel lblNewLabel = new JLabel("Image:");
        lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
        panel_1.add(lblNewLabel);

        textField = new JTextField();
        panel_1.add(textField);
        textField.setColumns(10);

        JLabel lblNewLabel_1 = new JLabel("Width:");
        panel_1.add(lblNewLabel_1);

        Dimension Dim = new Dimension(45, 20);
        JSpinner spinner = new JSpinner();
        spinner.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0),
                null, new Integer(1)));
        spinner.setPreferredSize(Dim);
        panel_1.add(spinner);

        JLabel lblHeight = new JLabel("Height:");
        panel_1.add(lblHeight);

        JSpinner spinner_1 = new JSpinner();
        spinner_1.setModel(new SpinnerNumberModel(new Integer(0),
                new Integer(0), null, new Integer(1)));
        spinner_1.setPreferredSize(Dim);
        panel_1.add(spinner_1);

        JPanel panel_3 = new JPanel();

        JButton btnNewButton = new JButton("Open");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                chooseFile();
            }
        });

        panel_3.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("Cancel");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
        });
        panel_3.add(btnNewButton_1);

        JButton btnSave = new JButton("Save");
        panel_3.add(btnSave);

        frame.getContentPane().add(panel_3, BorderLayout.SOUTH);

        scrollPane = new JScrollPane();
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

        panel_2 = new ImagePanel();
        scrollPane.setViewportView(panel_2);

        frame.setVisible(true);
    }

    public void chooseFile() {
        if (aFileChosen()) {
            file = fc.getSelectedFile();
            if (file.exists()) {
                textField.setText(fc.getName());
                try {
                    img = ImageIO.read(file);
                    panel_2.setImg(img);
                } catch (IOException e) {
                }
            }
        } else
            JOptionPane.showMessageDialog(frame, "No file was chosen");
    }

    public boolean aFileChosen() {
        fc = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
                "PNG, JPG & GIF Images", "png", "jpg", "gif");
        fc.setFileFilter(filter);
        fc.setApproveButtonText("Choose");
        fc.setApproveButtonToolTipText("Selects the image you want to resize");
        r = fc.showOpenDialog(frame);
        if (r == JFileChooser.APPROVE_OPTION)
            return true;
        return false;
    }

    class ImagePanel extends JPanel {

        private static final long serialVersionUID = 1L;
        private Image img;

        ImagePanel() {
        };

        ImagePanel(Image img) {
            this.img = img;
        }

        public void setImg(Image img) {
            this.img = img;

            repaint();
        }

        @Override
        public Dimension getPreferredSize() {
            return (new Dimension(300, 300));
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            // Clears the previously drawn image.     
            g.clearRect(0, 0, getWidth(), getHeight());
            //and draws the new one...
            g.drawImage(img, 0, 0, this);
        }
    }
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:02:49+00:00Added an answer on June 10, 2026 at 5:02 pm

    Use the other constructor, the one that wraps another component: JScrollPane(Component).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.