Howto: Simulate a close (X) button in an applications toolbar?

  • Follow


In my multiply document application, I wish to have a right-most toolbar 
button act as the close button for the currently active document/view. 
Is there any facility in the Swing API that allows me to come up wich 
such a button which is platform-dependent. I prefer not to make up my 
own customized X button which.

Thanks in advance,
Casper
0
Reply Casper 9/16/2005 5:51:01 PM

"Casper" <casper@jbr.dk> wrote in message 
news:9CDWe.36155$Rd5.1121943@weber.videotron.net...
> In my multiply document application, I wish to have a right-most toolbar 
> button act as the close button for the currently active document/view. Is 
> there any facility in the Swing API that allows me to come up wich such a 
> button which is platform-dependent. I prefer not to make up my own 
> customized X button which.

    It's not clear what your problem is.

    You don't want to draw your own X?

    You don't know how to close the currently active document/view?

    Something else?

    - Oliver 


0
Reply Oliver 9/16/2005 5:53:45 PM


On Fri, 16 Sep 2005 19:51:01 +0200, Casper <casper@jbr.dk> wrote or
quoted :

>In my multiply document application, I wish to have a right-most toolbar 
>button act as the close button for the currently active document/view. 
>Is there any facility in the Swing API that allows me to come up wich 
>such a button which is platform-dependent. I prefer not to make up my 
>own customized X button which.

Frame.dispose() or Frame.setVisible( false ) is the sort of thing that
typically happens on close. All you need is a button that calls one of
those two.

If you want to close something internal, you can have the button to a
setVisible( false ) on some components followed by
invalidate/validate.  Alternatively you can remove them from the
container and do the validate, depending on whether you plan to bring
them back to life again.

-- 
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
0
Reply Roedy 9/16/2005 7:03:25 PM

Maybe I was not being clear enough in my original post. You know that 
little close button on the top-right of a JFrame? I wish to draw a 
platform/L&F dependent "close widget". I do not merely want a JButton 
with an X, since this would be platform/L&F independent.

Regards,
Casper
0
Reply Casper 9/16/2005 11:02:19 PM

On Sat, 17 Sep 2005 01:02:19 +0200, Casper <casper@jbr.dk> wrote or
quoted :

>Maybe I was not being clear enough in my original post. You know that 
>little close button on the top-right of a JFrame? I wish to draw a 
>platform/L&F dependent "close widget". I do not merely want a JButton 
>with an X, since this would be platform/L&F independent.

In that case, you are going to have to peek inside the code for JFrame
to see how it works.  I suspect you be in trouble. Likely the L&F
draws the entire bar with its own devices.  There is not even
necessarily an X icon.  I suspect an L&F is possible where for example
you triple click the menu bar to close.

You could camera-cut the icon from various platform L&Fs and use them
in a JButton, selecting the image by L&F.

The obvious way to solve your problem is to put the thing you want to
close in something that comes with an X built-in.. e.g. JFrame.  Can
JDialogs or JInternal frames have close boxes?
-- 
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
0
Reply Roedy 9/17/2005 2:01:03 AM

Roedy Green wrote:
> In that case, you are going to have to peek inside the code for JFrame
> to see how it works.  I suspect you be in trouble. Likely the L&F
> draws the entire bar with its own devices.  There is not even
> necessarily an X icon.  I suspect an L&F is possible where for example
> you triple click the menu bar to close.
> 
> You could camera-cut the icon from various platform L&Fs and use them
> in a JButton, selecting the image by L&F.

Thought of that too, but seems more like a workaround/hack than real 
future-proof solution.

> The obvious way to solve your problem is to put the thing you want to
> close in something that comes with an X built-in.. e.g. JFrame.  Can
> JDialogs or JInternal frames have close boxes?
However, having such a window would also require a visible beveled frame 
along with a caption/title bar, which does not fit well within an 
existing application window.

It took a while, but it seems like the WindowsIconFactory of the package 
com.sun.java.swing.plat.windows will help me out here. Among other 
things, it has a .createFrameCloseIcon() method.

Casper
0
Reply Casper 9/17/2005 4:18:44 PM

On Sat, 17 Sep 2005 18:18:44 +0200, Casper <casper@jbr.dk> wrote or
quoted :

> Among other 
>things, it has a .createFrameCloseIcon() method.

Check that either such an icon must be defined for all L&Fs or provide
a default icon in case there isn't one.
-- 
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
0
Reply Roedy 9/17/2005 5:20:29 PM

Casper wrote:

<snip>

> 
> It took a while, but it seems like the WindowsIconFactory of the package 
> com.sun.java.swing.plat.windows will help me out here. Among other 
> things, it has a .createFrameCloseIcon() method.

It won't work very well on other platforms, since that is a Windows LAF 
class.

Swing already provides you with what you need.  The UIManager class can 
give you the platform specific 'Close' icon.

-----------
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JToolBar;
import javax.swing.UIManager;

public class CloseDemo {

     CloseDemo() {
         Icon closeIcon = null;
         closeIcon = UIManager.getIcon("InternalFrame.closeIcon");
         JFrame f = new JFrame();
         f.setLayout(new BorderLayout());
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         JInternalFrame jifA =
                  new JInternalFrame("Document A",true,true);
         JInternalFrame jifB =
                  new JInternalFrame("Document B",true,true);
         jifA.setSize(300,300);
         jifB.setSize(400,400);
         final JDesktopPane jdp = new JDesktopPane();
         jdp.add(jifA);
         jdp.add(jifB);
         f.add(jdp, BorderLayout.CENTER);
         JToolBar tb = new JToolBar();
         JButton closeButton = new JButton(closeIcon);
         closeButton.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent ae) {
                 JInternalFrame jif = jdp.getSelectedFrame();
                 if (jif != null) {
                     jdp.getDesktopManager().closeFrame(jif);
                 }
             }
         });
         tb.add(closeButton);
         f.add(tb, BorderLayout.NORTH);
         f.setSize(800,600);
         jifA.setVisible(true);
         jifB.setVisible(true);
         f.setVisible(true);
     }
     public static void main(String[] args) {
         new CloseDemo();
     }
}
---------------

Jim S.



-- 
Remove my extraneous mandibular appendages to reply via email.
0
Reply Jim 9/18/2005 1:56:35 PM

Casper <casper@jbr.dk> writes:

> 
> Roedy Green wrote:

> > You could camera-cut the icon from various platform L&Fs and use them
> > in a JButton, selecting the image by L&F.
> 
> Thought of that too, but seems more like a workaround/hack than real 
> future-proof solution.

Well, there is also the additional problem of figuring out where to put
this button.  In the Macintosh Aqua (OS X) L&F, the close icon appears
at the upper LEFT of the window title bar, not on the right.  So putting
the little red round icon on the right is more likely to confuse someone
than help them.

-- 
Thomas A. Russ,  USC/Information Sciences Institute

0
Reply tar 9/19/2005 9:46:01 PM

Call setUndecorated(false) on your frame. This should remove the title
bars.

Now add your own title bar with the close buttons

-Jessu
http://www.feedfeeds.com

0
Reply jessu 9/20/2005 9:18:56 AM

9 Replies
346 Views

(page loaded in 0.009 seconds)

Similiar Articles:













7/26/2012 5:41:51 PM


Reply: