Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 883311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:34:02+00:00 2026-05-15T12:34:02+00:00

This is driving me crazy. I read the Sun’s tutorial regarding the creation of

  • 0

This is driving me crazy. I read the Sun’s tutorial regarding the creation of a basic table with a default data model, but cant figure out a simple example about how to load an array of data-objects like:

class dataObject{
String name;
String gender;
Byte age;

public dataObject (String name, String gender, Byte age){
   this.name = name;
   .
   .

}

Then i create, for example, a vector of this stuff:

Vector v = new Vector(99);

v.addElement(new dataObject("Marrie", "Female", 33);
v.addElement(new dataObject("John", "Male", 32);

With dataObject i’d gather the info, now how the heck i show it in a table? Because this is not working:

JTable newTable = new Jtable(v, header) // header is another Vector.

I’m getting some errors that lead me to this last line. So, any help, even little, is apreciated. I know there are several threads about this, but those people already have a gasp about how JTable + TableModel works, I just barely get it.

Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:34:03+00:00Added an answer on May 15, 2026 at 12:34 pm

    There are two ways you can create a JTable with a basic, prepared dataset:

    1. a 2D Object array
    2. a Vector whose elements are Vector

    so you can do this:

     Object [][] model = {{"Marrie", "Female","33"},{"John","Male","32"}};
     JTable table = new JTable(model);
    

    or you could do this:

     Vector model = new Vector();
     Vector row = new Vector();
    
     row.add("Marrie");
     row.add("Female");
     row.add("33");
     model.add(row);
    
     row = new Vector();
     row.add("John");
     row.add("Male");
     row.add("32");
     model.add(row);
    
     JTable table = new JTable(model);
    

    The next step would be to implement your own TableModel to utilize the DataObject class that you have put together (note that Java classes start with caps). Extending AbstractTableModel makes life easy, as you only need to implement three methods to get started:

    public int getRowCount();
    public int getColumnCount();
    public Object getValueAt(int row, int column);
    

    the first two are easy, you can get the size of your Vector for row count and hard-code the val for column count. getValueAt is where you pull the data from your DataObject

    Here is an example using an anonymous class, extending AbstractTableModel.

    final Vector<DataObject> myDataObjects = new Vector<DataObject>();
    myDataObjects.add(...);// add your objects
    JTable table = new JTable(new AbstractTableModel() {
    
        public int getRowCount() {return myDataObjects.size();}
        public int getColumnCount() { return 3; }
        public Object getValueAt(int row, int column){
             switch (column) {
               case 0:
                  return myDataObjects.get(row).getName();
               case 1:
                  return myDataObjects.get(row).getGender();
               case 2:
                  return myDataObjects.get(row).getAge();
               default:
                  return "";
             }
        }
    });
    

    I have kept the Vector so as to keep it close to your current implementation. You can easily change that to an ArrayList in this example without any worries.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using .Net 2.0 and this is driving me crazy but there's probably some
This is driving me crazy! I have read at least 5 questions on here
This is driving me crazy, I am trying to do something like: $this->data =
I'm sure I'm doing something silly, but this is driving me crazy. I'm trying
This is driving me crazy. I have this one php file on a test
This is driving me crazy. I'm not going to take it anymore. I'm going
This has been driving me crazy for a few days. Why doesn't the following
Total newbie question but this is driving me mad! I'm trying this: myInt =
This is driving me nuts. I am using some 3rd-party code in a Windows
Ok, this is driving me nuts. I've done just about everything I can to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.