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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:01:13+00:00 2026-05-16T07:01:13+00:00

I want to resize the JButton at runtime by clicking on its border and

  • 0

I want to resize the JButton at runtime by clicking on its border and draging it. Can anyone explain me how to do it with a sample code.

public void mouseDragged(MouseEvent E)
{

Point point= E.getPoint();
//JButton get = floor_plan.dynamicButtons.get(E.getComponent());
JButton get=(JButton) E.getComponent();
int height = get.getHeight();
int width = get.getWidth();
int X=E.getXOnScreen();
int Y=E.getYOnScreen();
if(floor_plan.resize==1)
    if (floor_plan.isHeld) {
        System.out.println(X);
        System.out.println(Y);
        get.setPreferredSize(
              new Dimension(floor_plan.grabbedDimension.width -  
                                 (floor_plan.grabbedPoint.x - point.x), 
                            floor_plan.grabbedDimension.height - 
                                 (floor_plan.grabbedPoint.y - point.y)));
        get.setBounds(new Rectangle(get.getLocation(), get.getPreferredSize()));
        return;
    }
    System.out.println("height:"+height);
    System.out.println("width:"+width);
    get.setBounds(X-240,Y-125,height,width);

}
  • 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-16T07:01:14+00:00Added an answer on May 16, 2026 at 7:01 am

    Well, this might do it for you. When adding the new button, simply add a ResizableButton instead.

    Note, it will resize no matter where you click on it.

    public class ResizableButton extends JButton {
    
        private Point grabbedPoint;
        private Dimension grabbedDimension;
    
        private boolean isHeld = false;
    
        public ResizableButton(String name) {
            super(name);
            addMouseListener(clickListener);
            addMouseMotionListener(moveListener);
        }
    
        private MouseMotionListener moveListener = new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                if (isHeld) {
                    Point newP = e.getPoint();
                    setPreferredSize(new Dimension(grabbedDimension.width
                            - (grabbedPoint.x - newP.x), grabbedDimension.height
                            - (grabbedPoint.y - newP.y)));
                    setBounds(new Rectangle(getLocation(), ResizableButton.this
                            .getPreferredSize()));
                }
            }
        };
    
        private MouseListener clickListener = new MouseAdapter() {
    
            @Override
            public void mouseReleased(MouseEvent e) {
                isHeld = false;
            }
    
            @Override
            public void mousePressed(MouseEvent e) {
                isHeld = true;
                grabbedPoint = e.getPoint();
                grabbedDimension = ((JButton) e.getSource()).getSize();
            }
        };
    }
    

    Below is code for a button that moves or re-sizes based on whether or not the Alt button is pressed.:

    public class MovableResizableButton extends JButton {
    
        private boolean isHeld;
        private Point pointClicked;
        private Dimension startingSize;
    
        public MovableResizableButton(String name) {
            super(name);
            addMouseMotionListener(new MouseMotionAdapter() {
                @Override
                public void mouseDragged(MouseEvent e) {
                    if (isHeld) {
                    Dimension newSize = getPreferredSize();
                    Point newPoint = getLocation();
                    if ((e.getModifiersEx() & InputEvent.ALT_DOWN_MASK) == InputEvent.ALT_DOWN_MASK) {
                        newSize = new Dimension(startingSize.width - (pointClicked.x - e.getPoint().x),
                                                startingSize.height -(pointClicked.y - e.getPoint().y));
                    }else {
                        Point startPoint = getLocation();
                        newPoint = new Point(startPoint.x - (pointClicked.x - e.getPoint().x), 
                                             startPoint.y - (pointClicked.y - e.getPoint().y));
                    }
                    setPreferredSize(newSize);
                    setBounds(new Rectangle(newPoint, getPreferredSize()));
                    }
                }
            });
            addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    pointClicked = e.getPoint();
                    startingSize = getSize();
                    isHeld = true;
                }
                @Override
                public void mouseReleased(MouseEvent e) {
                    isHeld = false;
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to resize a div from client-side, it's default value is height:500px, clicking
I want to resize the font of a SPAN element's style until it the
I want resize browser window to 1000x700 pixel by default on load of html
I want to resize an image with the GDI library so that when I
I want to resize cell's height according to the label's height and label's height
I want to resize bmp images in codeigniter. How do i do that. Codeigniter
I want to resize some window with XResizeWindow(display, xid, width, height) which was created
I have created a WriteableBitmap in Gray16 format. I want to resize this WriteableBitmap
if you try my code below you can see that the background color is
How do I prevent my form from being resized? I don't want it to

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.