views:

67

answers:

2

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  A: 
  • Attach a MouseListener to the button
  • Attach a MouseMotionListener to the button
  • On mousePressed you register the location of the MouseEvent
  • On mouseDragged you register the new location of the MouseEvent you take the delta of the two events and add that to the size of the button using setSize/setPreferredSize/whatever they are called
willcodejavaforfood
can u explain with a code sample ?
Deepak
I think I've given you quite a lot of help here and there would not be much code. I think you should try yourself first and show me some code if you cant get it working :)
willcodejavaforfood
This is basically how I did it. Very simple. And even simpler to use since I just extended `JButton`.
jjnguy
+1  A: 

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;
            }
        });
    }
}
jjnguy
Also, if you want to be able to move the button after it has been placed, well I think I could do that as well.
jjnguy
They will never learn if we spoon feed them :)
willcodejavaforfood
I think for a moving button you would have to avoid a LayoutManager and use absolute positioning
willcodejavaforfood
@willcodejava, he is dynamically placing buttons on a 'canvas' wherever a user clicks. So I know that he is already not using a layout manager. http://stackoverflow.com/questions/3468844/java-swing-add-remove-jbuttons-on-runtime
jjnguy
@will, I also couldn't help but solve this one. It seemed like a fun little challenge.
jjnguy
Do you have inside information? :)
willcodejavaforfood
@will, kinda. I helped him with his previous problem too.
jjnguy
@willcodejavaforfood: I appreciate for you approach. I am relatively new to swing concepts and i am in a situation to complete an application given to me. So i dont have much time to trial and error :) @Justin 'jjnguy' Nelson : I will try this and get back to you!!
Deepak
Perfect i got that working!!!! Thanks a lot again!!
Deepak
@Deepak, thanks for posing interesting questions!
jjnguy
@ Nelson: i have edited my post have a look!! i got a new problem!! i can resize and move the buttons but when i move them the height and width are getting swapped repeatedly so it is giving poor effect on the screen.
Deepak
@Deepak. Hopefully I will get time to look at this tomorrow (for me). But, its late in the US, and I'm going to bed.
jjnguy
@nelson: yeh thats fine !!!
Deepak
@Deepak, I had to rewrite the code because I had all of it saved at work. But, this new version will allow you to move a button, or resize the button. To change the behavior, just press the `Alt` key while clicking on the button.
jjnguy
@nelson: I tried your code i am getting null pointer exception at startPoint.x , startPoint.width etc...
Deepak
@Deepak, hmm, I don't get that error. But, try the new version that has the `isHeld` boolean.
jjnguy
@Nelson: With your codes and some self mod i made tat work somehow. Now i am getting one problem!! when i use Point newPoint = E.getLocationOnScreen(); to place the button in new location my mouse cursor is in one point and the button is floating away from it. When i use E.getPoint(); i can see oscillating button between 2 points. Y do i get that ?
Deepak
@Nelson: I managed to fit in your code.. that works like charm!! thanks a lot again :) I think i need ur help in future as well..
Deepak
@Deepak, glad you got it to work.
jjnguy
@Nelson: help me out in another query here http://stackoverflow.com/questions/3485057/adding-and-hiding-jlayeredpane-dynamically
Deepak