Article View: comp.lang.java.programmer
Article #434Re: Pop up a message box in applet
From: Joseph Ramsey
Date: Mon, 29 Jun 1998 00:00
Date: Mon, 29 Jun 1998 00:00
114 lines
4337 bytes
4337 bytes
morris_newsgroup@yahoo.com wrote:
>
> I have an applet which accepts input from users through a GUI. I'm trying to
> pop up a message box with only an OK button when users make an error. Is
> this doable in applet or I can only do this in an application instead. I
> think this is a general java gui question. By the way, I'm using JBuilder2
> and found that when I dropped a message component onto my applet. I can't
> find a frame which the message component can use. In other words, there was
> nothing in the frame property of the message component. I tried to create a
> frame inside the applet source code and it seemed to have some security
> errors when I ran the applet in the browser.
>
> Does anyone know anything about this? It may not be just a JBuilder2
> question. In general, I would like to know if it's possible that I can pop up
> a message box with either just a message or a text box which users can enter
> something in an applet.
>
> Sample code would be helpful if this is possible.
Here's a message window with an OK button I wrote for a app I'm
working on. I used JDK 1.1.5 for it on Windows. You have to pass
it the frame of the applet you're in, so you have to find this. You
probably know how to do that but I tacked code on the end just in
case. (You have to put this code in the calling class. Define a
class-level variable frame of type Frame. Or rewrite the thing to
suit your quirks instead of mine.)
/*
* DataWindowSpreadsheet.java 0.4 98/04/20
*
* Copyright (c) 1998 Joseph D. Ramsey. All Rights Reserved.
*/
import java.awt.*;
import java.awt.event.*;
/**
* This class generates a popup message dialog which
* displays a simple message and then disappears when
* the user clicks 'OK' (or types 'Enter' or clicks
* the window close tab).
*/
public class MessageDialog extends Dialog
{
String message;
Button okButton = new Button("OK");
Label messageLabel;
public MessageDialog(Frame frame, String message)
{
super(frame, "Message:", true);
this.message = message;
messageLabel = new Label(message);
setLayout(new FlowLayout());
add(messageLabel);
add(okButton);
pack();
setResizable(false);
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
dispose();
}
});
okButton.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
dispose();
}
}
});
this.addWindowListener (new WindowAdapter() {
public void windowClosing(WindowEvent event) {
dispose();
}
});
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
int height = this.getSize().width;
setLocation(screenSize.width / 2 - height / 2,
screenSize.height / 2);
show();
}
}
METHOD TO GET APPLET FRAME:
/**
* This method returns the frame which ultimately contains
* this panel. One needs to know which frame this is
* in order to display dialogs and charts in separate
* Dialog windows.
*/
protected Frame getFrame()
{
if (this.frame == null)
{
Component c = this;
Frame frame = null;
while ( (c = c.getParent() ) != null)
{
if (c instanceof Frame)
frame = (Frame) c;
}
this.frame = frame;
}
return this.frame;
}
Technical Details
Message-ID:
<3597C8C8.B1D0FB52@ucsd.edu>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<6n6nh9$bd1$1@nnrp1.dejanews.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fa3e3,afddcc2748cf26d8 X-Google-Attributes: gidfa3e3,public X-Google-Thread: f74b3,afddcc2748cf26d8 X-Google-Attributes: gidf74b3,public X-Google-Thread: 10fd6d,afddcc2748cf26d8 X-Google-Attributes: gid10fd6d,public X-Google-Thread: 10a146,afddcc2748cf26d8 X-Google-Attributes: gid10a146,public From: Joseph Ramsey <jramsey@ucsd.edu> Subject: Re: Pop up a message box in applet Date: 1998/06/29 Message-ID: <3597C8C8.B1D0FB52@ucsd.edu> X-Deja-AN: 367143127 Content-Transfer-Encoding: 7bit References: <6n6nh9$bd1$1@nnrp1.dejanews.com> To: morris_newsgroup@yahoo.com Content-Type: text/plain; charset=us-ascii Organization: University of Califonia, San Diego Mime-Version: 1.0 Newsgroups: comp.lang.java.programmer,borland.public.jbuilder.java.language,comp.lang.java.gui,borland.public.jbuilder.ide