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 8459373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:18:48+00:00 2026-06-10T13:18:48+00:00

I have created a program that has a JScrollPane . However, JScrollPane doesn’t seem

  • 0

I have created a program that has a JScrollPane. However, JScrollPane doesn’t seem to do what is supposed to. It won’t scroll although a component is larger in size than the JScrollPane.

Producing a SSCCE out of my program would be pain in the neck, so I will just give you the whole program with comment markers of important places. (IMPORTANT and END OF IMPORTANT) Anything out of the comment shouldn’t influence the issue.

When starting up a program click File>New then OK in the dialog. That will draw the problematic GUI.

P.S. Only height is important here, width-wise everything works.

package core;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;

//IMPORTANT!
@SuppressWarnings("serial")
class Page extends JPanel
{
public int content;

public Page(JPanel panel, int index)
{
    setMaximumSize(new Dimension(794, 1123));
    setAlignmentX(Component.CENTER_ALIGNMENT);
    setBackground(Color.WHITE);

    panel.add(this, index);

    content = 0;
}
}
/END OF IMPORTANT!

public class Main
{

public static Properties config;
public static Locale currentLocale;
public static ResourceBundle lang;

public static JFrame mWindow;

public static JMenuBar menu;
public static JMenu menuFile, menuEdit;
public static JMenuItem itmNew, itmClose, itmLoad, itmSave, itmSaveAs, itmExit, itmCut, itmCopy, itmPaste, itmProperties; 

public static JDialog newDoc;
public static JLabel newDocText1, newDocText2;
public static JRadioButton questions, list;
public static ButtonGroup newDocButtons;
public static JButton newDocOk;
public static Boolean firstSegment;

public static JPanel workspace;
public static JScrollPane scroll;
public static ArrayList<Page> pages;

//IGNORE WHOLE METHOD, HAS NOTHING TO DO WITH THE PROBLEM
public static void newDocumentForm()
{
    //new document dialog
    mWindow.setEnabled(false);

    newDoc = new JDialog(mWindow, lang.getString("newDoc"));
    newDoc.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    newDoc.addWindowListener(new WindowListener ()
    {
        public void windowActivated(WindowEvent arg0) {}
        public void windowClosing(WindowEvent arg0)
        {
            mWindow.toFront();

            newDocText1 = null;
            newDocText2 = null;
            questions = null;
            list = null;
            newDocButtons = null;
            newDocOk = null;
            newDoc.dispose();

            mWindow.setEnabled(true);               
        }

        public void windowClosed(WindowEvent arg0) {}
        public void windowDeactivated(WindowEvent arg0) {}
        public void windowDeiconified(WindowEvent arg0) {}
        public void windowIconified(WindowEvent arg0) {}
        public void windowOpened(WindowEvent arg0) {}   
    });

    newDoc.setSize(400, 200);
    newDoc.setLocationRelativeTo(null);
    newDoc.setResizable(false);
    newDoc.setLayout(null);
    newDoc.setVisible(true);

    newDocText1 = new JLabel(lang.getString("newDocText1"));
    newDocText1.setBounds(5, 0, 400, 20);

    newDocText2 = new JLabel(lang.getString("newDocText2"));
    newDocText2.setBounds(5, 20, 400, 20);

    newDoc.add(newDocText1);
    newDoc.add(newDocText2);

    firstSegment = true;

    questions = new JRadioButton(lang.getString("questions"));
    questions.setSelected(true);
    questions.setFocusPainted(false);
    questions.setBounds(10, 60, 400, 20);
    questions.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent arg0)
        {
            firstSegment = true;
        }
    });

    list = new JRadioButton(lang.getString("list"));
    list.setFocusPainted(false);
    list.setBounds(10, 80, 400, 20);
    list.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent arg0)
        {
            firstSegment = false;
        }
    });

    newDoc.add(questions);
    newDoc.add(list);

    newDocButtons = new ButtonGroup();
    newDocButtons.add(questions);
    newDocButtons.add(list);

    newDocOk = new JButton(lang.getString("ok"));
    newDocOk.addKeyListener(new KeyListener()
    {
        public void keyPressed(KeyEvent e)
        {
            if (e.getKeyCode() == KeyEvent.VK_ENTER)
            {
                newDocOk.doClick();
            }
        }

        public void keyReleased(KeyEvent e) {}
        public void keyTyped(KeyEvent e) {}
    });

    newDocOk.setFocusPainted(false);
    newDocOk.setBounds(160, 120, 80, 40);
    newDocOk.setMnemonic(KeyEvent.VK_ACCEPT);
    newDocOk.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent arg0)
        {
            createNewDocument();
        }
    });

    newDoc.add(newDocOk);
    newDocOk.requestFocus();
}

public static void createNewDocument()
{
    //dispose of new document dialog
    mWindow.toFront();

    newDocText1 = null;
    newDocText2 = null;
    questions = null;
    list = null;
    newDocButtons = null;
    newDocOk = null;
    newDoc.dispose();

    mWindow.setEnabled(true);

            //IMPORTANT!
    //create document display               
    workspace = new JPanel();
    workspace.setLayout(new BoxLayout(workspace, BoxLayout.PAGE_AXIS));
    workspace.add(new Box.Filler(new Dimension(0, 40), new Dimension(0, 40), new Dimension(0, 40)));
    workspace.setBackground(Color.BLACK);

    scroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scroll.getVerticalScrollBar().setUnitIncrement(20);
    scroll.setViewportView(workspace);

    pages = new ArrayList<Page>();

    @SuppressWarnings("unused")
    Page p = new Page(workspace, 1);


    mWindow.add(scroll, BorderLayout.CENTER);
    mWindow.repaint();
    mWindow.validate();
}
    //END OF IMPORTANT

public static void main(String[] args) throws FileNotFoundException, IOException
{   
    //load properties
    config = new Properties();
    config.load(new FileInputStream("src\\config.properties"));

    //load language
    currentLocale = new Locale(config.getProperty("language"), config.getProperty("country"));
    lang = ResourceBundle.getBundle("language", currentLocale);

            //IMPORTANT!
    //create main window
    mWindow = new JFrame(lang.getString("title"));
    mWindow.setSize(1000, 800);
    mWindow.setMinimumSize(new Dimension(1000, 800));
    mWindow.setLocationRelativeTo(null);
    mWindow.setLayout(new BorderLayout());
    mWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    mWindow.setVisible(true);
            //END OF IMPORTANT

    //create menu bar
    menu = new JMenuBar();

    menuFile = new JMenu(lang.getString("file"));
    menuEdit = new JMenu(lang.getString("edit"));

    itmNew = new JMenuItem(lang.getString("new"));
    itmNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
    itmNew.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            newDocumentForm();
        }
    });

    itmClose = new JMenuItem(lang.getString("close"));
    itmClose.setActionCommand("Close");
    itmClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK));

    itmLoad = new JMenuItem(lang.getString("load"));
    itmLoad.setActionCommand("Load");
    itmLoad.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));

    itmSave = new JMenuItem(lang.getString("save"));
    itmSave.setActionCommand("Save");
    itmSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));

    itmSaveAs = new JMenuItem(lang.getString("saveAs"));
    itmSaveAs.setActionCommand("SaveAs");
    itmExit = new JMenuItem(lang.getString("exit"));
    itmExit.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            //Add confirmation window!
            System.exit(0);
        }
    });


    itmCut = new JMenuItem(lang.getString("cut"));
    itmCut.setActionCommand("Cut");
    itmCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));

    itmCopy = new JMenuItem(lang.getString("copy"));
    itmCopy.setActionCommand("Copy");
    itmCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));

    itmPaste = new JMenuItem(lang.getString("paste"));
    itmPaste.setActionCommand("Paste");
    itmPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));

    itmProperties = new JMenuItem(lang.getString("properties"));
    itmProperties.setActionCommand("properties");
    itmProperties.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK));

    menuFile.add(itmNew);
    menuFile.add(itmClose);
    menuFile.addSeparator();
    menuFile.add(itmLoad);
    menuFile.addSeparator();
    menuFile.add(itmSave);
    menuFile.add(itmSaveAs);
    menuFile.addSeparator();
    menuFile.add(itmExit);

    menuEdit.add(itmCut);
    menuEdit.add(itmCopy);
    menuEdit.add(itmPaste);
    menuEdit.addSeparator();
    menuEdit.add(itmProperties);

    menu.add(menuFile);
    menu.add(menuEdit);

    //create actionListener for menus



    mWindow.add(menu, BorderLayout.NORTH);

    mWindow.repaint();
    mWindow.validate();
}
}
  • 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-10T13:18:49+00:00Added an answer on June 10, 2026 at 1:18 pm

    You set the maximum size of the viewport panel while JScollPane checks the preferred size.

    setPreferredSize(new Dimension(794, 1123));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a non-form c# program that uses the NotifyIcon class. The text
I have created a program that decrypts whats stored in a export file from
The whole story: I have created a fancy .NET program which has an installer
I have created a little program that reads in a Java file and feeds
If you create a JScrollPane that has a viewport larger than the JScrollPane's component,
Using VB6, I have created an Outlook plugin, that has a property page. The
I have a C# program that fails pretty consistently. That's ok, I've created the
I have created an office scheduling program that uses jQuery to post to a
I have a 3rd party program that I run under a specially created user
This is what I have done so far: Created a class that has my

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.