Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating an array for a Jtable

Status
Not open for further replies.

CaptRage61

Programmer
Feb 17, 2005
50
US
I am trying to create an array of Element objects for use in a JTable. I am having problems creating this array and I am not sure why. Here is what I am trying to do:
Code:
Element e = new Element("Mercury", "Hg", 80, 200.59,356.7 );
    Element[][] data = {
        {e.getName(), e.getSymbol(),
         e.getNumber(), e.getWeight(), e.getBP()},
       };
Here is my Element class
Code:
public class Element {
    String name;
    String symbol;
    int number;
    double weight, bp;


    public Element() {
    }
    public Element(String n, String s, int num, float w, float b){
        name = n;
        symbol = s;
        number = num;
        weight = w;
        bp = b;
    }
    public String getName(){
        return name;
    }
    public String getSymbol(){
        return symbol;
    }
    public int getNumber(){
        return number;
    }
    public double getWeight(){
        return weight;
    }
    public double getBP(){
        return bp;
    }
    public void readFile(){
        //reads the contents of a file
    }
}

Does anyone have any ideas on how to make this work???
 
Not sure how your array is going to be used, but at face value it would be assigned like this:-
Code:
Element e = new Element("Mercury", "Hg", 80, 200.59,356.7 );
Element e2 = new Element("Potassium", "K", 19, 39.0983, 773.85);

Element[] data = {e, e2};

Tim
 
That is how I declared e before but it does not like that syntax.
 
CaptRage, take a look at this again.

Your initial syntax is not ok. Tim's is.

Anyway, what kind of array do you want to create and which data do you want to store in it? I still don't understand it fully.

Cheers,

Dian
 
I finally got it to create it correctly, I just kept on typing the same thing over and over and the error finally went away.

I want to create an array of Elements that contain the 5 different variables that I have in my Element class. This array will be used as the data to go into a JTable there will be a column for the name, the symbol, the attomic number, the weight and the boiling point.

The goal is to get the data from a file that stores all this information then have it displayed in a table and be able to sort the elements by any of the variables.
 
You might consider sub-classing the AbstractTableModel and using it instead of the array with your JTable.

It can contain a collection of Element objects. You use the getColumnName() method to return the headings, and getValueAt() to return each column value.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top