Results 1 to 15 of 15
  1. #1
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default quesion - how to display an array in an applet?

    hi all,

    i'm working in a project now. i know how to declare arrays but i never use them in an applet.

    so my question is how can i view two dimensions arrays in applet??

    also, how can i get data from a user and save it in the array??

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    this sounds like a JTable

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

    Default

    Use a text field to get data from a user.

  4. #4
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default

    thanks for your replies
    the number of students will be given by the user
    i now know how can i add textfields as the number entered by the user, but the question is, how can i store the entered names (by user) in an array??

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

    Default

    1) get the data out of the text field into a String. Read the API doc for the text field class for which method to use
    2) put a reference in the array to that String: array[ix] = string;

  6. #6
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default

    i am sorry, i am a beginner in Java, and i did not really understand all what you posted

    let me give an idea of what i am doing in my program, the program is to ask the user he enter a number of students. then the same number of textfields must be displayed. the user has to enter students names, and grades for 6 subjects.
    my question is if i want to display all students names and grades in a table, find the averages of each student and each subject? how can i store the entered grade of student name in the array. should i use only one textfield for students names and one for grades? if yes, how can get the data from the textfield and store it in the array

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

    Default

    JTable would give a nicer but I'm not sure if a beginner would be up to it.
    Otherwise use a layout manager like GridLayout to build simple table of text fields.
    Are you going to get the names and grades one at a time from the user and then display them all after they have been processed? Does each student have grades for 6 courses?
    Then the table would have 7 columns, one for name and 6 for grades. Plus maybe another column for the averages.

    how can i store the entered grade of student name in the array.
    1) where do you get the grade? Input to a text field by user?
    Read text field API doc for methods to get data from text field.
    Once grade is in a String, convert it to number by method of Integer or Double class.
    2) Store grade in an array?
    Make an array of proper type: int or float. Decide which element to put the grade in and assign that element the grade: array[ix] = grade;

  8. #8
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default

    Many thanks for your help
    but How many text fields i have to define?

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

    Default

    If there are 6 text flelds on a row and there are 6 rows, that would require 36.

  10. #10
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    hope the following code give you some ideas how to implement your requirements


    Java Code:
    		import java.applet.Applet;
    import java.awt.BorderLayout;
    
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    
    public class Test extends Applet {
    	
    	private static final long serialVersionUID = 1L;
    
    	public void init() {
    		JTextArea area = new JTextArea();
    		area.setEditable(false);
    		
    		
    		int[][] arr = { { 0, 1 }, { 1, 1 }, { 2, 1 } };
    		String str = "";
    
    		for (int r = 0; r < arr.length; r++) {
    			str += "Array[" + r + "][1] = ";
    			for (int c = 0; c < arr[r].length; c++) {
    				str += arr[r][c] + " ";
    			}
    			str += "\n";
    			area.append(str);
    			str = "";
    		}
    		add(area, BorderLayout.CENTER);
    	}
    }

    modify the code according to your requirements.
    Last edited by j2me64; 06-04-2010 at 05:06 PM.

  11. #11
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default

    many thanks to all of you for your help

    i just knew that textfields can be declared in an array :)

    but there is one problem i do not know what how to solve it, which is where should i write the loops, should i create another public void main () and write it there?

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;

    public class students_grades extends Applet implements ActionListener {
    Label title = new Label("<{This program calculate students grades in 6 subjects}>");
    Label number_of_students = new Label("<{Inter the number of the students}>");
    TextField number_of_students_ = new TextField(5);
    Button View_Table = new Button ("View Table");


    public void init( ) {
    //setLayout();
    add (title);
    add (number_of_students);
    add (number_of_students_);
    add (View_Table);
    number_of_students_.addActionListener(this);
    View_Table.addActionListener(this);

    }
    public void actionPerformed(ActionEvent event) {

    // to get the number of students entered by user
    // String titles [8] = [No. , Student Name, Math, Comp, Salam, Arab, MGT, ORG];
    String number_of_students_text = number_of_students_.getText();
    int number_of_students_int = Integer.parseInt(number_of_students_text);

    // define two arrays of textfield
    TextField txtname []= new TextField [number_of_students_int];
    TextField txtgrade [][] = new TextField [number_of_students_int][6];

    // define two arrays ===> one for student names, and the other one for students grade
    String students_names [] = new String [number_of_students_int];
    float students_grades [][] = new float [number_of_students_int][6];

    // add name text fields
    for (int i=0; i<number_of_students_int; i++)
    {
    add (txtname[i]);
    }
    // add grade text fields in 2D array
    for (int i=0; i<number_of_students_int ;i++)
    {
    for (int j=0; j<6 ; j++)
    {
    add (txtgrade[i][j]);
    }
    }

    // get text from text fields and save it in the array
    for (int i=0; i<number_of_students_int; i++)
    {
    students_names [i] = txtname[i].getText();
    }

    for (int i=0; i<number_of_students_int ;i++)
    {
    for (int j=0; j<6 ; j++)
    {
    String Container = txtgrade[i][j].getText();
    int intContainer = Integer.parseInt(Container);
    students_grades[i][j] = intContainer;
    }
    }


    } // action performed
    } // class

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by EBBOOO View Post
    many thanks to all of you for your help

    i just knew that textfields can be declared in an array :)

    but there is one problem i do not know what how to solve it, which is where should i write the loops, should i create another public void main () and write it there?
    (J)Applets don't have a main( ... ) method (you could've read that in your books); they have an init() method where it all starts.

    kind regards,

    Jos

  13. #13
    EBBOOO is offline Member
    Join Date
    Jun 2010
    Posts
    42
    Rep Power
    0

    Default

    thanks Jos
    I've heard this before but i don't know what it's really mean, cause I'm just a beginner

    so, what exactly should i add? or change?

  14. #14
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    My interpretation of what Jos is mainly saying is that you should RTFM. The information on JApplets can be obtained here.

    Getting Started With Applets (The Java™ Tutorials > Deployment > Applets)

    Have fun.

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

    Default

    For now forget about running the code in an applet. Just work on building the GUI in a JPanel and put that panel in a JFrame. Applet extends Panel, so most of your techniques should work there.

Similar Threads

  1. display bar graph with the help of java applet
    By ras_pari in forum Java Applets
    Replies: 1
    Last Post: 12-15-2009, 02:18 PM
  2. display a new jpanel in an applet
    By toymachiner62 in forum Java Applets
    Replies: 6
    Last Post: 10-19-2009, 12:10 AM
  3. Replies: 2
    Last Post: 04-26-2009, 07:36 AM
  4. How to display image from byte array in JPANEL
    By waqasdaar in forum AWT / Swing
    Replies: 0
    Last Post: 03-22-2009, 01:11 AM
  5. Replies: 0
    Last Post: 04-14-2008, 09:43 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
  •