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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:43:25+00:00 2026-06-14T04:43:25+00:00

I am trying to implement a menu based graph – wherein i will represent

  • 0

I am trying to implement a menu based graph – wherein i will represent a vertex by a circle and each circle will have an index provided by the user.The user has to go to File menu and click on Addvertex to create a new node with an index.The Problem though is – The circle is drawn only once – any subsequent clicks to addVertex does not result in drawing of a circle -Can’t understand Why…

Here is my code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;
import java.util.*;

public class FileChoose extends JFrame {

    public FileChoose() {
        JMenuBar l=new JMenuBar();
        JMenu file=new JMenu("File");
        JMenuItem open=new JMenuItem("Addvertex");
        open.addActionListener(new Drawer());
        JMenuItem close=new JMenuItem("Exit");
        close.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        JMenu tools=new JMenu("Tools");
        file.add(open);
        file.add(close);

        this.setJMenuBar(l);
        l.add(tools);
        l.add(file);

        this.setSize(new Dimension(200, 200));
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
    }

    void HelloHere(String p) {
        Draw d = new Draw(p);
        this.add(d);
        setExtendedState(MAXIMIZED_BOTH);
    }

    class Drawer extends JFrame implements ActionListener {

        Integer index;

        public void actionPerformed(ActionEvent e) {
            JPanel pn = new JPanel();
            JTextField jt = new JTextField(5);
            JTextField jt1 = new JTextField(5);
            pn.add(jt);
            pn.add(jt1);
            int result=JOptionPane.showConfirmDialog(null, pn, "Enter the values", JOptionPane.OK_CANCEL_OPTION);
            if (result == JOptionPane.OK_OPTION) {
                index = Integer.parseInt(jt.getText());
                System.out.println(jt1.getText());
            }

            this.setVisible(true);
            this.setSize(500, 500);
            this.setLocationRelativeTo(null);

            HelloHere(index.toString());
        }
    }

    public static void main(String [] args) {
        FileChoose f = new FileChoose();
        f.setVisible(true);
    }
}

class Draw extends JPanel {

    String inp;

    public Draw(String gn) {
        inp = gn;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;
        Random r = new Random();
        int x = r.nextInt(100);

        g2.drawOval(x, x * 2, 100, 100);
        g2.drawString(inp, x + (x / 2), x + (x / 2));
    }
}
  • 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-14T04:43:27+00:00Added an answer on June 14, 2026 at 4:43 am

    Some pointers:

    • Use EDT for creation and manipulation of Swing Components.

    • Dont extend JFrame class unnecessarily.

    • Use anonymous Listeners where possible/allowed

    • Make sure JFrame#setVisible(..) is the last call on JFrame instance specifically pointing here:

          this.setVisible(true);
          this.setSize(500, 500);
          this.setLocationRelativeTo(null);
          HelloHere(index.toString());
      
    • Call pack() on JFrame instance rather than setSize(..)

    • Do not use multiple JFrames see: The Use of Multiple JFrames: Good or Bad Practice?

    • The problem you are having is here:

      Draw d = new Draw(p);
      this.add(d);
      

      You are creating a new instance of Draw JPanel each time and then overwriting the last JPanel added with the new one (this is default the behavior of BorderLayout). To solve this read below 2 points:

    • Call revalidate() and repaint() on instance after adding components to show newly added components.

    • Use an appropriate LayoutManager

    As I am not sure of you expected results I have done as much fixing of the code as I could hope it helps:

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.*;
    import javax.swing.*;
    
    public class FileChoose {
    
        JFrame frame;
    
        public FileChoose() {
            frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JMenuBar l = new JMenuBar();
            JMenu file = new JMenu("File");
            JMenuItem open = new JMenuItem("Addvertex");
    
            open.addActionListener(new ActionListener() {
                Integer index;
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    JPanel pn = new JPanel();
                    JTextField jt = new JTextField(5);
                    JTextField jt1 = new JTextField(5);
                    pn.add(jt);
                    pn.add(jt1);
                    int result = JOptionPane.showConfirmDialog(null, pn, "Enter the values", JOptionPane.OK_CANCEL_OPTION);
                    if (result == JOptionPane.OK_OPTION) {
                        index = Integer.parseInt(jt.getText());
                        System.out.println(jt1.getText());
                    }
                    HelloHere(index.toString());
                }
            });
    
            JMenuItem close = new JMenuItem("Exit");
            close.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
    
            JMenu tools = new JMenu("Tools");
            file.add(open);
            file.add(close);
    
            frame.setJMenuBar(l);
            l.add(tools);
            l.add(file);
            frame.pack();
    
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        void HelloHere(String p) {
            Draw d = new Draw(p);
            frame.add(d);
            frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            frame.revalidate();
            frame.repaint();
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    FileChoose f = new FileChoose();
                }
            });
        }
    }
    
    class Draw extends JPanel {
    
        String inp;
    
        public Draw(String gn) {
            inp = gn;
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            Graphics2D g2 = (Graphics2D) g;
            Random r = new Random();
            int x = r.nextInt(100);
    
            g2.drawOval(x, x * 2, 100, 100);
            g2.drawString(inp, x + (x / 2), x + (x / 2));
        }
    }
    

    UPDATE:

    Declare this globally in your Draw class: Random r=new Random(); as if you iniate a new Random instance each time you call paintComponent() the distributions will not be significant/random enough

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

Sidebar

Related Questions

I'm trying to implement Menu in a Window using MVVM pattern. So I have
I am trying to implement a menu into an area. http://www.gardensandhomesdirect.co.uk/newhomepage You will see
I am trying to implement an iOS popover-style menu for a mobile layout based
I am trying to implement a very simple little dropdown-style mini-menu that will appear
I have a page design I'm trying to implement with a header (menu bar)
I'm trying to implement a css menu in a website, and have run into
I'm trying to implement a tab menu just like the one in Stack Overflow.
I'm trying to implement a CSS menu and am having a problem with the
Greetings, for one of my applications I'm trying to implement an Edit menu. This
I'm trying to implement a simple horizontal navigation menu that just shows a single

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.