Solved

Popup Window

Posted on 1998-05-25
10
428 Views
Last Modified: 2010-04-16
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.
0
Comment
Question by:shannon_cogan
10 Comments
 
LVL 1

Expert Comment

by:froderik
ID: 1221435
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
0
 

Expert Comment

by:sdaxm11
ID: 1221436
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);

 }
0
 

Expert Comment

by:anandkrish
ID: 1221437
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);
      }
}
=========================================



0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:anandkrish
ID: 1221438
Tell me clearly what you want. Tell me what more fuctionality u require.
0
 
LVL 2

Expert Comment

by:aziz061097
ID: 1221439
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.
0
 

Author Comment

by:shannon_cogan
ID: 1221440
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)

0
 
LVL 5

Expert Comment

by:fontaine
ID: 1221441
Aziz's code compiles just fine with the SUN's JDK. Drop the "final" and tell me if it works.
0
 
LVL 2

Expert Comment

by:aziz061097
ID: 1221442
Hi Shannon,
            Just take out the final and try it out. I don't know why it did not compile.

Aziz

0
 
LVL 2

Expert Comment

by:aziz061097
ID: 1221443
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
0
 
LVL 1

Accepted Solution

by:
dryang earned 200 total points
ID: 1221444
These are three files done in JDK 1.1.6. If you need the class files you can request from me. Do take note of the event handlers and the method putText( ) in the popFrame class

//the applet

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

public final class AppPopoUp extends Applet
      implements ActionListener
{
      Button intPop;
      Button sendText;
      
      TextField txf;
      
      popFrame PF;
      
      public void init()
      {
            intPop = new Button("Initiate Pop up");
            sendText = new Button("Send Text");
            txf = new TextField("Edit this to add your own text");
            
            intPop.addActionListener(this);
            
            add(intPop);
            add(sendText);
            add(txf);

            }
      
      public void actionPerformed(ActionEvent AE)
      {
            if(AE.getSource() == intPop)
            {
                  intPop.removeActionListener(this);
                  PF = new popFrame();
                  sendText.addActionListener(this);
              txf.addActionListener(this);
                  }
            else if(AE.getSource() == sendText || AE.getSource() == txf)
                  {
                        PF.putText(txf.getText());
                        }
            }
      
      }


////////////////////////////

///////// the Pop up Frame
import java.awt.*;
import java.awt.event.*;

public class popFrame extends Frame implements WindowListener
{
      Label Content;
      Panel pnl;
      
      popFrame()
      {
            super("Popup Frame");
            
            setSize(300,100);
            addWindowListener(this);
            
            pnl = new Panel(null);
            pnl.setBounds(new Rectangle(250,35));
            
            Content = new Label();
            Content.setBounds(new Rectangle(200,25));
            Content.setLocation(0,0);
            
            pnl.add(Content);
            
            add(pnl);
            
            setVisible(true);
            validate();
      }
      
      public void putText(String displaytext)
      {
            Content.setText(displaytext);
            }

      public void windowActivated(WindowEvent WE){}
      public void windowClosed(WindowEvent WE){}
      public void windowClosing(WindowEvent WE){dispose();}
      public void windowDeactivated(WindowEvent WE){}
      public void windowDeiconified(WindowEvent WE){}
      public void windowIconified(WindowEvent WE){}
      public void windowOpened(WindowEvent WE){}
      
      }

////////////////////The HTM file
<HTML>
<HEAD>
<!-- Generated by Kawa IDE -->
<TITLE>Applet title</TITLE>
</HEAD>
<BODY>
<H1>First Heading</H1>
<HR>
<APPLET CODE=AppPopoUp.class WIDTH=300 HEIGHT=300></APPLET>
<HR>
</BODY>
</HTML>
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mysql jsp example issue 32 68
Cisco ASA: Java web start no go, asdm launcher no go 3 56
jmeter usage 4 31
maven not picking latest jar instead picking old jar from .m2 12 42
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question