🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #7565

Re: I need a window which is modal

#7565
From: Rick Ingham
Date: Fri, 19 Jan 2001 06:53
297 lines
12095 bytes
This is a multi-part message in MIME format.

------=_NextPart_000_005A_01C081A1.7F5FDAC0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
This made me curious enough, I just had to figure it out.  Here is both a ModalWindow and ModalFrame that extend JWindow and JFrame.

Once you invoke show() or setVisible(true), the calling thread will block in a thread wait, so make sure you have an action on the
window that will invoke hide() or setVisible(false) to unblock and notify() the original thread.

"Rick Ingham" <rdingham@home.com> wrote in message news:948gf1$nu85@bornews.inprise.com...
> Sorry, I keep forgetting to bring home a code snippit.
>
> After I opened my big mouth, I realized mine is only a way to make the thread modal.  It pauses the thread calling
setVisible(true)
> or show() putting it into a thread wait().  Then an action event of some kind within the window will do a notify() to rekindle the
> sleeping thread.  That's only part of the modal behavior of a Dialog.
>
> It doesn't make the parent window unavailable or disabled, nor does it force the pseudo dialog window to come to the front when
any
> of the parent hierarchy is selected.
>
> I think this can be done by registering a FocusListener with each of the parent components and calling toFront if focusGained() is
> invoked while the modal window is visible.
>
> If you still need more, I'll try to put something like this together as an example for you.
> "Torsten Welches" <torsten.welches@who.cares.netcologne.de> wrote in message news:3a669326$1_2@dnews...
> >
> > Rick,
> >
> > interesting! Would you mind to post some info/snippets about it?
> > I guess you mean something like reimplementing Dialog -
> > including your own ui-event pump and so on.
> > I always thought this still wasn't possible without a true modal
> > (native/peer) window component.
> >
> > Torsten
> >
> > "Rick Ingham" <rdingham@home.com> wrote:
> > >I did this once using thread synchronization and an overridden show()/hide() that called wait()/notify().
> >
>

------=_NextPart_000_005A_01C081A1.7F5FDAC0
Content-Type: application/x-javascript;
	name="ModalWindowTest.java"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="ModalWindowTest.java"

package untitled1;=0A=
import java.awt.*;=0A=
import javax.swing.*;=0A=
=0A=
public class ModalWindowTest extends JWindow {=0A=
=0A=
  public ModalWindowTest() {=0A=
  }=0A=
  public static void main(String[] args) {=0A=
    ModalWindowTest modalWindowTest1 =3D new ModalWindowTest();=0A=
=0A=
    Window parent =3D new JFrame("Root Window JFrame");=0A=
    parent.setSize(200,200);=0A=
    parent.setLocation(0,0);=0A=
    parent.setVisible(true);=0A=
    for(int i=3D1; i < 4; i++ ) {=0A=
      parent =3D new ModalWindow(parent);=0A=
                   //ModalFrame(parent,"ModalFrame "+i);=0A=
      parent.setSize(200,200);=0A=
      parent.setLocation(20*i,20*i);=0A=
      parent.setVisible(true);=0A=
      System.out.println("Window "+i);=0A=
    }=0A=
  }=0A=
}
------=_NextPart_000_005A_01C081A1.7F5FDAC0
Content-Type: application/x-javascript;
	name="ModalWindow.java"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="ModalWindow.java"

package untitled1;=0A=
import java.awt.*;=0A=
import java.awt.event.*;=0A=
import javax.swing.*;=0A=
=0A=
public class ModalWindow extends JWindow {=0A=
  private Window parent =3D null;=0A=
  private boolean visible =3D false;=0A=
  private AncestorListener ancestorListener =3D null;=0A=
  private String myMutex =3D this.getClass().toString()+this.hashCode();=0A=
=0A=
  public ModalWindow() {=0A=
    super();=0A=
    init();=0A=
  }=0A=
  public ModalWindow( Window owner ) {=0A=
    super( owner );=0A=
    parent =3D owner;=0A=
    init();=0A=
  }=0A=
  public ModalWindow( Frame owner ) {=0A=
    super( owner );=0A=
    parent =3D owner;=0A=
    init();=0A=
  }=0A=
  private void init() {=0A=
    if( parent !=3D null ) {=0A=
      ancestorListener =3D new AncestorListener();=0A=
    }=0A=
    visible =3D false;=0A=
  }=0A=
  public void show() { setVisible(true); }=0A=
  public void hide() { setVisible(false); }=0A=
  public void setVisible( boolean makeVisible ) {=0A=
    if( makeVisible =3D=3D visible ) return;=0A=
    visible =3D makeVisible;=0A=
    if( makeVisible ) {=0A=
      registerAncestorListener();=0A=
      super.show();=0A=
    } else {=0A=
      unregisterAncestorListener();=0A=
      super.hide();=0A=
    }=0A=
System.out.println("setVisible("+makeVisible+") "+this.hashCode());=0A=
    synchronized( myMutex ) {=0A=
      try {=0A=
        if( visible ) myMutex.wait();=0A=
        else          myMutex.notifyAll();=0A=
      } catch (Exception e) {=0A=
        e.printStackTrace();=0A=
        System.out.println("Error in "+this.getClass()+".wait(): " + =
e.getMessage());=0A=
      }=0A=
    }=0A=
  }=0A=
  private void registerAncestorListener() {=0A=
    if( ancestorListener =3D=3D null ) return;=0A=
    Container parentContainer =3D parent;=0A=
    while( parentContainer !=3D null) {=0A=
System.out.println(" Registering listener "+this.hashCode()+"  on =
"+parentContainer.hashCode());=0A=
      parentContainer.addFocusListener(ancestorListener);=0A=
      if( parentContainer instanceof Window )=0A=
        ((Window) parentContainer).addWindowListener(ancestorListener);=0A=
      parentContainer =3D parentContainer.getParent();=0A=
    }=0A=
    Window[] windows =3D this.getOwnedWindows();=0A=
    for(int i =3D 0; i < windows.length; i++) {=0A=
      windows[i].addWindowListener(ancestorListener);=0A=
    }=0A=
  }=0A=
  private void unregisterAncestorListener() {=0A=
    if( ancestorListener =3D=3D null ) return;=0A=
    Container parentContainer =3D parent;=0A=
    while( parentContainer !=3D null) {=0A=
      parentContainer.removeFocusListener(ancestorListener);=0A=
      if( parentContainer instanceof Window )=0A=
        ((Window) =
parentContainer).removeWindowListener(ancestorListener);=0A=
      parentContainer =3D parentContainer.getParent();=0A=
    }=0A=
    Window[] windows =3D this.getOwnedWindows();=0A=
    for(int i =3D 0; i < windows.length; i++) {=0A=
      windows[i].removeWindowListener(ancestorListener);=0A=
    }=0A=
  }=0A=
=0A=
  private class AncestorListener extends WindowAdapter implements =
FocusListener {=0A=
    public void windowActivated( WindowEvent e) {=0A=
System.out.println("windowActivated "+this.hashCode()+"  --*--  =
"+e.getSource().hashCode());=0A=
      if( visible ) toFront();=0A=
    }=0A=
    public void focusLost( FocusEvent e ) {}=0A=
    public void focusGained( FocusEvent e ) {=0A=
System.out.println("focusGained "+this.hashCode()+"  --*--  =
"+e.getSource().hashCode());=0A=
      if( visible ) toFront();=0A=
    }=0A=
  }=0A=
=0A=
}
------=_NextPart_000_005A_01C081A1.7F5FDAC0
Content-Type: application/x-javascript;
	name="ModalFrame.java"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="ModalFrame.java"

package untitled1;=0A=
import java.awt.*;=0A=
import java.awt.event.*;=0A=
import javax.swing.*;=0A=
=0A=
public class ModalFrame extends JFrame {=0A=
  private Window parent =3D null;=0A=
  private boolean visible =3D false;=0A=
  private AncestorListener ancestorListener =3D null;=0A=
  private String myMutex =3D this.getClass().toString()+this.hashCode();=0A=
=0A=
  public ModalFrame() {=0A=
    super();=0A=
    init();=0A=
  }=0A=
  public ModalFrame( Frame owner, String title ) {=0A=
    super( title );=0A=
    parent =3D owner;=0A=
    init();=0A=
  }=0A=
  public ModalFrame( Window owner, String title ) {=0A=
    super( title );=0A=
    parent =3D owner;=0A=
    init();=0A=
  }=0A=
  private void init() {=0A=
    if( parent !=3D null ) {=0A=
      ancestorListener =3D new AncestorListener();=0A=
    }=0A=
    visible =3D false;=0A=
  }=0A=
  public void show() { setVisible(true); }=0A=
  public void hide() { setVisible(false); }=0A=
  public void setVisible( boolean makeVisible ) {=0A=
    if( makeVisible =3D=3D visible ) return;=0A=
    visible =3D makeVisible;=0A=
    if( makeVisible ) {=0A=
      registerAncestorListener();=0A=
      super.show();=0A=
    } else {=0A=
      unregisterAncestorListener();=0A=
      super.hide();=0A=
    }=0A=
System.out.println("setVisible("+makeVisible+") "+this.hashCode());=0A=
    synchronized( myMutex ) {=0A=
      try {=0A=
        if( visible ) myMutex.wait();=0A=
        else          myMutex.notifyAll();=0A=
      } catch (Exception e) {=0A=
        e.printStackTrace();=0A=
        System.out.println("Error in "+this.getClass()+".wait(): " + =
e.getMessage());=0A=
      }=0A=
    }=0A=
  }=0A=
  private void registerAncestorListener() {=0A=
    if( ancestorListener =3D=3D null ) return;=0A=
    Container parentContainer =3D parent;=0A=
    while( parentContainer !=3D null) {=0A=
    System.out.println(" Registering listener "+this.hashCode()+"  on =
"+parentContainer.hashCode());=0A=
      parentContainer.addFocusListener(ancestorListener);=0A=
      if( parentContainer instanceof Window )=0A=
        ((Window) parentContainer).addWindowListener(ancestorListener);=0A=
      parentContainer =3D parentContainer.getParent();=0A=
    }=0A=
    Frame[] frames =3D this.getFrames();=0A=
    for(int i =3D 0; i < frames.length; i++) {=0A=
      frames[i].addWindowListener(ancestorListener);=0A=
    }=0A=
  }=0A=
  private void unregisterAncestorListener() {=0A=
    if( ancestorListener =3D=3D null ) return;=0A=
    Container parentContainer =3D parent;=0A=
    while( parentContainer !=3D null) {=0A=
      parentContainer.removeFocusListener(ancestorListener);=0A=
      if( parentContainer instanceof Window )=0A=
        ((Window) =
parentContainer).removeWindowListener(ancestorListener);=0A=
      parentContainer =3D parentContainer.getParent();=0A=
    }=0A=
    Frame[] frames =3D this.getFrames();=0A=
    for(int i =3D 0; i < frames.length; i++) {=0A=
      frames[i].removeWindowListener(ancestorListener);=0A=
    }=0A=
  }=0A=
=0A=
  private class AncestorListener extends WindowAdapter implements =
FocusListener {=0A=
    public void windowActivated( WindowEvent e) {=0A=
    System.out.println("windowActivated "+this.hashCode()+"  --*--  =
"+e.getSource().hashCode());=0A=
      if( visible ) toFront();=0A=
    }=0A=
    public void focusLost( FocusEvent e ) {}=0A=
    public void focusGained( FocusEvent e ) {=0A=
    System.out.println("focusGained "+this.hashCode()+"  --*--  =
"+e.getSource().hashCode());=0A=
      if( visible ) toFront();=0A=
    }=0A=
  }=0A=
=0A=
}
------=_NextPart_000_005A_01C081A1.7F5FDAC0--
Technical Details
Message-ID: <948o7v$nnm5@bornews.inprise.com>
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!news-x2.support.nl!feed2.onemain.com!feed1.onemain.com!xfer13.netnews.com!netnews.com!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!bornews.borland.com!not-for-mail
References: <3a6369f0_1@dnews> <3a6564c7$1_2@dnews> <944b6j$pcp18@bornews.inprise.com> <3a669326$1_2@dnews> <948gf1$nu85@bornews.inprise.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,4562037466fa7b2c
X-Google-Attributes: gid111d45,public
X-Google-ArrivalTime: 2001-01-18 23:00:11 PST
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!news-x2.support.nl!feed2.onemain.com!feed1.onemain.com!xfer13.netnews.com!netnews.com!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!bornews.borland.com!not-for-mail
From: "Rick Ingham" <rdingham@home.com>
Newsgroups: borland.public.jbuilder.java.swing
Subject: Re: I need a window which is modal
Date: Thu, 18 Jan 2001 22:53:38 -0800
Organization: Another Netscape Collabra Server User
Lines: 299
Message-ID: <948o7v$nnm5@bornews.inprise.com>
References: <3a6369f0_1@dnews>  <3a6564c7$1_2@dnews>  <944b6j$pcp18@bornews.inprise.com>  <3a669326$1_2@dnews> <948gf1$nu85@bornews.inprise.com>
NNTP-Posting-Host: c191422-a.sttln1.wa.home.com
Mime-Version: 1.0
Content-Type: multipart/mixed;
 boundary="----=_NextPart_000_005A_01C081A1.7F5FDAC0"
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4133.2400
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400