• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Object into Table

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
Java_Novice
Asked:
Java_Novice
  • 7
  • 5
  • 4
  • +2
1 Solution
 
CEHJCommented:
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
 
girionisCommented:
 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
 
girionisCommented:
 Ah... CEHJ was faster :-)
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
CEHJCommented:
and the fields should not have public scope
0
 
Java_NoviceAuthor Commented:
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
 
CEHJCommented:
Write the object properly and i'll help you complete it.
0
 
Java_NoviceAuthor Commented:
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
 
Java_NoviceAuthor Commented:
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
 
CEHJCommented:
>>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
 
emalagarCommented:
Good one CEHJ :)
0
 
Java_NoviceAuthor Commented:
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
 
Java_NoviceAuthor Commented:
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
 
Java_NoviceAuthor Commented:
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
 
girionisCommented:
 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
 
Java_NoviceAuthor Commented:
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
 
CEHJCommented:
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
 
girionisCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
/* 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
 
Mayank SAssociate Director - Product EngineeringCommented:
How about rating it now?
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 7
  • 5
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now