Link to home
Start Free TrialLog in
Avatar of randylPalomo
randylPalomo

asked on

jdialog sample

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
Avatar of sptw
sptw

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.
Avatar of randylPalomo

ASKER

well it really works, what if i added a button before the dialog box open. please add sptw, i forgot.
ASKER CERTIFIED SOLUTION
Avatar of sptw
sptw

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
thanks sptw, my program is now working hope u could help me in my next question.
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....
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.