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

  • Home
  • SEARCH
  • 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 6774679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:48:59+00:00 2026-05-26T15:48:59+00:00

I have a java class that extends from JPanel. The basic purpose of extending

  • 0

I have a java class that extends from JPanel. The basic purpose of extending this class from JPanel is that i want to draw some graphical figures inside this JPanel. The code of this class is as follow:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
public class ObjectViewer extends JPanel {
    public void paintComponent(Graphics g) {
        //just for testing
        g.drawRect(0, 0, 100, 10);

        g.drawLine(150, 150, 250, 150);
    }
}

Now i am loading this JPanel in JScrollPane, by using the following code.

JPanel jpnl = new ObjectViewer();
jScrollPane2.add(jpnl);//Note: I drag and drop this JScrollPanel at desired location from NetBeans gui.

But i am unable to see any graphics (Rect and Line) in that JScrollPane, any suggestions for what i am doing wrong?

  • 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-05-26T15:49:00+00:00Added an answer on May 26, 2026 at 3:49 pm

    You never add your JPanel directly to a JScrollPane but rather to its viewport. So for instance, not

    jScrollPane2.add(jpnl);
    

    but rather something like:

    jScrollPane2.setViewportView(jpnl);
    

    or

    jScrollPane2.getViewport().add(jpnl);
    

    alternatively, you could add the JPanel as a parameter in the scrollpane’s constructor.

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

Sidebar

Related Questions

I have a problem making an inner class that extends from JPanel to draw
I have this class called PollFrame that extends JFrame in a file called PollFrame.java
I have a simple Java class that has some methods: public class Utils {
If I have a parent-child that defines some method .foo() like this: class Parent
I have the following situation. I have a Java Class that inherits from another
I have resource bundle as Java class that read values from database. When i
I have a class that extends javax.swing.JPanel , it contains a single JButton. I've
I have a java class that applies an xslt to all xml files in
I need your expertise once again. I have a java class that searches a
I'm currently working on Mangler's Android implementation. I have a java class that looks

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.