Resizing of Jbutton when we drag the button

Hi ,
I want to resize the button dynamically when we drag the edge of the button.Is it possible in swing.Can you
please send me the sample code.
Thanking you,
with regards,
Revathi

Hi Revathi,
Here is a sample test code you can execute directly :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.*;
import javax.swing.border.*;
public class MyFrame extends JFrame {
     public static String windowsUI = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
     private static final Cursor horizontalCursor = new Cursor(Cursor.W_RESIZE_CURSOR);
     private static final Cursor verticalCursor = new Cursor(Cursor.S_RESIZE_CURSOR);
     private static final Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
     public MyFrame() {
          super("My Frame");
          setWindowsLF();
          addWindowListener(new WindowAdapter() {
               public void windowClosing(WindowEvent e) {
                    System.exit(0);
          setBounds(312,250,412,200);
          Container c = getContentPane();
          c.setLayout(null);     // very important
          JButton b1 = new JButton("button", new ImageIcon("save.gif"));
          c.add(b1);
          b1.setBounds(10,10,100,50);
          new ButtonResizeListener(b1);
          JToggleButton b2 = new JToggleButton("toggle", new ImageIcon("save.gif"));
          c.add(b2);
          b2.setBounds(120,10,100,50);
          new ButtonResizeListener(b2);
          setVisible(true);
     final class ButtonResizeListener extends MouseInputAdapter {
          private int selectedEdge = 0;
          private JComponent component;
          private Rectangle bounds;
          public ButtonResizeListener(JComponent component) {//can't be shared my several components
               this.component = component;
               component.addMouseListener(this);
               component.addMouseMotionListener(this);
          private final void getCursor(Point p) {
               double x = p.getX();
               double y = p.getY();
               bounds = component.getBounds();
               if (x<3) {
                    component.setCursor(horizontalCursor);
               else
               if (x>bounds.getWidth()-4) {
                    component.setCursor(horizontalCursor);
               else
               if (y<3) {
                    component.setCursor(verticalCursor);
               else
               if (y>bounds.getHeight()-4) {
                    component.setCursor(verticalCursor);
               else {
                    component.setCursor(defaultCursor);
          private int getEdge(Point p) {
               int edge = 0;
               double x = p.getX();
               double y = p.getY();
               bounds = component.getBounds();
               if (x<3) {
                    edge = 1;
                    component.setCursor(horizontalCursor);
               else
               if (x>bounds.getWidth()-4) {
                    edge = 3;
                    component.setCursor(horizontalCursor);
               else
               if (y<3) {
                    edge = 2;
                    component.setCursor(verticalCursor);
               else
               if (y>bounds.getHeight()-4) {
                    edge = 4;
                    component.setCursor(verticalCursor);
               else {
                    component.setCursor(defaultCursor);
               return(edge);
          private void moveTo(int edge, Point dir) {
               Point dir2 = SwingUtilities.convertPoint(component, dir, component.getParent());
               bounds = component.getBounds();
               if (selectedEdge==1) {
                    component.setBounds((int)(dir2.getX()), (int)bounds.getY(), (int)(bounds.getWidth()-dir2.getX()+bounds.getX()), (int)bounds.getHeight());
               else
               if (selectedEdge==2) {
                    component.setBounds((int)bounds.getX(), (int)dir2.getY(), (int)bounds.getWidth(), (int)(bounds.getHeight()-dir2.getY()+1+bounds.getY()));
               else
               if (selectedEdge==3) {
                    component.setBounds((int)bounds.getX(), (int)bounds.getY(), (int)(dir.getX()+1), (int)bounds.getHeight());
               else
               if (selectedEdge==4) {
                    component.setBounds((int)bounds.getX(), (int)bounds.getY(), (int)bounds.getWidth(), (int)(dir.getY()+1));
          public void mouseExit(MouseEvent e) {
               component = null;
          public void mouseMoved(MouseEvent e) {
               getCursor(e.getPoint());
          public void mousePressed(MouseEvent e) {
               selectedEdge = getEdge(e.getPoint());
          public void mouseDragged(MouseEvent e) {
               if (selectedEdge == 0) {
                    return;
               moveTo(selectedEdge, e.getPoint());
          public void mouseReleased(MouseEvent e) {
               bounds = component.getBounds();
     public static final void setWindowsLF() {
          try {
               UIManager.setLookAndFeel(windowsUI);
          catch (Exception exc) {
               System.err.println("Error loading L&F: " + exc);
     public static void main(String[] args) {
          new MyFrame();
}Denis

Similar Messages

  • Dear all, Since a week ago I observe that when I press the button for my macbook air to start a nap, first the screen turns to black but I can see the arrow (pointer). And only after few seconds the arrow and the screen really shut down.

    Dear all, Since a week ago I observe that when I press the button for my macbook air to start a nap, first the screen turns to black (although I can see it is stil on) and I can see the arrow (pointer). Only after 15 seconds the arrow and the screen really shut down. It did not occur before, as a matter of fact it was instantaneous. What could be the problem?

    Note the time in your Mac's menu bar or some other accurate timepiece. Then, press the button to put the MBA to sleep. Wait until the cursor disappears and for all indications that the Mac appears to be asleep.
    Then, wake the Mac the way you normally wake it.
    Then, find the Console app - it is in your Utilities folder and looks like this
    You can find it by selecting Utilities from the Finder's Go menu.
    Open Console.
    If the log list column on the left is not already displayed, show the log list by selecting Show Log List from Console's View menu.
    Locate system.log in the list and select it. Many date and time-stamped entries will appear, hundreds of them, and you must find the entries relevant to your Mac's sleep problem. To do that type the words sleep cause in the Filter field at the upper left of the Console window:
    That will cause all log entries to be hidden, except for the entries containing those words. The last entry in the list will correspond to the last time you woke the Mac. It will have a time stamp in the form hh:mm:ss later than the time you noted in the Mac's menu bar prior to putting it to sleep.
    Make note of that exact time in hours, minutes, and seconds.
    Next: Clear the filter field by clicking the x in the above screenshot. It should then look like this:
    All previously displayed log entries will reappear.
    Next: Find the log entry that corresponds exactly to the time you noted in hours, minutes, and seconds containing the words "previous sleep cause". You may have to scroll up or down a considerable distance. When you find that line, select it and drag your cursor up and prior to the entry corresponding to the approximate time you noted before putting the Mac to sleep. This will result in a selection of perhaps 50 to 100 lines or so.
    One or more of them, along with their time stamps, may reveal the reason for the fifteen second delay.
    Copy and paste those log entries in a reply.
    Most of the entries will be cryptic but will contain information you might consider personal such as your Mac's name. If you do not want that information to appear, delete or obscure it when posting your reply. Leave enough information so that the entries can be deciphered.

  • I have all my songs in my iTunes library on the computer, but when I press the button to sync it, it doesn't put the songs on my iPhone. I have songs from my iPod on here too.  How do I get it to put the songs on my iPhone?

    I have all my songs in my iTunes library on the computer, but when I press the button to sync it, it doesn't put the songs on my iPhone. I have songs from my iPod on here too. How do I get it to put the songs on my iPhone?

    Have you selected the music to sync?

  • How can I get my ES2 loops to play correctly? I keep getting the message that the audio is not found when I drag the loops from my audio browser even though they play fine in the preview.

    How can I get my ES2 loops to play correctly? I keep getting the message that the audio is not found when I drag the loops from my audio browser even though they play fine in the preview.

    It's exactly as I stated. Whenever I try to drag these kinds of loops (ESX24 / software instrument loops? the ones marked in green with the white music note next to them) from the loop browser into the timeline a message comes up saying Audio Not Found for that loop.  And a new track is created automatically when loops are dragged into the timeline, so I'm not creating some other random / synth instrument track so I'm not sure  what the deal is... But perhaps I'll try creating a software instrument track first and then drag the loop into that track and see what happens - maybe there's something with the default settings that automatically creates audio tracks whenever loops are imported?

  • Something has happen my ipad air 2 it wont let me download or update any apps, when I press the button do update or download it looks like it is but it doesn't so if anyone knows what's going wind please help me

    Something has happen my ipad air 2 it wont let me download or update any apps, when I press the button do update or download it looks like it is but it doesn't so if anyone knows what's going wind please help me

    Do you have any restrictions for purchasing apps on the phone? Settings > General > Restrictions. If you have a Mac computer or another Apple device can you purchase things in the App Store using the same Apple ID on something else?

  • HT201209 Trying to redeem a free download for a song from KLove. When I get into my acct & try to redeem it, it just keeps bringing up the "sign-in" box. When I click the button on KLove's webpage to redeem it takes me to that page but does the same thing

    Trying to redeem a free download for a song from KLove. When I get into my acct & try to redeem it, it just keeps bringing up the "sign-in" box. When I click the button on KLove's webpage to redeem it takes me to that page but does the same thing

    Get the redeem code from the KLove page:
    http://www.klove.com/music/store.aspx
    ...and enter it into the Redeem Code box in the iTunes Store.
    You will have to sign in to your iTunes account.
    It worked fine here.  If that doesn't work for you, let us know what went wrong.

  • Hello Apple community... I am trying to combine multiple pdf's into one, using preview, but when I drag the additional pages they show up as separate documents.

    I am trying to combine multiple pdf's into one, using preview, but when I drag the additional pages they show up as separate documents.  When I drag the doc in a line would show up in the past, and if I went above th eline all was good.  It doesn't have a line, and no matter what I try I am having no success.
    Please help!

    According to an Apple Support article, unless there are unforeseen issues
    in file ownership or permissions, the combining of .PDFs should be simple.
    •OS X: Combining PDF documents using Preview - Apple Support
    There are a few variable and similar methods, some change with version
    of OS X in use; some vintage OS X may vary a little in the process...
    A variety of similar topics appear in search results, with terms:
    " mac os x combine pdf into one " such as this page shows...
    https://www.google.com/?gws_rd=ssl#q=mac%20os%20x%20combine%20pdf%20into%20one
    If the system is older than Mavericks there may be other methods to try.
    Suggestions are among linked results in the search. I see a few fair ones.
    •Here's another that offers generally similar suggestions...*  this info looks OK, extra links, do not:
    http://osxdaily.com/2014/06/27/how-to-join-multiple-pdf-files-into-a-single-pdf- document-in-mac-os-x/
    { note: avoid clicking on links to products in * this ^ page, since they likely will not help & may be adware prone }
    If you have a problem after trying other methods, based on an OS X
    your computer is running, post back with more exact information...
    Good luck & happy computing!
    edited

  • How can I stop iTunes from launching when I hit the button on my head phones

    On my MBP I have some head phones that have an inline mic with call answer button. The button on the head phones is fairly sensitive and I tend to bump it on accident often. When I hit the button iTunes is launched or if I have it open it starts music.
    Is there anyway to stop this from happening? This is driving me crazy!

    This problem has been an issue for a number of months and has led to frustration and public humiliation on countless occasions.
    When disconnecting from my Bose Soundlink at home (playing nothing) in order to taking my laptop on the run I open my laptop to find that the entire contents of my iTunes playlist is being belted in public. The subway, the library, my classroom is not the time or place for my laptop to make the decision to begin playing my music without permission. When at home and my Soundlink goes on standby I have nearly had a heart attack when my laptop spontaneously begins blaring my music when I wasn't even touching the thing.
    I love my Soundlink and I love my Mac but together the devices are causing no end of aggravation with no fix in sight. With each OS update I hope the problem is addressed but to no avail, it is getting tiresome that such a flaw isn't being fixed in a timely manner.

  • How do I convert an excel (2008) document to Numbers?  When I drag the Excel document onto the Numbers icon I get, "Import error.  The file format is invalid"

    How can I convert an Excel (2008) document to Numbers.  When I drag the Excel document onto the Numbers icon I get, "Import error.  The file format is invalid.

    That extension identifies the most current Excel file format, a format that can be opened by Numbers '09 (and by Numbers '08).
    A check with Numbers '09 shows it capable of opening .xlsx files when right-clicked (and Numbers chose as the application to open, when double-clicked (if Numbers has been set as the default application to open this type of file, or when dragged to the Numbers icon in the dock.
    I see two possibilities for Numbers inability to open it and the message you receive:
    1. The file is not actually an Excel file, and the sender has added the .xlsx extension to the filename manually.
    2. The file is an Excel file, but has somehow been corrupted.
    Have you tried opening the file with a different application? The open source Office suites, OpenOffice.org, LibreOffice and NeoOffice can also open .xlsx files. If they're unable to open this one, that would point toward the file itself being faulty. You can download these applications from their rspective websites, linked in the names above. All request a donation to support future development, but only Neo requires one to have been made recently, and that only for the current release (which won't be necessary to open .xlsx files).
    Regards,
    Barry

  • Hi, I need help my computer is not starting up at all, nothing happening when I press the button on/off. The battery charger is working as using it for another laptop. Not sure what to do. Anyone have any idea? many thanks

    Hi, I need help my computer is not starting up at all, nothing happening when I press the button on/off. The battery charger is working as using it for another laptop. Not sure what to do. Anyone have any idea? many thanks

    You could try a SMC reset:
    http://support.apple.com/kb/HT3964
    If no change, do what Allan advises.
    Caio.

  • I have some stuff i want to put on external hard drive when i drag the file or files it just puts them back

    i have some stuff i want to put on external hard drive when i drag the file or files it just puts them back

    Did you format the external disk on Windows? If you did this, probably the external disk is formatted in NTFS. OS X can't write on this format, so it's normal that it doesn't allow you to copy the files.
    In this case, you have three possibilities:
    1. Install Paragon NTFS. This program allows you to write on NTFS partitions.
    2. Copy the hard drive content to your internal disk and format the external disk with Mac OS Extended (Journaled). Do it only if you are going to use the external disk only with OS X.
    3. The same as the last one but formatting it with exFAT. This filesystem is compatible with Windows (XP, Vista, 7, 8) and OS X (10.6.5 and newer), so you can write and read files on external disks formatted with it on Windows and OS X, but you may have problems on Linux

  • MY iPhone 4 after  being updated to iOS7 will not download or update any apps. When i press the button on my device to download a new app the loading wheel appears then in a few seconds disappears. I dont get a terms and conditions screen or anything.

    MY iPhone 4 after  being updated to iOS7 will not download or update any apps. When i press the button on my device to download a new app the loading wheel appears then in a few seconds disappears. I dont get a terms and conditions screen or anything.

    Ive tried using wifi and 3G seperately. Ive tried restarting the phone. Ive tried restoring it from a backup but none of these have worked yet.

  • HT5622 Actually um trying to update new iOS 7 for my iphone 4 but when i press the button to install new software suddenly it appears why " iOs 7 failed verification because you are no longer connected to the internet " even my phone is already connected

    hie there Actually um trying to update new iOS 7 for my iphone 4 but when i press the button to install new software suddenly it appears why " iOs 7 failed verification because you are no longer connected to the internet " even my phone is already connected to net

    I tried powering down and re-starting the iPhone (as suggested at http://support.apple.com/kb/TS1275). This seems to have cleared the "...not connected to the Internet" problem, and iOS 6.1 is now in the process of downloading.
    Thanks very much for your feedback.

  • While using my Iphone 5, all of a sudden  black and white stripes where on the screen. I have tryed to turn the phone off and on again, but there are only the stripes. I can't use it, and nothing happens when I push the buttons. Can someone help me?

    While using my Iphone 5, all of a sudden  black and white stripes where on the screen. I have tryed to turn the phone off and on again, but there are only the stripes. I can't use it, and nothing happens when I push the buttons. Can someone help me?

    Try resetting your phone, hold the home and sleep/wake button down until the apple symbol comes up, then release. If that doesnt work, you may have damaged your phone and need to take it to an apple genius bar.

  • HT4075 when i drag the pdf file over other (in previewer) to merge it does not show green circle with plus one. what to do?

    when i drag the pdf file over other (in previewer) to merge it does not show green circle with plus one. what to do?

    Hello djensen1x,
    Could you please let me know what version of Acrobat are you using.
    Also, tell me your workflow of combining those PDF files?
    Please share the screenshot of the error message that you get.
    Hope to get your response.
    Regards,
    Anubha

Maybe you are looking for

  • MOPZ error while generating stack file for EREC upgrade

    I need to upgrade E-Recruitment 604 to 606. Currently we are on Netweaver 7.01 While adding this system in SOLMAN 7.1 SPS 05 in order to generate stack.xml file i manually selected this product system in product system editor and we had selected belo

  • How to attach file in emails

    Hi how can add attachment in emails

  • Nexus 1010 weird HA mechanism

    Hello guys, We are in the middle of the installation of 2 Nexus 1010 appliances. the two appliances have a failover mechanism where you have only one address for the cluster of appliance. I don't think that it is a good idea because the Vsm on the ap

  • Argentina - EBS with format AUZUG and UMSATZ (using MultiCash program)

    Hi, We are mapping the external transaction types while configuring EBS with AUZUG and UMSATZ format for our client. Does anybody have a document explaining the external transaction types in case of UMSATZ format, based on which we can define the pos

  • Is there any non-generic ADC/DAC in Multisim?

    I was wondering if Multisim carried any ADC's or DAC's that "real parts" and not just generic.  I have a circuit that I'm testing that has all "real IC's" except for the 8-bit ADC and 8-bit DAC which are generics.  The circuit works but I'm having pr