🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.java
Article #361

Re: powrot z dialogu

#361
From: Adam Lejman
Date: Fri, 28 Feb 1997 08:26
142 lines
5144 bytes
Nitecki wrote:
> 
> Probuje zaimplementowac w javie obiekt zachowujacy sie jak MessageBox np. w C++.
> Istnieje przy tym nastepujacy problem jezeli chcemy uzyskac powrot do funkcji wywolujacej
> taki MessageBox dopiero po jego zamknieciu to mamy problem. Na uzywanej przeze mnie
> platformie OS/2 Warp sterowanie z instrukcji show() wraca zawsze od razu nie czekajac
> na zamkiniecie dialogu nawet gdy ten jest modalny. Na uzywanej przeze mnie maszynie
> UNIXowej ibm.uci.agh.edu.pl dzieje sie inaczej sterowanie wraca dopiero po zamknieciu
> tego dialogu.

Nie ty pierwszy i zapewne nie ostatni natknales sie na problem z modal
dialogami. Podobno, tak w javie zalozono, ze po wywolaniu dialogu
funkcja show(), watek go wywolujacy biegnie dalej, a dialog dostaje
drugi watek. Bo niby po co blokowac watek glowny, ktory w tym czasie
moze robic cos innego. Na tym polega smaczek javy. 
Jak obsluzysz dialog to po prostu musisz wolajacego o tym poinformowac.
Blokowanie watku glownego po wywolaniu dialogu jest podobno niedorobka
maszyny wirtualnej na danym systemie. Wyjatek tu stanowi FileDialog,
ktory blokuje watek wywolujacy na wszystkich platformach.

Jednym ze sposobow (moj ulubiony i skuteczny jak do tej pory) jest
wysylanie eventu o zakonczeniu dialogu do rodzica. Rodzic po otrzymaniu
wiadomosci, ze dialog zostal obsluzony, moze wykorzystac pobrane przez
niego dane. Ale nic tak nie wyjasnia rozwiazania problemu jak przyklad.
Przyznam, ze pomysl zaczerpnalem z jednego postingow z
compl.lang.java.misc autorstwa Davida Linkera
<dtlinker@u.washington.edu>:

Adam Lejman

<--------------------------------------------------------------------->
|       _/_/    _/    Adam Lejman,    Warsaw University of Technology |
|     /    _/  _/     Institute of Microelectronics & Optoelectronics |
|   _/_/_/_/  _/      ul. Koszykowa 75, 00-662 Warszawa, POLAND       |
|  _/    _/  _/       tel: (+48)-22-660.72.07  fax: (+48)-22-25.82.03 |
| _/    _/  _/_/_/_/  http://neutron.elka.pw.edu.pl/~alejman/         |
<--------------------------------------------------------------------->


import java.awt.*;

public class testDialog extends Dialog
{
	Button theButton;
	TextField theText;
	Frame theParent;
        
	public testDialog(Frame parent, String title)
	{
		super(parent, title, true); // Call inherited constructor
        theText = new TextField(20);
        this.setLayout(new FlowLayout());
        this.add(theText);
        theButton = new Button("OK");
        this.add(theButton);
        this.pack();
        theParent = parent; // Save who called us
	}
        
	public String gettheText()
	{
		return theText.getText(); // Return the string value
	}
        
	public boolean wasDialogButton(Event event) // Was it our button?
	{
		if (event.target == theButton)
			return true;
		else
			return false;
	}
        
	public boolean handleEvent (Event event)
	{
		if ((event.id == Event.ACTION_EVENT) && (event.target == theButton))
		{
			this.hide();
			theParent.handleEvent(event); // Force parent to handle event
		}
		return false;
	}    
}


import java.awt.*;

public class aTest extends Frame{

	static Button Quit, Show;
    static testDialog d;
    static TextField theText;
        
    public aTest(String title)
    {
    	super (title);
        Quit = new Button("Quit");
        Show = new Button("Show Dialog");
        theText = new TextField(20);
        setLayout(new FlowLayout());
        add(theText);
        add(Show);
        add(Quit);
        d = new testDialog(this,"Open a file");
        this.pack();
        this.show();
    }
        
    public boolean handleEvent (Event event)
    {
    	if (event.id == Event.ACTION_EVENT)
        {
        	if (event.target == Quit)
            {
        	    hide();
                return true;
        	} else
        	if (event.target == Show)
       		{
    	    	d.pack();
    	        d.show();
    	        return true;
    	    }
    	    else
    	    if (d.wasDialogButton(event)) // We got close event from the
dialog
    	    {
    	    	System.out.println("The value was " + d.gettheText()); //Debug
info
    	        theText.setText(d.gettheText()); //Set the field
    	        return true;
    	    }
     	    return false;
        } else 
        	return false;
	}
        
    public static void main(String args[]) 
    {
    	Frame f = new aTest("Test of Dialogs");
	}

}
Technical Details
Message-ID: <331696C5.6E48@imio.pw.edu.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!news.icm.edu.pl!gemini.coi.pw.edu.pl!news-adm@io.coi.pw.edu.pl
References: <5f4gjc$ddl@info.cyf-kr.edu.pl>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!news.icm.edu.pl!gemini.coi.pw.edu.pl!news-adm@io.coi.pw.edu.pl
From: Adam Lejman <al@imio.pw.edu.pl>
Newsgroups: pl.comp.lang.java
Subject: Re: powrot z dialogu
Date: Fri, 28 Feb 1997 09:26:50 +0100
Organization: Warsaw University of Technology, Poland
Lines: 142
Message-ID: <331696C5.6E48@imio.pw.edu.pl>
References: <5f4gjc$ddl@info.cyf-kr.edu.pl>
Reply-To: al@imio.pw.edu.pl
NNTP-Posting-Host: alermik.imio.pw.edu.pl
Mime-Version: 1.0
X-Mailer: Mozilla 3.01 (Macintosh; I; PPC)
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit