?
Solved

Object  into Table

Posted on 2003-03-03
19
Medium Priority
?
207 Views
Last Modified: 2010-03-31
Hi I have an Golfer object Which is composed as follows :-

 public class Golfer {

    public String Name;
    public int Pair;
    public int Shots;
    public int Score;
    public int Hole;
   
   


    public Golfer() {

      Name = " ";
      Pair = 0 ;
      Shots = 0;
      Score = 0;
      Hole = 0;
     


    }
  }


This object has 16 instances. I want to display the information stored in that instances within a jtable. Can some one help me on. Can some one tell me how i can do this.
0
Comment
Question by:Java_Novice
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 4
  • +2
19 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8057823
Yes, but you need to complete your Golfer class first with get/set methods. Your code in the constructor, incidentally is superfluous, as Java will initialize the fields for you.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8057844
 Firstly where do you store the data in the Golfer object? In the way the Golfer object is constructed it contains no data whatsoever.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8057847
 Ah... CEHJ was faster :-)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 8057859
and the fields should not have public scope
0
 

Author Comment

by:Java_Novice
ID: 8057933
I kept the variables within the object public bascially because i couldnt be asked writing access functions. Anyway the inforamtion is being stored within the object is comming from a text file . The information is being stored sucessfully within the object with the use of a string tokenizer and creating 16 instances of the Golfer object. After having stored the information within the instances of the object,I dont know what to do next to get that inforamtion within in the table. so that the information is stored under headings. example shown below below.


NAME        PAIR     SHOTS      SCORE       HOLE
Nick faldo   2        1          2            1
     
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8057990
Write the object properly and i'll help you complete it.
0
 

Author Comment

by:Java_Novice
ID: 8058061
What do you mean by  Write the object properly , do you want me to make the variables private and write the access functons.When i said i cant be asked i meant i dont have time as im on a tight schedule and also i have been instructed to keep the size of the code down. I would paste the whole File that i have created so far but its pretty big as im using jbuilder. Can not just not direct me on how i should put the data in the table from the object. Some one told me that i will need to create a table model.
0
 

Author Comment

by:Java_Novice
ID: 8058086
I kept the variables within the object public bascially because i couldnt be asked writing access functions. Anyway the inforamtion is being stored within the object is comming from a text file . The information is being stored sucessfully within the object with the use of a string tokenizer and creating 16 instances of the Golfer object. After having stored the information within the instances of the object,I dont know what to do next to get that inforamtion within in the table. so that the information is stored under headings. example shown below below.


NAME        PAIR     SHOTS      SCORE       HOLE
Nick faldo   2        1          2            1
     
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8058100
>>asked i meant i dont have time as im on a tight schedule

Isn't that a coincidence - so am i. Best of luck.
0
 
LVL 2

Expert Comment

by:emalagar
ID: 8058188
Good one CEHJ :)
0
 

Author Comment

by:Java_Novice
ID: 8058197
CEHJ  why are being like this for. All the others one this site are so helpful and here im almost begging you for some help and you ar being awkward. Sorry if i sound a bit of a b*****d. but im a bit wroked up and stressed out about the whole table thing.
0
 

Author Comment

by:Java_Novice
ID: 8058209
CEHJ  why are being like this for. All the others one this site are so helpful and here im almost begging you for some help and you ar being awkward. Sorry if i sound a bit of a b*****d. but im a bit wroked up and stressed out about the whole table thing.
0
 

Author Comment

by:Java_Novice
ID: 8058231
CEHJ  why are being like this for. All the others one this site are so helpful and here im almost begging you for some help and you ar being awkward. Sorry if i sound a bit of a b*****d. but im a bit wroked up and stressed out about the whole table thing.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8058564
 Java_Novice you have to understand that most of the people here actually spend their own time to help other people. We all have a busy schedule but we try to squeeze 5 mins in between tasks to help each other.

  I am sorry to tell you this but if you are not willing to help your self then noone else will help you. Please try to do something on your own and then you will have all the help you asked for.
0
 

Author Comment

by:Java_Novice
ID: 8063545
Ok Thank girionis . i can give you the code from a parts of the programe. like i said earlier that my whole program far to big in size to paste. The function that is placing the imforamtion in the object is as follows:-

Void LoadGolfers_actionPerformed(ActionEvent e) {
    FileDialog LoadG = new FileDialog (this, "Load Golfer File", FileDialog.LOAD);
    LoadG.setDirectory ("");
    LoadG.show();


    String fileName;
        fileName = LoadG.getFile();
        PHole.setText("");

        String line = null;

        try {
          BufferedReader buf = new BufferedReader(new FileReader(fileName));
          int i = 0;

          while ( (line = buf.readLine()) != null) {
            // now take this line, let's say this is for ex: Ballesteros Severiano and use a StringTokenizer to break it
           StringTokenizer st = new StringTokenizer(line, " ");

            // while(st.hasMoreTokens()){
            // Golfers[i] = new Golfer();
             String S = (String)st.nextToken();
             String F = (String)st.nextToken();

             Golfers[i].Name = S +" "+ F;
             PPlayer.append(Golfers[i].Name +"\n");
             PScore.append(Golfers[i].Score+"\n");
             PPar.append(Golfers[i].Pair+"\n");
             PHole.append(Golfers[i].Hole+"\n");
             setcard ();

             i++;



          }

          buf.close();


        }

        catch (Exception eb) {
          eb.printStackTrace();
        }
 // adds the items to the JCombobox
        for (int i = 0; i < 16; ++i) {
       PPair1.addItem(Golfers[i].Name);
       PPair2.addItem(Golfers[i].Name);
       FetchName.addItem(Golfers[i].Name);
      }




  }


So please tell me what do i do now to get the information stored within the objects  by the string tokenizer into a table.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8063676
You're missing the point. The class Golfer has still not been defined properly and of course score, pair and hole  (which should not be capitalized) do not contain any data.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8063924
CEHJ is right. You need to implement your Golfer functionality in order to store the data. Currently the only thing you are doing is to initialize the variables and nothing more. You need to do something like:

public class Golfer
{
   private String name;
   
   public Golfer(String name)
   {
     this.name = name;
   }

   public String getName()
   {
      return name;
   }
}

  for all the fields in the Golfer class. By doing this you hold the data in the class.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 400 total points
ID: 8109085
/* Here's a sample code for JTable with relevance to your program - the program is not complete */

import javax.swing.* ;
import java.awt.* ;
import java.util.* ;

class ShowFrame extends JFrame
{
  Container contentPane ;
  JTable table ;
  JScrollPane scrollPane ;
  String data[][], columns[] =  { "Name", "Pair", "Shots", "Score", "Hole" } ; // columns for the table

  ShowFrame ( Golfer objects[], int count ) // constructor ()
  {
    super ( "POINTS TALLY - GOLF" ) ;
    contentPane = getContentPane () ;
    contentPane.setBackground ( Color.gray ) ;
    data = new String[count][5] ;
   
    for ( int i = 0 ; i < count ; i ++ )
    {
      data[i][0] = objects[i].Name ;
      data[i][1] = Integer.toString ( objects[i].Pair ) ;
      data[i][2] = Integer.toString ( objects[i].Shots ) ;
      data[i][3] = Integer.toString ( objects[i].Score ) ;
      data[i][4] = Integer.toString ( objects[i].Hole ) ;

    } // end for

    table = new JTable ( data, columns ) ; // table to display the data
    table.setFont ( new Font ( "Default", Font.BOLD, 13 ) ) ;
    table.setForeground ( Color.blue ) ;
    table.setCellSelectionEnabled ( false ) ;
    table.setRowSelectionAllowed ( false ) ;
    table.setColumnSelectionAllowed ( false ) ;
    table.setAutoResizeMode ( 4 ) ;
    table.setEnabled ( false ) ;

    scrollPane = new JScrollPane ( table, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS ) ;
    contentPane.add ( scrollPane ) ;
    resize ( 1024, 768 ) ;
    show () ;

  } // end of constructor ()

} // class definition over

To display the table, simply store all your sixteen (or as many) objects into a Golfer array, say obj[], and pass it to the ShowTable constructor as:

new ShowTable ( obj, n ) ;

where n is the number of objects that you have in the array (16 as per your question).

Mayank.

[Hope that helps! Netx time, no codes!]
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8544893
How about rating it now?
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question