Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4079053

An extended Dialog class cannot correctly set value given

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P4 P4
    • None
    • 1.1.2
    • client-libs
    • x86
    • windows_nt



      Name: joT67522 Date: 09/15/97


      An extended Dialog class cannot correctly set value given TextCompoent.

      (** Step 1 **) Run: java ChoiceDialog
      (** Step 2 **) Source codes

      An extened Dialog, myDialog, implements ActionListener cannot
      correctly set value to a TextComponent passed in at the class
      construction.

      The value at example 1 of the item.getText()
      didn't return correct answer all the time.

      Example 2 use method to catch value change, it failed too.
      BUT if I reuse the object, the value changed correctly.

      //
      // EXAMPLE 1
      //
      import java.awt.*;
      import java.awt.event.*;

      public class AcceptDialog implements ActionListener
      {
         public static void main(String args[]) {
            Frame f = new Frame();
            TextField item = new TextField();
            f.setTitle("Function Counting Analyze");
            f.setSize(300,300);
            f.pack();
            f.show();
            AcceptDialog ad = new AcceptDialog( f, "Input your choice"
                        , "What is the answer of\nFlag is flying in the deep
      sea\n?????\n", item );
            System.out.println("Answer is " + item.getText() );
            System.exit(0);
         } // main

         protected Frame f;
         protected Dialog dl;
         protected MultiLineLabel mLabel;
         protected Button acceptButton, cancelButton;
         protected boolean acceptAndExit;
         protected TextField acceptField;
         protected StatusConsol sc;

         public AcceptDialog( Frame parent, String title, String message, TextField af
      ){
            this(parent, title, message, af, (StatusConsol)null, true );
         }
         public AcceptDialog( Frame parent, String title, String message
                            , TextField af, StatusConsol sc ) {
            this(parent, title, message, af, sc, true );
         }
         public AcceptDialog( Frame parent, String title, String message, TextField af
                            , StatusConsol sc, boolean acceptAndExit ){
            // create the window
            dl = new Dialog(f = parent, title, true);
            this.sc = sc;
            this.acceptAndExit = acceptAndExit;

            dl.setLayout(new BorderLayout());
            mLabel = new MultiLineLabel(message);
            dl.add( mLabel, "Center");

            Panel fp = new Panel(new GridLayout(2,1));
            Panel p = new Panel();
            acceptField = af;
            acceptField.setColumns(32);
            p.add(acceptField);
            acceptField.addActionListener(this);
            fp.add(p);
            p = new Panel();
            p.add(acceptButton = new Button("ACCEPT"));
            acceptButton.addActionListener(this);
            p.add(cancelButton = new Button("CANCEL"));
            cancelButton.addActionListener(this);
            fp.add(p);
            dl.add( fp, "South" );

            dl.pack();
            dl.show();
         }

         protected void output( String msg ) {
            if ( this.sc == null )
               System.out.println( msg );
            else
               sc.sendMessage( msg );
         }

      //
      // Implementation of ActionListener Interface
      //
         public void actionPerformed(ActionEvent event)
         {
            Object target = event.getSource();

            if ( target == acceptField ) {
               output(acceptField.getText() + " Enterred.");
               if ( acceptAndExit ) {
                  dl.dispose();
               }
            } else if ( target == acceptButton ) {
               output("Accept.");
               dl.dispose();
            } else if ( target == cancelButton ) {
               output("Cancelled.");
               acceptField.setText("");
               dl.dispose();
            }
         } // actionPerformed()
      } // class AcceptDialog
       
      //
      // Example 2 -- YesNoDialog
      //
      import java.awt.*;
      import java.awt.event.*;

      public class YesNoDialog extends Dialog
         implements ActionListener
      {
         protected Frame f;
         protected MultiLineLabel mLabel;
         protected Button yesButton, noButton;
         protected boolean acceptAndExit;
         protected StatusConsol sc;

         public YesNoDialog( Frame parent, String title, String message ){
            this(parent, title, message, (StatusConsol)null);
         }
         public YesNoDialog( Frame parent, String title, String message, StatusConsol
      sc ) {
            // create the window
            super(parent, title, true);
            this.sc = sc;

            this.setLayout(new BorderLayout());
            mLabel = new MultiLineLabel(message);
            add( mLabel, "Center");
            Panel p = new Panel();
            p.add(yesButton = new Button("YES"));
            yesButton.addActionListener(this);
            p.add(noButton = new Button("NO"));
            noButton.addActionListener(this);
            add( p, "South" );
            // addWindowListener(this); // want window events
            pack();
            show();
         }

         public void showAgain() { pack(); show(); }

         private void output( String msg ) {
            if ( this.sc == null )
               System.out.println( msg );
            else
               sc.sendMessage( msg );
         }
         protected void winEventMsg( String who, WindowEvent event) {
            // output("Dialog (" + who + ") - " + event.toString());
         }

         public void yes() { output("YES pushed");}
         public void no() { output("NO pushed");}
      //
      // Implementation of ActionListener Interface
      //
         public void actionPerformed(ActionEvent event)
         {
            Object target = event.getSource();

            if ( target == yesButton ) {
               yes();
               dispose();
            } else if ( target == noButton ) {
               no();
               dispose();
            }
         } // actionPerformed()
      } // class YesNoDialog

      //
      // Example 2: ChoiceDialog -- main method is here
      //

      import java.awt.*;

      public class ChoiceDialog extends YesNoDialog {
            public static void main(String args[]) {
               Frame f = new Frame();
               f.setTitle("YES & NO Dialog");
               f.setSize(300,300);
               f.pack();
               f.show();
               ChoiceDialog ad = new ChoiceDialog( f, "Make a Choice"
                        , "What is the answer of\nFlag is flying in the deep
      sea\n?????\n" );
               System.out.println("Answer is " + ad.getAnswer() );
               ad = new ChoiceDialog( f, "mm", "Make a choice" );
               System.out.println("Answer is " + ad.getAnswer() );
               for(int ix=0; ix < 10; ix++) {
                  ad.showAgain(); // now it work. ??? WHY ???
                  System.out.println("Answer is " + ad.getAnswer() );
               }
               System.exit(0);
            } // main
        
            private int answerX;
            static int cnt = 0;
            public ChoiceDialog( Frame f, String title, String msg ) {
               super(f, title, msg);
               answerX = -99;
            }
            private void output(String msg) {
               System.out.println(msg + answerX + " @(" + (++cnt) + ")" );
            }
            public void yes() { answerX = 1; output("choice answer = "); }
            public void no() { answerX = 0; output("choice answer = ");}
            synchronized public int getAnswer() {
               output("Choice has answer of ");
               return answerX;
               }
      }
      company - Sprint , email - ###@###.###
      ======================================================================

            duke J. Duke
            johsunw Joon Oh (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: