Results 1 to 5 of 5
  1. #1
    gymangel812 is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default Several newbie questions, applet help mostly

    for my homework i am creating applet that has a user enter a sandwich name (they must enter this), select a size (they can only select 1 size), and use checkboxes to select topping (more than 1 topping can be selected, no topping *have* to be selected). I am using textpad to edit the program.

    I am having several problems:
    1. If the sandwich field is not chosen, the message doesn't show up on the output label. It shows null.

    2. If toppings aren't chosen, it is also showing null, when it should be showing nothing.

    3. the applet should be "updated" (changed immediately after clicking a different size radio button or condiment checkbox) if a user changed a box (ie from small to medium).

    Java Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    public class Freddie extends Applet implements ItemListener
    {
    	String size, catsupOutput, mustardOutput, picklesOutput, sandwichName;
    
    	// create components for applet
    	Label sandwichLabel = new Label ("Please enter your sandwich:");
    		TextField sandwichField = new TextField(20);
    
    	Label sizeLabel = new Label("Please choose your size:");
    
    	CheckboxGroup sizeGroup = new CheckboxGroup();
    		Checkbox smallBox = new Checkbox("Small",true,sizeGroup);
    		Checkbox mediumBox = new Checkbox("Medium",false,sizeGroup);
    		Checkbox largeBox = new Checkbox("Large",false,sizeGroup);
    
    	Label condimentLabel = new Label("Please choose your condiments:");
    		Checkbox catsupBox = new Checkbox("Catsup",false);
    		Checkbox mustardBox = new Checkbox("Mustard",false);
    		Checkbox picklesBox = new Checkbox("Pickles",false);
    	Label outputLabel = new Label(" ");
    
    	public void init()
    	{
    		setBackground(Color.red);
    		setForeground(Color.black);
    		add(sandwichLabel);
    		add(sandwichField);
    		sandwichField.requestFocus();
    		add(sizeLabel);
    		add(smallBox);
    		smallBox.addItemListener(this);
    		add(mediumBox);
    		mediumBox.addItemListener(this);
    		add(largeBox);
    		largeBox.addItemListener(this);
    		add(condimentLabel);
    		add(catsupBox);
    		catsupBox.addItemListener(this);
    		add(mustardBox);
    		mustardBox.addItemListener(this);
    		add(picklesBox);
    		picklesBox.addItemListener(this);
    		add(outputLabel);
    	}
    
    	// This method is triggered by the user clicking on a option button
    	public void itemStateChanged(ItemEvent choice)
    	{
    		sandwichName = "";
    		sandwichName = sandwichField.getText();
    		if (sandwichField.getText().equals ("")) outputLabel.setText("Please enter a sandwich.");
    		else
    			if (smallBox.getState()) size = "small";
    			else
    				if (mediumBox.getState()) size = "medium";
    				else
    					if (largeBox.getState()) size = "large";
    		if (catsupBox.getState()) catsupOutput = "catsup";
    		if (mustardBox.getState()) mustardOutput = "mustard";
    		if (picklesBox.getState()) picklesOutput = "pickles";
    		outputLabel.setText(size + " " + sandwichName + " with " + catsupOutput + " " + mustardOutput + " " + picklesOutput);
    	}
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Walk through your logic on paper. You'll see ...
    Java Code:
      public void itemStateChanged(ItemEvent choice) {
        sandwichName = "";
        sandwichName = sandwichField.getText();
        
        // *** this will set the label's text if field is empty, but
        if (sandwichField.getText().equals(""))
          outputLabel.setText("Please enter a sandwich.");
        else if (smallBox.getState())
          size = "small";
        else if (mediumBox.getState())
          size = "medium";
        else if (largeBox.getState())
          size = "large";
        if (catsupBox.getState())
          catsupOutput = "catsup";
        if (mustardBox.getState())
          mustardOutput = "mustard";
        if (picklesBox.getState())
          picklesOutput = "pickles";
        
        // *** what will this do to the label's text that you've set above?
        outputLabel.setText(size + " " + sandwichName
            + " with " + catsupOutput + " " + mustardOutput
            + " " + picklesOutput);
    
      }
    (see comments)

  3. #3
    gymangel812 is offline Member
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Default

    ah i fixed the sandwich label validation but i still can't figure out why the output label showing null until something is checked & the applet isn't updating instantly when something is unchecked...

    here is the updated itemstatechanged method:
    Java Code:
    	public void itemStateChanged(ItemEvent choice)
    	{
    		sandwichName = "";
    		sandwichName = sandwichField.getText();
    		if (sandwichField.getText().length() == 0) {
    		            outputLabel.setText("Please enter a sandwich.");
    		        } else {
    		            if (smallBox.getState()) {
    		                size = "small";
    		            } else if (mediumBox.getState()) {
    		                size = "medium";
    		            } else if (largeBox.getState()) {
    		                size = "large";
    		            }
    		            if (catsupBox.getState()) {
    		                catsupOutput = "catsup";
    		            }
    		            if (mustardBox.getState()) {
    		                mustardOutput = "mustard";
    		            }
    		            if (picklesBox.getState()) {
    		                picklesOutput = "pickles";
    		            }
    		            outputLabel.setText(size + " " + sandwichName + " with "
    		                    + catsupOutput + " " + mustardOutput + " " + picklesOutput);
    		        }
    		    }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your Strings are declared but not initialized (up at the top of your program), so they will be null by default. As to responsiveness, add some println statements to see what is happening and when.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Also, I would try to simplify, clean up, and beautify the code if it were mine, for one by subdividing that itemStateChanged method. Something like:
    Java Code:
       public void displaySandwichInfo() {
          String sandwichText = sandwichField.getText();
          if (sandwichText == null || sandwichText.trim().isEmpty()) {
             sandwichText = "Please enter a sandwich";
          }
          else {
             // you create the two methods below
             sandwichText += getSandwichSize(); 
             sandwichText += getCondiments();
          }
          outputLabel.setText(sandwichText);
       }
    and then have itemStateChanged call this. You could even put a focus listener or Text listener on your text field and have it call this.

Similar Threads

  1. newbie needs help with an applet please
    By tux633k in forum Java Applets
    Replies: 5
    Last Post: 09-10-2009, 10:23 PM
  2. Couple of newbie questions
    By ananasman in forum New To Java
    Replies: 11
    Last Post: 11-21-2008, 12:54 AM
  3. Some newbie questions about shortcuts
    By nefigah in forum Eclipse
    Replies: 2
    Last Post: 07-29-2008, 09:25 AM
  4. Newbie with questions
    By buzzdsm in forum New To Java
    Replies: 14
    Last Post: 06-05-2008, 05:11 PM
  5. Newbie in applet, Help me
    By barney in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 08:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •