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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:53:07+00:00 2026-05-27T00:53:07+00:00

I can not access public method of my class MyPanel which extends JPanel .

  • 0

I can not access public method of my class MyPanel which extends JPanel. In MyPanel I previously had private method called moveSquare, but in order to make it work for external requests, I changed it to public, but it still do not work? How to make it working?

import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseMotionAdapter;

public class mull {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JPanel p = createAndShowGUI();
                iterate(p);
                // "The method iterate(MyPanel) in the type mull is not applicable for the arguments (JPanel)"
            }
        });
    }

    private static JPanel createAndShowGUI() {
        System.out.println("Created GUI on EDT? "+
        SwingUtilities.isEventDispatchThread());
        JFrame f = new JFrame("Swing Paint Demo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        // how to grab link to this panel - in order to use it in iteration loop ?
        MyPanel p = new MyPanel();
        f.add(p);
//        f.add(new MyPanel());
        f.pack();
        f.setVisible(true);
        return p;
    } 
    private static void iterate(JPanel p){
        // the loop should change square position on each iteration
        // how to implement ?

        for (int i  = 0; i < 999; i++){
            ((MyPanel) p).moveSquare(100 + i*10, 200 + i*10); // here is problem: 
            //"Cannot make a static reference to the non-static method moveSquare(int, int) from the type MyPanel"
        }


    }
}

class MyPanel extends JPanel {

    private int squareX = 50;
    private int squareY = 50;
    private int squareW = 200;
    private int squareH = 200;

    public MyPanel() {

        setBorder(BorderFactory.createLineBorder(Color.black));

        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                moveSquare(e.getX(),e.getY());
            }
        });

        addMouseMotionListener(new MouseAdapter() {
            public void mouseDragged(MouseEvent e) {
                moveSquare(e.getX(),e.getY());
            }
        });

    }
    // originally this method was private - in orger to access it within mull, it vas changed to public
    public void moveSquare(int x, int y) {
        int OFFSET = 1;
        if ((squareX!=x) || (squareY!=y)) {
            repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
            squareX=x;
            squareY=y;
            repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
        } 
    }


    public Dimension getPreferredSize() {
        return new Dimension(900,700);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);       
        g.drawString("This is my custom Panel!",10,20);
        g.setColor(Color.RED);
        g.fillRect(squareX,squareY,squareW,squareH);
        g.setColor(Color.BLACK);
        g.drawRect(squareX,squareY,squareW,squareH);
    }  
}

What does this syntax do ((MyPanel) p).moveSquare(100 + i*10, 200 + i*10); ?
How to paint custom shapes (ovals) in iterate(p)?

  • 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-27T00:53:08+00:00Added an answer on May 27, 2026 at 12:53 am

    It doesn’t seem like you’re even calling iterate().

    But in any case, your moveSquare method is not static, and thus expects a specific MyPanel instance to work on. So you’ll probably want to keep a reference to your panel. Say, instead of f.add(new MyPanel()), you could use:

    MyPanel p = new MyPanel();
    f.add(p);
    

    and then pass p to iterate and call p.moveSquare(100,200) in there.

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

Sidebar

Related Questions

Why I can not access my variable p in mull class's iterate method? public
I have created a class called SimpleCanvas which extends the JPanel class, and I
In Java, I can access a public member of a class using . as
I have a static utility class, FileUtils which has the following method: public static
I have this class where I have a private property and a public method
In C#, we can not change access modifier while overriding a method from base
Can I access a users microphone in Python? Sorry I forgot not everyone is
The following code does not compile, saying error C2248: 'A::getMe' : cannot access private
I need to create a proxy for a class. I do not have access
I realize that it's possible to define a static class method as private and

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.