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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:29:15+00:00 2026-06-01T10:29:15+00:00

I have a JPanel and I want do draw to this JPanel some very

  • 0

I have a JPanel and I want do draw to this JPanel some very simple images (items). I want to use methods like DrawRect or DrawOval.. This panel will have a scrollbar. It should look like this.

I need to remove and add item (images) on a specific index. Can you help me please?

  • 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-01T10:29:17+00:00Added an answer on June 1, 2026 at 10:29 am

    Start with a main JPanel which uses either a GridLayout or a vertical BoxLayout. Put this inside a JScrollPane. Inside the main JPanel, you’ll want to have instances of JPanel which extend the regular paintComponent() method to do drawing with drawRect(), drawOval(), etc. This should get you started:

    public JScrollPane buildScrollingContainerPanel()
    {
        JPanel containerPanel = new JPanel(new GridLayout(0, 1));
        JScrollPane scrollPane = new JScrollPane(containerPanel);
        containerPanel.add(new MyPanel(true, false));
        containerPanel.add(new MyPanel(false, true));
    
        return (scrollPane);
    }
    
    private class MyPanel extends JPanel
    {
        private boolean drawRect;
        private boolean drawOval;
    
        public MyPanel(boolean drawRect, boolean drawOval)
        {
            super();
            this.drawRect = drawRect;
            this.drawOval = drawOval;
        }
    
        public void setDrawRect(boolean b)
        {
            drawRect = b;
            repaint();
        }
    
        public void setDrawOval(boolean b)
        {
            drawOval = b;
            repaint();
        }
    
        @Override
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
    
            if (drawOval)
            {
                g.setColor(Color.RED);
                g.drawOval(16, 16, 16, 16);
            }
    
            if (drawRect)
            {
                g.setColor(Color.GREEN);
                g.drawRect(32, 32, 16, 16);
            }
        }
    }
    

    to access children of the containerPanel, use containerPanel.getComponent(int) then cast to MyPanel.

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

Sidebar

Related Questions

In my program I want to draw a simple score line graph. I have
I have a JPanel that looks something like this: JPanel panel = new JPanel();
I have a JPanel that contains an image. I want to draw rulers on
I have a JPanel like this: GridBagConstraints constPanelInfo = new GridBagConstraints(); panelInfo = new
I have a JPanel that I want to add some components. in particular JButton
I have a JPanel that I want to use to contain 3 vertical components:
I have a JScrollPane and JPanel inside it(I want to draw on JPanel). Also
I want to know can we have a JPanel with a Layout other than
I have a JPanel to which when a button is pressed I want to
I have a counter in a JPanel in my java applet. I want the

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.