Home arrow static arrow Java Programming [Archive] - Applet Help
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Applet Help
10 Duke Stars available
This topic has 8 replies on 1 page.

Posts:5
Registered: 6/23/04
Applet Help  
Jun 23, 2004 6:39 AM



 
Hello - I have to create an applet for a class that I am taking and my issue is I need to add an input box popup. I'm not sure where to place it in the script. Should I add it to the begining of the code. Well let me narrow it down. I need to create an applet with a button, two labels, a TextField and an input box.

The first thing to do is create an applet that will display a set of instructions (label one). Once the user has read the instructions they click a button (OK) to move to the next part. This is the input box popup with the TextField. The user would need to enter in a number that is 11 char. long. Then click OK at the bottom for submittal (this button acts as a reset control). If the user doesn't enter enough numbers the second label will let the user know. I'm not at all good with Java. I've included the code that I have so far. Any help is appreciated.

//SSNApplet.java: Applet using materials learned thoughout the course
package PortfolioArtifact;

import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

abstract public class SSNApplet extends javax.swing.JApplet implements ActionListener
{
//Declare and create text field for SSN
private JTextField jtfSSN = new JTextField();

//Declare and create an OK button
private JButton jbButtonOK = new JButton("OK");

/** Instruction Panel */
public void init(){
//Create panel one to hold LabelOne
JPanel pLabelOne = new JPanel();
pLabelOne.setLayout(new GridLayout(2,1));
pLabelOne.add(new Label("Click the button and enter a social security number in the format 123-45-6789"));

//Create panel two to hold ButtonOK
JPanel pButtonOK = new JPanel();
pButtonOK.setLayout(new FlowLayout(FlowLayout.CENTER));
pButtonOK.add(jbButtonOK);

//Add the components to the applet
getContentPane().add(pLabelOne, BorderLayout.CENTER);
getContentPane().add(pButtonOK, BorderLayout.SOUTH);

//Register listerner
jbButtonOK.addActionListener(this);
}

/** Initialize the Input Box Popup */
/** Main method */
public static void main(String[] args) {
String strSocialSecurityNo = JOptionPane.showInputDialog(null, "Enter SSN here", "Portfolio Artifact", JOptionPane.QUESTION_MESSAGE);
double dblSocialSecurityNo = Double.parseDouble(strSocialSecurityNo);

}

}

Thanks

 

Posts:1,995
Registered: 12/10/00
Re: Applet Help  
Jun 23, 2004 6:43 AM (reply 1 of 8)



 
Please post formatted code enclosed in [code][/code] as described in [url http://forum.java.sun.com/features.jsp#Formatting] Formatting help[/url].

As I see you already use JOptionPane.showInputDialog(). What's wrong with it? Please describe what results you expect and what you get instead.

Mike
 

Posts:5
Registered: 6/23/04
Re: Applet Help  
Jun 23, 2004 6:54 AM (reply 2 of 8)



 
//SSNApplet.java: Applet using materials learned thoughout the coursepackage PortfolioArtifact; import javax.swing.JOptionPane;import java.awt.*;import java.awt.event.*;import javax.swing.JButton;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField; abstract public class SSNApplet extends javax.swing.JApplet implements ActionListener{  //Declare and create text field for SSN  private JTextField jtfSSN = new JTextField();   //Declare and create an OK button  private JButton jbButtonOK = new JButton("OK");   /** Instruction Panel */  public void init(){    //Create panel one to hold LabelOne    JPanel pLabelOne = new JPanel();    pLabelOne.setLayout(new GridLayout(2,1));    pLabelOne.add(new Label("Click the button and enter a social security number in the format 123-45-6789"));     //Create panel two to hold ButtonOK    JPanel pButtonOK = new JPanel();    pButtonOK.setLayout(new FlowLayout(FlowLayout.CENTER));    pButtonOK.add(jbButtonOK);     //Add the components to the applet    getContentPane().add(pLabelOne, BorderLayout.CENTER);    getContentPane().add(pButtonOK, BorderLayout.SOUTH);     //Register listerner    jbButtonOK.addActionListener(this);  }   /** Initialize the Input Box Popup */  /** Main method */  public static void main(String[] args) {String strSocialSecurityNo = JOptionPane.showInputDialog(null, "Enter SSN here", "Portfolio Artifact", JOptionPane.QUESTION_MESSAGE);  double dblSocialSecurityNo = Double.parseDouble(strSocialSecurityNo);   }  } 


The input box works but I don't know where in the code I need to place it to get it to work with my applet. I am aware that the applet doesn't work because I'm not quite done with it. I'm also not sure how or where to implement the second label. This label will let the user know the text has insufficient char.

Thanks.
 

Posts:1,995
Registered: 12/10/00
Re: Applet Help  
Jun 23, 2004 7:04 AM (reply 3 of 8)



 
You will call the input dialog when the proper event is fired - for example when the user clicks the button that has to open the input dialog. You just register as an ActionListener to that button and when action is performed i.e. actionPerformed(ActionEvent) is called from that button you show the input box and process user input.

If you need information on buttons, listeners, etc. check the [url http://java.sun.com/docs/books/tutorial/uiswing/events/index.html]tutorial[/url]

HTH
Mike
 

Posts:5
Registered: 6/23/04
Re: Applet Help  
Jun 23, 2004 7:08 AM (reply 4 of 8)



 
I appreciate your help. I'm sure I will be posting again later. For some reason grasping Java has become a difficult task for me. I'll most likely need help wrapping this applet up.

Thanks again,
Christie
 

Posts:1,995
Registered: 12/10/00
Re: Applet Help  
Jun 23, 2004 7:14 AM (reply 5 of 8)



 
You already have some ActionListener-related code. It is somewhat strange that your applet is abstract, and there is no actionPerformed implementation. This code looks like a skeleton to build your applet from, right?
Anyway, don't give your dukes so easily, you may need to ask more than 2-3 questions ;)

Mike
 

Posts:5
Registered: 6/23/04
Re: Applet Help  
Jun 23, 2004 7:24 AM (reply 6 of 8)



 
Actually, using JBuilder, I had an error and the only way I could get it to go away was to make it abstract. I figured I could go back and change it later. I wasn't really sure as to why it was giving me such a hard time. I really changed it just to make sure that my input was working.
 

Posts:1,995
Registered: 12/10/00
Re: Applet Help  
Jun 23, 2004 7:34 AM (reply 7 of 8)



 
If you really didn't know why you got that compiler error you should consider reading some OO-related topics, especially interfaces and inheritance. I will point you to the [url http://java.sun.com/docs/books/tutorial/java/] Java Basis part of the tutorial[/url]. It's worth reading it not only to complete your assignment easier but in future everyday use if you remain in programming field.

MIke
 

Posts:5
Registered: 6/23/04
Re: Applet Help  
Jun 23, 2004 4:34 PM (reply 8 of 8)



 
In this program if the user does not enter 11 char. then the Applet will show a label stating that the information entered is invalid. How do I get the program to recognize the 11 char?

Thanks
 
This topic has 8 replies on 1 page.