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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:29:16+00:00 2026-05-27T05:29:16+00:00

I’ve got a problem that I’m trying to solve for hours, and I’d be

  • 0

I’ve got a problem that I’m trying to solve for hours, and I’d be glad if you could help me. My program is a kind of graph drawing program with Swing GUI.
I’ve got a Draw2 class for drawing, overriding paintcomponent. There is a control class for the GUI. The control and drawing window are seperate JFrames-s. What I’m trying to do is to draw at button click but I’ve got problems with the communication between objects.
I’ve tried to implement the drawing to button click with an if condition in the paintcomponent method, if the boolean is true the method should draw, if it’s not it shouldn’t. I’d change the boolen to true in the button’s actionlistener and repaint the window. How can I reach the instance of Draw2 in the DrawAction method? Sorry if my question is very stupid but i’ve just begun learning Java. (I’ve seen a similar topic here but I didn’t really understood the answer there) So the relevant part of my code:

public class Draw2 extends JPanel{
   boolean toDraw;

public void paintComponent (Graphics g) {
    super.paintComponent(g);
    if (toDraw == true){
        //Draw Graph
    }
}

}

public class Control extends JPanel{
private JButton jButton1;
private JButton jButton2;

void createControl(){
    JButton1 = new JButton("Draw");
    jButton1.addActionListener(new DrawAction());

    //Other  JTextfields, JComboBoxes, etc. with groupLayout
}

//inner class:
public class DrawAction implements ActionListener{
    public void actionPerformed(ActionEvent arg0) {
        //How should I change toDraw in the instance of Draw2
        //repaint the "canvas"          
    }
}

}

public static void main(String[] args){

    JFrame frame = new JFrame("Control");
    JFrame frame2 = new JFrame("Draw");


    Draw2 gp = new Draw2();
    control cont = new control();
    cont.createControl(frame);



    gp.setPreferredSize(new Dimension(0,0));

    //Control Frame
    frame.setSize(800,330);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(cont, BorderLayout.NORTH);
    frame.setVisible(true);

    //Drawing Frame
    frame2.setSize(800,600);
    frame2.setLocation(0, 330);
    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   

    frame2.add(gp);
    frame2.setVisible(true);        
}

Thanks in advance

  • 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-27T05:29:16+00:00Added an answer on May 27, 2026 at 5:29 am

    I would extend the createControl(frame) so it would also take Draw2 as an argument:

    createControl(frame, gp).

    This new builder method would set an instance of Draw2 inside your Control class.

    public class Control extends JPanel
    {
        private JButton jButton1;
        private JButton jButton2;
        private Draw2 draw;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into

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.