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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:08:18+00:00 2026-06-14T17:08:18+00:00

I am trying to create a JScrollPane that contains a JPanel that will be

  • 0

I am trying to create a JScrollPane that contains a JPanel that will be increasing and decreasing in height. When it becomes larger than the size of the JScrollPane, it should create a vertical scroll bar which will allow me to scroll through the entire JPanel. However, I am having difficulty achieving this. Yes, I know I am not using LayoutManagers. No, I will not be using them, and I need a solution that does not involve their usage.

Here are the two button’s AbstractActions that add and subtract from the JPanel:

class AddACT extends AbstractAction
    {
      public void actionPerformed(ActionEvent e)
      {
        info.setSize(420,info.getHeight() + 40);
        info.add(new SubPanel); // Adds another JPanel into the main JPanel (for content input)
        gui.repaint();
        infoS.validate();
      }
    }

class RemoveACT extends AbstractAction
    {
      public void actionPerformed(ActionEvent e)
      {
        info.remove(subPanel()); // This would remove the last JPanel added to the main JPanel
        info.setSize(420,info.getHeight() - 40);
        gui.repaint();
        infoS.validate();
      }

And here is the code for the main JPanel and the JScrollPane:

final JPanel info = new JPanel();
final JScrollPane infoS = new JScrollPane(info, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);  
info.setLayout(null);
info.setSize(420,600);
infoS.setLocation(10,80);
infoS.setSize(420,490);
gui.add(infoS); // gui is the frame's content pane (the overall JPanel)

This is the second project I’ve been trying to learn GUI by doing. I am a complete novice in Swing and am only intermediate in Java. Sorry if I am making a blindingly obvious mistake.

  • 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-14T17:08:19+00:00Added an answer on June 14, 2026 at 5:08 pm

    1) Use LayoutManagers (+1 to @kleopatra and @GagandeepBali comments)

    The absence of LayoutManagers only guarantees your GUI’s will look very trashy (especially when run on other OSes/builds) and being a Novice you should rather learn the correct way than learn the wrong way and get into bad habits like calling setSize() etc.

    Have a read on these links to get you started:

    • A Visual Guide to Layout Managers
    • Concurrency in Swing

    2) See this example for how to use a JScrollPane, it simply adds a JPanel with buttons to a JScrollPane which in-turn is added to the JFrame.

    3) Also see this example for how to make the JScrollPane vertically scroll-able only.

    4) For more on JScrollPanes have a look here: How to Use Scroll Panes.

    5) As for how it interacts with LayoutManager, if you do not explicitly set its size via setPreferredSize(Dimension d) the scroll pane computes it based on the preferred size of its nine components (the viewport, and, if present, the two scroll bars, the row and column headers, and the four corners)

    6) On your usage of validate():

    • validate() is used when new JComponents are added to a visible component

    • revalidate() is used when JComponent is removed/added from a visible component

    • revalidate() covers validate() too

    Thus always use this:

    //add or remove component(s)
    revalidate();
    repaint();
    

    References:

    • http://www.daniweb.com/software-development/java/threads/405568/validate-vs-revalidate
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying create a query that will output a total number, as well as
I'm trying to create a JPanel that displays webpages. I've gotten to the stage
I'm trying to create a custom cell renderer that will display an image in
I am trying create a small web application that allows a user to login
I am trying create a WCF service that leverages the WPF MediaPlayer on the
I am trying to create a JTable that has a row header that looks
I am trying create cpu load generator tool. In which my input will be
I'm trying create a gui using Tkinter that grabs a username and password and
I'm trying create this function such that if any key besides any of the
I am trying create a search box which will search datatables. The search box

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.