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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:59:18+00:00 2026-06-10T19:59:18+00:00

I don’t know why, but I can’t call the method paint(Graphics g) from code.

  • 0

I don’t know why, but I can’t call the method paint(Graphics g) from code. I have MouseListeners:

    @Override
    public void mouseReleased(MouseEvent e) {       
        pointstart = null;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        pointstart = e.getPoint();
    }

and MouseMotionListeners:

    @Override
    public void mouseMoved(MouseEvent e) {
        pointend = e.getPoint();
    }

    @Override
    public void mouseDragged(MouseEvent arg0) {
        pointend = arg0.getPoint();
        // ==========================================================================
        //          call repaint:

        repaint();


        // ==========================================================================
    }

and my paint method:

public void paint(Graphics g){
    super.paint(g); //here I have my breakpoint in debugger
    g.translate(currentx, currenty);
    if(pointstart!=null){
        g.setClip(chartPanel.getBounds());
        g.setColor(Color.BLACK);
        g.drawLine(pointstart.x, pointstart.y, pointend.x, pointend.y);
    }
}

in the initialize method:

currentx = chartPanel.getLocationOnScreen().x;
currenty = Math.abs(chartPanel.getLocationOnScreen().y - frame.getLocationOnScreen().y);

All in class, which is my work frame, it extends JFrame.

Problem is, that program doesn’t call paint method. I checked that in debugger.
I tried do this by paintComponent , without super.paint(g).
And the best is that, that code I copied from my other project, where it works fine.

UPDATE:
This is code which I want to draw line on panel (no matter – panel/chartpanel/etc). And it doesn’t paint. I tried do this by paint(Graphics g){super.paint(g) ….. } and it doesn’t too. Painting should be aneble after clicking button “line”.

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class Window extends JFrame {
private JFrame frame;
public JPanel panelbuttons;
public JPanel panelscrollpane;
public JButton btnLine;
public JToolBar toolBar;
public JScrollPane scrollPane;
public JPanel panel;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Window window = new Window();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public Window() {
    initialize();
}

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 644, 430);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel = new JPanel();

    panelbuttons = new JPanel();
    frame.getContentPane().add(panelbuttons, BorderLayout.WEST);
    panelbuttons.setLayout(new BorderLayout(0, 0));

    toolBar = new JToolBar();
    toolBar.setOrientation(SwingConstants.VERTICAL);
    panelbuttons.add(toolBar, BorderLayout.WEST);

    btnLine = new JButton("line");
    btnLine.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            panel.addMouseListener(mouselistenerLine);
            panel.addMouseMotionListener(mousemotionlistenerLine);
        }
    });
    toolBar.add(btnLine);

    panelscrollpane = new JPanel();
    frame.getContentPane().add(panelscrollpane, BorderLayout.CENTER);
    panelscrollpane.setLayout(new BorderLayout(0, 0));



    scrollPane = new JScrollPane(panel);
    panel.setLayout(new BorderLayout(0, 0));
    scrollPane.setViewportBorder(null);
    panelscrollpane.add(scrollPane);
    panelscrollpane.revalidate();
}

    Point pointstart = null;
    Point pointend = null;

private MouseListener mouselistenerLine = new MouseListener() {


        @Override
        public void mouseReleased(MouseEvent e) {
            System.out.println("I'm in first listener. - mouse Released");
            pointstart = null;
        }

        @Override
        public void mousePressed(MouseEvent e) {
            System.out.println("I'm in first listener. - mousePressed");
            pointstart = e.getPoint();
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseClicked(MouseEvent e) {
        }

    };

    private MouseMotionListener mousemotionlistenerLine = new MouseMotionListener() {

            @Override
            public void mouseMoved(MouseEvent e) {
                System.out.println("I'm in second listener. - mouseMoved");
                pointend = e.getPoint();
            }

            @Override
            public void mouseDragged(MouseEvent arg0) {
                System.out.println("I'm in second listener. - mouseDragged");
                pointend = arg0.getPoint();
                repaint();
            }

    };

    public void paintComponent(Graphics g){

        System.out.println("I'm in paintComponent method.");

        if(pointstart!=null){
            System.out.println("I'm drawing.");
            g.setClip(scrollPane.getBounds());
            g.setColor(Color.BLACK);
            g.drawLine(pointstart.x, pointstart.y, pointend.x, pointend.y);

        }
    }
}
  • 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-10T19:59:20+00:00Added an answer on June 10, 2026 at 7:59 pm

    You are creating two separate instances of JFrame and showing only one.
    One instance is created because Window class extends JFrame, and the second is created inside initialize method.

    To fix this without a lot of changes in code do this:

    private void initialize() {
        frame = this;
        frame.setBounds(100, 100, 644, 430);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    

    BTW. change paintComponent(Graphic g) to paint(Graphic g) because JFrame is not a JComponent.

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

Sidebar

Related Questions

Don't know if this is possible, but I have some code like this: val
Don't know why but I can't find a solution to this. I have 3
don't know better title for this, but here's my code. I have class user
I don't know why, but this code worked for me a month ago... maybe
Don't know where else to ask, but from one day to the other my
Don't ask me why but i can't use this method because I need to
Don't know how to frase this but I found this code wich works as
don't know if the title describes anything about what I'm trying to say but
Don't ask me how but I'm in a situation where I have DCPs published
Don't really know how to formulate the title, but it should be pretty obvious

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.