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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:13:41+00:00 2026-06-05T18:13:41+00:00

Hey guys I wanted to create a JScrollPane but it won’t work… and I

  • 0

Hey guys I wanted to create a JScrollPane but it won’t work… and I don’t know why… here’s my code…

public class test extends JFrame{
    public test(){      
        setSize(1000,600);

    }

    private static JButton[] remove;
    private static JPanel p = new JPanel();

    public static void main(String[]args){
        p.setLayout(null);
        JFrame t=new test();
        remove = new JButton[25];
        for(int i=0;i<25;i++){
            remove[i]=new JButton("Remove");
            remove[i].setBounds(243,92+35*i,85,25);
            p.add(remove[i]);       
        }
        JScrollPane scrollPane = new JScrollPane(p);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        t.add(scrollPane);
        t.setVisible(true);
    }

Umm and Im pretty sure the frame isn’t big enough for these 25 buttons… But if i delete that p.setLayout(null); A horizontal scroll bar will be created automatically… I don’t really know what is wrong with my code… Pls help thank you very much!

  • 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-05T18:13:42+00:00Added an answer on June 5, 2026 at 6:13 pm

    You need to set p’s preferredSize for this to work.

      p.setPreferredSize(new Dimension(800, 2000));
    

    Or you could have p extend JPanel and then override the getPreferredSize() method to return the proper dimension.

    And I agree — get rid of your null layouts. Learn about and use the layout managers if you want to use Swing correctly and have robust Swing GUI’s.

    e.g.,

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Foo extends JFrame {
    
       private static final int BUTTON_COUNT = 25;
    
       public static void main(String[] args) {
          JPanel btnPanel = new JPanel(new GridLayout(0, 1, 0, 20));
          btnPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
          AbstractAction removeAction = new AbstractAction("Remove") {
    
             @Override
             public void actionPerformed(ActionEvent evt) {
                JButton src = (JButton) evt.getSource();
                JPanel container = (JPanel) src.getParent();
                container.remove(src);
                container.revalidate();
                container.repaint();
             }
          };
          for (int i = 0; i < BUTTON_COUNT; i++) {
             JButton removeBtn = new JButton(removeAction);
             btnPanel.add(removeBtn);
          }
          JPanel borderPanel = new JPanel(new BorderLayout());
          borderPanel.add(btnPanel, BorderLayout.NORTH);
          JScrollPane scrollpane = new JScrollPane(borderPanel,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
          scrollpane.setPreferredSize(new Dimension(400, 800));
    
          JFrame frame = new JFrame("Foo");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(scrollpane);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys. I don't know much JS, but I wanted to do some quick
Hey guys, I don't know RegExp yet. I know a lil about it but
Hey guys I wanted to know if I get get some advice from some
Hey guys i dont know what i'm doing wrong here. www.example.com/data/stuff1/stuff2/stuff3 to www.example.com/anything.php?par1=data&&par2=stuff1/stuff2/stuff3 And
Hey guys as far as i don't have real android phone I wanted to
Hey guys, I wanted to know how to send data to a wordpress page
Hey guys i have a question here, How do i create a body that
hey guys i know this may sound stupid, but i am stuck with this
Hey guys this is my html code: <div class=nakupy> <li class=icn_kategorie><a href=#>Nákupy</a> <div class=sub_menu>
hey guys having this really simple problem but cant seem to figure out have

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.