jdialog sample

randylPalomo
randylPalomo used Ask the Experts™
on
i'm newbie need help. can u give me a sample program, wherein an applet opens a dialog box that contain input fields to be save later in the main applet when and ok button invoke. even just a single field. thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
A simple applet.... hope it helps

//********************************************************
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class AppletSample extends JApplet {
     private JLabel result;
     
     public void init () {
          result = new JLabel("Initial",SwingConstants.CENTER);
          getContentPane().add(result,BorderLayout.CENTER);
          new DialogExample(this);
     }
     
     public void setLabel (String lbl) {
          result.setText(lbl);
     }
     
     class DialogExample extends JDialog implements ActionListener {
          JTextField tf;
          JButton btn;
          AppletSample applet;
         
          public DialogExample (AppletSample parent) {
               super();
               setTitle("Update Parent");
               setSize(200,100);
               setLocation(240,40);          
               applet = parent;
               tf = new JTextField (10);
               btn = new JButton ("OK");
               btn.addActionListener(this);
               JPanel panel = new JPanel(new FlowLayout());
               panel.add(tf);
               panel.add(btn);
               
               getContentPane().add(panel,BorderLayout.CENTER);
               setVisible(true);
          }
         
          public void actionPerformed (ActionEvent ae) {
               if (ae.getSource() == btn) {
                    applet.setLabel(tf.getText().trim());
               }
          }
     }
}
//********************************************************
the html file....

<html>
<applet code="AppletSample.class" width = 200 height = 50></applet>
</html>

I tested using appletviewer.

Author

Commented:
well it really works, what if i added a button before the dialog box open. please add sptw, i forgot.
Commented:
>>>>> 'what if i added a button before the dialog box open'
Does the mean that u add a button in the applet, upon clicking the button the dialog box appears?
If so,
i have updated the code to cater for this

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class AppletSample extends JApplet implements ActionListener {
     private JLabel result;
     private JButton button;
     
     public void init () {
          result = new JLabel("Initial",SwingConstants.CENTER);
          button = new JButton("Open Dialog");
          button.addActionListener(this);
          getContentPane().add(result,BorderLayout.CENTER);
          getContentPane().add(button,BorderLayout.SOUTH);
     }
     
     public void setLabel (String lbl) {
          result.setText(lbl);
     }
     public void actionPerformed (ActionEvent ae) {
          if (ae.getSource() == button)
               new DialogExample(this);
     }
     
     class DialogExample extends JDialog implements ActionListener {
          JTextField tf;
          JButton btn;
          AppletSample applet;
         
          public DialogExample (AppletSample parent) {
               super();
               setTitle("Update Parent");
               setSize(200,100);
               setLocation(240,40);          
               applet = parent;
               tf = new JTextField (10);
               btn = new JButton ("OK");
               btn.addActionListener(this);
               JPanel panel = new JPanel(new FlowLayout());
               panel.add(tf);
               panel.add(btn);
               
               getContentPane().add(panel,BorderLayout.CENTER);
               setVisible(true);
          }
         
          public void actionPerformed (ActionEvent ae) {
               if (ae.getSource() == btn) {
                    applet.setLabel(tf.getText().trim());
               }
          }
     }
}
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Author

Commented:
thanks sptw, my program is now working hope u could help me in my next question.

Commented:
Please be fair when you grade the answer. Since the solution met your requirement the first and updated for your additional requirement, fair grading is deserved.

Thanks anyway :)..... Post your question, i'll do my best to help....

Author

Commented:
ooppss sorry sptw that well be noted in fact i opted to change after i click the accepted answer button but i don't know how to change. anyway its my learning process also, i need to read tips on comments and answer.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial