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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:39:45+00:00 2026-05-22T14:39:45+00:00

I have a question. Can I create glasspane in body MousePressed ? If yes

  • 0

I have a question. Can I create glasspane in body MousePressed ? If yes anyone can write me how? I mean that I press mouse button and glass pane is visible and I can painting on him.

EDIT

Ok I have now what I want. My glass pane is creating when I click mouse button and disapear when I release this button. Now I have another question. Where I should create my painting method. I want draw rectangle on this glasss pane using mouse dragged. Where I must implement paint method? In other class or in this events? I implement one my try paint function but I don’t know if this is good way. This is my code:

public class Selection extends JPanel
{
    static Point startPoint;
    public static void GUI()
    {

        final JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        JButton button = new JButton("Select");
        final JPanel glassPane = new JPanel();


        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.add(button);
        glassPane.setOpaque(false);
        frame.add(panel);


        frame.setGlassPane(glassPane);
        glassPane.addMouseListener(new MouseAdapter()
        {

            @Override
            public void mousePressed(MouseEvent e)
            {
                super.mousePressed(e);
                System.out.println("f.getGlassPane() mousePressed");
                if(e.getButton() == MouseEvent.BUTTON1)


                frame.getGlassPane().setVisible(true);

                startPoint=e.getPoint();

                Graphics2D g = null;
                Graphics2D g2 = (Graphics2D) g;
                Rectangle2D rect = new Rectangle2D.Double();
                rect.setFrameFromDiagonal(e.getPoint().x, e.getPoint().y,startPoint.x, startPoint.y);
                g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5F));
                g2.setColor(Color.BLUE);
                g2.fill(rect);
                g2.draw(rect);

            }

        });
        glassPane.addMouseMotionListener(new MouseMotionListener() {


            @Override
            public void mouseDragged(MouseEvent e) 
            {


            }

            @Override
            public void mouseMoved(MouseEvent e) {
                // TODO Auto-generated method stub

            }
        });

        frame.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent e)
            {
                super.mousePressed(e);
                if(e.getButton() == MouseEvent.BUTTON1)

                frame.getGlassPane().setVisible(true);

            }
            public void mouseReleased(MouseEvent e)
            {                     
                    frame.getGlassPane().setVisible(false);
            }
        });



        frame.setVisible(true);
    }


        int x1, x2, y1,y2;
        public void paintComponent(Graphics g)
        {
            Graphics2D g2 = (Graphics2D)g;
             g2.drawRect(x1,y1, x2, y2);
        }


    public static void main(String[] args) 
    {

        GUI();
    }

}
  • 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-22T14:39:46+00:00Added an answer on May 22, 2026 at 2:39 pm

    Hi please check out my answer to some other question where I present a way in which a glass pane can be used to simulated dialog behaviour. There you have shown how to show it and hide it on mouse click in my case right mouse click. This example should get you started nicely.

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

Sidebar

Related Questions

I have a question about joptionpane. Using JOptionPane.showMessageDialog(...), we can create a message dialog.
I have a question that can I use AS400ConnectionPool in Spring if then pls
I have probably stupid question but how Can I create Many-To-Many relationship with create
this question can create a misunderstanding: I know I have to use CSS to
I am new to cryptography, so I have a question: How can I create
So I have, what would seem like a common question that I can't seem
Reading this article http://support.microsoft.com/kb/813878 I have a question: Where can I get ipseccmd.exe for
I have another question and I can't seem to find anything on Google. What
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have not been able to find any definitive answers to this question: Can

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.