Link to home
Start Free TrialLog in
Avatar of shannon_cogan
shannon_cogan

asked on

Popup Window

Greetings Lad,
I am new to the Java world and would like to have an example
of a Java Popup Window.
I have seen these on the Internet, as an applet before.
They all look the same, white with an Java icon.

So, for 200 points, I would like to have a Java applet (J++) example that pops up a java window
that contains any text that should be inserted into param.
Now, to make myself clear.
I will award these points only if you have provided a good example that pops up a java window
and has param's to give text to the window.
I am using J++.

Thank you.
Avatar of froderik
froderik

Hi,
If I remember correctly, there is a Javascript method that does that exact thing. Much easier to implement. Just place the method call somewhere inside the Javascript block in the header. If you want to see examples of this I highly recommend the very good Javascript tutorial at Webmonkey: www.hotwired.com/webmonkey

Fredrik
try this, taken from teach java in 21 days

however this is written is Sun Java
if you like it i will submit as an answer


 public class GUI extends java.applet.Applet {

     Frame window;



     public void init() {

         add(new Button("Open Window"));

         add(new Button("Close Window"));



         window = new MyFrame("A Popup Window");

         window.resize(150, 150);

         window.show();

     }



     public boolean action(Event evt, Object arg) {

        if (evt.target instanceof Button) {

         String label = (String)arg;

            if (label.equals("Open Window")) {

                if (!window.isShowing())

                window.show();

            }

           else if (label == "Close Window") {

              if (window.isShowing())

                  window.hide();


         return true;

         }

        else return false;

     }

 }

 

 class MyFrame extends Frame {

     Label l;

    MyFrame(String title) {

    super(title);

     setLayout(new GridLayout(1, 1));

    l = new Label("This is a Window", Label.CENTER);

    add(l);

 }
I have written a Applet that does exactly what you want. I have used Sun java but for this examplemay be it dosent make a big difference.

The ex displays the string retrieved from the param on the applet's graphics region and also on the frame's label. Let me know if u got any problems.


The HTML file
===========================
HTML>
<HEAD>
<!-- Generated by Kawa IDE -->
<TITLE>Applet title</TITLE>
</HEAD>
<BODY>
<H1>First Heading</H1>
<HR>
<APPLET CODE="TestApplet" WIDTH=300 HEIGHT=300>
<PARAM name=param1 value="this is a testing param1">
</APPLET>
<HR>
</BODY>
</HTML>
=========================================
The Applet file TestApplet.java
=========================================
import java.applet.Applet;
import java.awt.*;

public class TestApplet extends Applet
{
      Frame fm;
      Label lb;
      String paramstr;
      public TestApplet()
      {
            fm = new Frame("Test window");
            paramstr = new String();
            lb = new Label();
      }
      public void init()
      {
            
      }      
      public void start()
      {
            paramstr = getParameter("param1");
            lb.setText(paramstr);
            fm.add(lb);
            fm.setSize(400,300);
            fm.setVisible(true);            
      }
      public void paint(Graphics g)
      {
            g.drawString(paramstr,10,20);
      }
}
=========================================



Tell me clearly what you want. Tell me what more fuctionality u require.
So, Shannon Cogan, I have written the code for your solution in VJ++. Try this out.

1. HTML code for "page1.htm".

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 98">
<META HTTP-EQUIV="Content-Type" content="text/html">
<TITLE>Document Title</TITLE>
</HEAD>
<BODY><!-- Insert HTML here -->
<APPLET code=Applet1.class
codeBase="file://C:\My Documents\Visual Studio Projects\Project1\" height=200
name=Applet1 width=320>
      <PARAM NAME="foreground" VALUE="FFFFFF">
      <PARAM NAME="background" VALUE="008080">
      <PARAM NAME="label" VALUE="This string was passed from the HTML host.">
      </APPLET>
</BODY>
</HTML>

2. Source code file Applet1.java.

// Applet1.java

import java.awt.*;
import java.applet.*;

/**
 * This class reads PARAM tags from its HTML host page and sets
 * the color and label properties of the applet. Program execution
 * begins with the init() method.
 */
public class Applet1 extends Applet
{
      String labelValue;
      String backgroundValue;
      String foregroundValue;
            
      /**
       * The entry point for the applet.
       */
      public void init()
      {
            initForm();

            usePageParams();

            // TODO: Add any constructor code after initForm call.
      }

      private      final String labelParam = "label";
      private      final String backgroundParam = "background";
      private      final String foregroundParam = "foreground";

      /**
       * Reads parameters from the applet's HTML host and sets applet
       * properties.
       */
      private void usePageParams()
      {
            final String defaultLabel = "Default label";
            final String defaultBackground = "C0C0C0";
            final String defaultForeground = "000000";

            /**
             * Read the <PARAM NAME="label" VALUE="some string">,
             * <PARAM NAME="background" VALUE="rrggbb">,
             * and <PARAM NAME="foreground" VALUE="rrggbb"> tags from
             * the applet's HTML host.
             */
            labelValue = getParameter(labelParam);
            backgroundValue = getParameter(backgroundParam);
            foregroundValue = getParameter(foregroundParam);

            if ((labelValue == null) || (backgroundValue == null) ||
                  (foregroundValue == null))
            {
                  /**
                   * There was something wrong with the HTML host tags.
                   * Generate default values.
                   */
                  labelValue = defaultLabel;
                  backgroundValue = defaultBackground;
                  foregroundValue = defaultForeground;
            }

            /**
             * Set the applet's string label, background color, and
             * foreground colors.
             */
            label1.setText(labelValue);
            label1.setBackground(stringToColor(backgroundValue));
            label1.setForeground(stringToColor(foregroundValue));
            this.setBackground(stringToColor(backgroundValue));
            this.setForeground(stringToColor(foregroundValue));
      }

      /**
       * Converts a string formatted as "rrggbb" to an awt.Color object
       */
      private Color stringToColor(String paramValue)
      {
            int red;
            int green;
            int blue;

            red = (Integer.decode("0x" + paramValue.substring(0,2))).intValue();
            green = (Integer.decode("0x" + paramValue.substring(2,4))).intValue();
            blue = (Integer.decode("0x" + paramValue.substring(4,6))).intValue();

            return new Color(red,green,blue);
      }

      /**
       * External interface used by design tools to show properties of an applet.
       */
      public String[][] getParameterInfo()
      {
            String[][] info =
            {
                  { labelParam, "String", "Label string to be displayed" },
                  { backgroundParam, "String", "Background color, format \"rrggbb\"" },
                  { foregroundParam, "String", "Foreground color, format \"rrggbb\"" },
            };
            return info;
      }

      Label label1 = new Label();

      /**
       * Intializes values for the applet and its components
       */
      void initForm()
      {
            labelValue=getParameter(labelParam);
            PopupWindow popUp = new PopupWindow(new Frame(), labelValue);
            System.out.println(labelValue);
            this.setBackground(Color.lightGray);
            this.setForeground(Color.black);
            label1.setText("label1");
            this.setLayout(new BorderLayout());
            this.add("North",label1);
      }
}

class PopupWindow extends Window
{
      PopupWindow(Frame frame, String param)
      {
            super(frame);
            IconText boxx = new IconText();
        boxx.setText(param);
            //setLayout(new BorderLayout());
        add(boxx);
            setSize(500,200);
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((d.width - getSize().width) / 2, (d.height - getSize().height) / 2);
            setVisible(true);
      }
}

class IconText extends Canvas {
  String msg = "";
  int descent;

  void setText(String msg) {
    this.msg = msg;
    Font f1 = new Font("Dialog" ,Font.PLAIN, 12);
    this.setFont(f1);
    this.setBackground(new Color(255,255,204)); // FFFFCC
    Graphics myG = this.getGraphics();
    FontMetrics fm = getFontMetrics(f1);
    int wid = fm.stringWidth(msg)+4;
    int hei = 2*fm.getHeight()+5;
    descent = fm.getDescent()+10;
    setSize(500, 200);
  }

  public void paint(Graphics g) {
    g.drawString(msg, 2, descent);
  }
}

In your Applet (here Applet1) you will have (had) to say:

PopupWindow popUp = new PopupWindow(new Frame(), myString);

That's all. myString is your parameter. Notice that you do not have a title bar, minimize button, maximize button or cancel button as all other windows have.

Thanks.
Aziz.
Avatar of shannon_cogan

ASKER

So AZIZ,
ERROR : (
in the following...
/**
   * Reads parameters from the applet's HTML host and sets applet
   * properties.
   */
   private void usePageParams()
   {
/*41*/   final String defaultLabel = "Default label";
/*42*/   final String defaultBackground = "C0C0C0";
/*43*/   final String defaultForeground = "000000";


--------------------Configuration: applet1 - Java Virtual Machine Debug--------------------
Compiling...
Microsoft (R) Visual J++ Compiler Version 1.01.7022
Copyright (C) Microsoft Corp 1996. All rights reserved.
C:\IDC Workshop\Products\Java\Learn\tmp2\applet1.java(41,10) : error J0025: Expected statement
C:\IDC Workshop\Products\Java\Learn\tmp2\applet1.java(42,10) : error J0025: Expected statement
C:\IDC Workshop\Products\Java\Learn\tmp2\applet1.java(43,10) : error J0025: Expected statement
Error executing jvc.exe.




applet1 - 3 error(s), 0 warning(s)

Aziz's code compiles just fine with the SUN's JDK. Drop the "final" and tell me if it works.
Hi Shannon,
            Just take out the final and try it out. I don't know why it did not compile.

Aziz

Hi Shannon ,

           If you have tried my program , it does bring up a popup window which takes values from the PARAM . I fail to understand why you rejected the answer and it would be decent if you leave a comment, atleast in regards to the time I had to spend to make it work.

Thanks
Aziz
ASKER CERTIFIED SOLUTION
Avatar of dryang
dryang

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial