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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:41:00+00:00 2026-06-11T04:41:00+00:00

I am trying to create a swing program. In my program I want to

  • 0

I am trying to create a swing program. In my program I want to achieve something like this: right click on a panel and select the menu “Draw rectangle” and program should draw a very simple rectangle on the panel. Here is my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;

public class MainWindow extends JFrame {

    JFrame frame = null;
    AboutDialog aboutDialog = null;
    JLabel statusLabel = null;    //label on statusPanel

    public MainWindow() {
        frame = new JFrame("Project");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //MENUS
        JMenuBar menuBar = new JMenuBar();                //menubar
        JMenu menuDosya = new JMenu("Dosya");           //menus on menubar
        JMenu menuYardim = new JMenu("Yardım");               //menus in menus
        menuBar.add(menuDosya);
        menuBar.add(menuYardim);
        JMenuItem menuItemCikis = new JMenuItem("Çıkış", KeyEvent.VK_Q);        //dosya menus
        menuItemCikis.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });
        menuDosya.add(menuItemCikis);
        JMenuItem menuItemYardim = new JMenuItem("Hakkında", KeyEvent.VK_H);      //hakkinda menus
        menuItemYardim.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                JDialog f = new AboutDialog(new JFrame());
                f.show();
            }
        });
        menuYardim.add(menuItemYardim);
        frame.setJMenuBar(menuBar);
        //TOOLBAR
        JToolBar toolbar = new JToolBar();
        JButton exitButton = new JButton("Kapat");
        toolbar.add(exitButton);
        //STATUSBAR
        JPanel statusPanel = new JPanel();
        statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
        frame.add(statusPanel, BorderLayout.SOUTH);
        statusPanel.setPreferredSize(new Dimension(frame.getWidth(), 20));
        statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
        statusLabel = new JLabel("Ready.");
        statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
        statusPanel.add(statusLabel);
        //MAIN CONTENT OF THE PROGRAM
        final JPanel mainContentPanel = new JPanel();
        //RIGHT CLICK MENU
        final JPopupMenu menuSag = new JPopupMenu("RightClickMenu");
        JMenuItem menuRightClickRectangle = new JMenuItem("draw rectangle");
        menuRightClickRectangle.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                //CircleShape cs=new CircleShape();
                mainContentPanel.add(new CircleShape()); //trying to draw.

                mainContentPanel.repaint();
                //mainContentPanel.repaint();     boyle olacak.
            }
        });
        JMenuItem menuRightClickCircle = new JMenuItem("Daire çiz");
        menuSag.add(menuRightClickRectangle);
        menuSag.add(menuRightClickCircle);
        mainContentPanel.addMouseListener(new MouseAdapter() {

            public void mouseReleased(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON3) {
                    menuSag.show(e.getComponent(), e.getX(), e.getY());
                    statusLabel.setText("X=" + e.getX() + "  " + "Y=" + e.getY());


                }
            }
        });
        JButton west = new JButton("West");
        JButton center = new JButton("Center");
        JPanel content = new JPanel();   //framein icindeki genel panel. en genel panel bu.
        content.setLayout(new BorderLayout());
        content.add(toolbar, BorderLayout.NORTH);
        content.add(statusPanel, BorderLayout.SOUTH);
        content.add(west, BorderLayout.WEST);
        content.add(mainContentPanel, BorderLayout.CENTER);
        frame.setContentPane(content);
        frame.setPreferredSize(new Dimension(400, 300));
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }
}

The problem is nothing is drawn on the panel. I guess there is an event loss in the program but i don’t know how to solve this issue.

  • 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-11T04:41:02+00:00Added an answer on June 11, 2026 at 4:41 am

    Please modify the code in the following way. Add a new class like this:

      class MainPanel extends JPanel {
    
        private List<Rectangle> rectangles = new ArrayList<Rectangle>();
    
        private void addRectangle(Rectangle rectangle) {
          rectangles.add(rectangle);
        }
    
        public void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D) g;
          for (Rectangle rectangle : rectangles) {
            g2.drawRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
          }
        }
    
      }
    

    Then, instead of

    final JPanel mainContentPanel = new JPanel();
    

    you should do:

    final MainPanel mainContentPanel = new MainPanel();
    

    And the action listener for the menu item becomes something like this:

    menuRightClickRectangle.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        // TODO: add your own logic here, currently a hardcoded rectangle
        mainContentPanel.addRectangle(new Rectangle(10, 10, 100, 50));
        mainContentPanel.repaint();
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a memory game in Java. Something like this, but
I'm trying to create a simple slideshow program like Powerpoint. In order to design
I am trying to create 10.000 hexagon connected each other like bee combs.I want
I'm trying to create a Java swing application and I'm trying to use a
I am trying to create a GUI using java swing. From there I have
I m trying to create a desktop application using swing in java, which allows
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
I am trying to create a help panel for an application I am working
i'm trying to make a program that will connect two points that i click
I'm trying to create image from SQLite database in Java Swing application. But it

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.