Results 1 to 4 of 4
  1. #1
    rpetronejr is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Default GridLayout - Layout of Applet Help

    Hi everyone I am in a bit of a time crunch and unsure what i am doing wrong with my applet/html page which calls it. Its a fairly simple applet with a few textboxes, checkboxes and one calculation. But when i load it, the format is all over the page, its not organized at all, i am lost. I have been at this for about a week. Can anyone please help me, code is below.

    Thanks in advance
    Rob

    Java Code:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class FinalApplet extends Applet implements ActionListener, ItemListener {
    
    	//declare variables
    	double value;
    
    	// Create components for applet
    	Label firstNameLabel = new Label("First Name");
    	TextField firstNameField = new TextField(15);
    	Label lastNameLabel = new Label("Last Name");
    	TextField lastNameField = new TextField(15);
    	Label addressLabel = new Label("Address");
    	TextField addressField = new TextField(15);
    	Label cityLabel = new Label("City");
    	TextField cityField = new TextField(10);
    	Label stateLabel = new Label("State");
    	TextField stateField = new TextField(2);
    	Label whenLabel = new Label("How soon to buy new car?");
    	Choice whenChoice = new Choice();
    	Label makeLabel = new Label("Make");
    	Choice makeChoice = new Choice();
    	Label modelLabel = new Label("Model");
    	Choice modelChoice = new Choice();
    	Label colorLabel = new Label("Color");
    	Choice colorChoice = new Choice();
    
    	//contruct components
    	CheckboxGroup optionsGroup = new CheckboxGroup();
    	Checkbox leatherBox = new Checkbox ("Leather", false, optionsGroup);
    	Checkbox navigationBox = new Checkbox ("Navigation", false, optionsGroup);
    	Checkbox seatsBox = new Checkbox ("Heated Seats", false, optionsGroup);
    	Checkbox sunroofBox = new Checkbox ("SunRoof", false, optionsGroup);
    
    	Label priceLabel = new Label("Anticipated Price");
    	TextField priceField = new TextField(5);
    	Label rateLabel = new Label("Rate");
    	TextField rateField = new TextField(5);
    	Label numbermonthsLabel = new Label("Number of months");
    	TextField numbermonthsField = new TextField(5);
    	Label commentsLabel = new Label("Comments");
    	TextField commentsField = new TextField(50);
    
    	Label outputLabel = new Label("Click the Calc button");
    
    
    	Button calcButton = new Button("Calculate");
    	Button submitButton = new Button("Submit");
    	Button clearButton = new Button("Clear");
    
    		public void init() {
    
    			setBackground(Color.BLUE);
    			setForeground(Color.BLACK);
    
    			setLayout(new GridLayout(4,0));
    
    			add(firstNameLabel);
    			add(firstNameField);
    			add(lastNameLabel);
    			add(lastNameField);
    			add(addressLabel);
    			add(addressField);
    			add(cityLabel);
    			add(cityField);
    			add(stateLabel);
    			add(stateField);
    			leatherBox.addItemListener(this);
    			add(leatherBox);
    			navigationBox.addItemListener(this);
    			add(navigationBox);
    			seatsBox.addItemListener(this);
    			add(seatsBox);
    			sunroofBox.addItemListener(this);
    			add(sunroofBox);
    			add(priceLabel);
    			add(priceField);
    			add(rateLabel);
    			add(rateField);
    			add(numbermonthsLabel);
    			add(numbermonthsField);
    			add(commentsLabel);
    			add(commentsField);
    			add(new Label(""));
    			add(outputLabel);
    			add(submitButton);
    			add(clearButton);
    			add(calcButton);
    
    			// Set Focus on First Name Field
    			firstNameField.requestFocus();
    
    			// Populate values
    			whenChoice.add("1 Months");
    			whenChoice.add("3 Months");
    			whenChoice.add("6 Months");
    			whenChoice.add("More than 1 Year");
    
    			makeChoice.add("Audi");
    			makeChoice.add("Honda");
    			makeChoice.add("Mazda");
    			makeChoice.add("Nissan");
    			makeChoice.add("Scion");
    
    			modelChoice.add("2 Door");
    			modelChoice.add("4 Door");
    			modelChoice.add("SUV");
    			modelChoice.add("Crossover");
    
    			colorChoice.add("Blue");
    			colorChoice.add("Red");
    			colorChoice.add("Silver");
    			colorChoice.add("White");
    
    			calcButton.addActionListener(this);
    			submitButton.addActionListener(this);
    			clearButton.addActionListener(this);
    
    
    		}
    		//check to see which button is pressed
    		public void actionPerformed(ActionEvent e)
    			{
    				String arg = e.getActionCommand();
    				if (arg.equalsIgnoreCase("Submit"))
    				{
    
    				}
    				else if (arg.equalsIgnoreCase("Clear"))
    				{
    
    					clearFields();
    
    					firstNameField.requestFocus();
    				}
    				else if (arg.equalsIgnoreCase("Calculate"))
    				{
    					double price = Double.parseDouble(priceField.getText());
    					double rate = Double.parseDouble(rateField.getText());
    					double months = Double.parseDouble(numbermonthsField.getText());
    					value = (price * rate) / months;
    					outputLabel.setText("Monthly Payment is" + Math.round(value* 100)/100D);
    
    				}
    			}
    
    		//Clear selections
    		private void clearFields()
    		{
    			firstNameField.setText("");
    			lastNameField.setText("");
    			addressField.setText("");
    			cityField.setText("");
    			stateField.setText("");
    			commentsField.setText("");
    		}
    			public void itemStateChanged(ItemEvent choice)
    			{
    
    			}
    
    	}
    Java Code:
    <HTML>
    <APPLET CODE = "FinalApplet.class" WIDTH = "400" HEIGHT = "550">
    </APPLET>
    </HTML>

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    How many components are you trying to add to the applet?
    Where do you want them to go?
    Have you read the API doc for the GridLayout? Your code asks for 4 rows and as many columns as needed.
    What do you get?
    the format is all over the page
    Can you be more descriptive than that?

  3. #3
    rpetronejr is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Default

    Trying to add about 12 to 13, various form text boxes lables, etc. I just want it to be

    First Name and Last Name
    Address, City, State
    All the text boxes
    The few text boxes for calculations
    Comments box
    All buttons

    But everything is everywhere, i have dug around, afraid i am lost and under a time restraint. Help would would great.

    Thanks
    Rob

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

    Default

    Quote Originally Posted by rpetronejr View Post
    Trying to add about 12 to 13, various form text boxes lables, etc. I just want it to be

    First Name and Last Name
    Address, City, State
    All the text boxes
    The few text boxes for calculations
    Comments box
    All buttons

    But everything is everywhere,
    Many here often mix layouts, for instance create a Panel that uses GridBagLayout for the Labels and paired TextFields, then place this in the BorderLayout.CENTER position of a Panel that uses BorderLayout , then create Buttons that are placed in a Panel that uses GridLayout and place that in the BorderLayout using Panel in the BorderLayout.SOUTH position. etc...

    i have dug around, afraid i am lost and under a time restraint. Help would would great.
    We'll be happy to help with your code especially if you have a specific question rather than here's the code, please fix it. But please remember that your time constraint is not our concern. Please have a look at the Sun tutorial on the different layout managers as it will help you greatly:
    Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Much luck.

    edit: you're current code appears to be suffering from a few problems including trying to place all the components into one container using a single layout, and also using a GridLayout(4, 0) which forces four rows but then adding components whose numbers and types don't lend themselves well to be displayed in this way. Again, the layout tutorials will help.
    Last edited by Fubarable; 07-03-2010 at 05:43 AM.

Similar Threads

  1. Why use layout managers in applet?
    By Splat in forum New To Java
    Replies: 2
    Last Post: 10-14-2009, 05:41 AM
  2. SWT GridLayout
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 10:04 AM
  3. Use GridLayout in java
    By toby in forum Java Applets
    Replies: 1
    Last Post: 08-04-2007, 01:44 AM
  4. Help with gaps in GridLayout
    By trill in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 08:20 PM
  5. Gridlayout
    By Marty in forum AWT / Swing
    Replies: 2
    Last Post: 05-31-2007, 12:48 PM

Posting Permissions

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