14 Replies - 364 Views - Last Post: 06 June 2014 - 06:12 AM Rate Topic: -----

#1 Andy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-February 14

JTABLE problem

Posted 05 June 2014 - 03:02 PM

Hey guys so i have a jtable which i want to put an array into... and Really dont know how.. I dont want to use array list mainly because it looks wayy to complicated so there must be some other way using defaultablemodel but i dont know how..

So here's my code: just a snippet which includes the testing of the jtable
        String[] columns = {"col1", "col2", "col3", "col4", "col5", "col6"};

        //declare 2d array
        String[] [] data = {{"1", "2", "3", "4", "5", "6"}};

        jt = new JTable (data, columns);
        jt.setPreferredScrollableViewportSize (new Dimension (50, 80));

        //set initial selectibility to false
        jt.setFocusable (false);
        jt.setRowSelectionAllowed (false);

        JScrollPane tableContainer = new JScrollPane (jt);
        frame.getContentPane ().add (tableContainer);


Is This A Good Question/Topic? 0
  • +

Replies To: JTABLE problem

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: JTABLE problem

Posted 05 June 2014 - 03:08 PM

What is the problem exactly? That code works fine for me.
Was This Post Helpful? 0
  • +
  • -

#3 Andy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-February 14

Re: JTABLE problem

Posted 05 June 2014 - 03:10 PM

View PostRyano121, on 05 June 2014 - 03:08 PM, said:

What is the problem exactly? That code works fine for me.


What i want to do is add a pre-declared String array into the jtable
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: JTABLE problem

Posted 05 June 2014 - 03:13 PM

Add it to the data 2d array then. Each array in that array corresponds to a row in the final table.

Object[][] data = {
    {"1", "2", "3", "4", "5", "6"}, // first row
    {"7", "8", "9", "10", "11", "12"}, // second row
}

Was This Post Helpful? 0
  • +
  • -

#5 Andy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-February 14

Re: JTABLE problem

Posted 05 June 2014 - 03:18 PM

View PostRyano121, on 05 June 2014 - 03:13 PM, said:

Add it to the data 2d array then. Each array in that array corresponds to a row in the final table.

Object[][] data = {
    {"1", "2", "3", "4", "5", "6"}, // first row
    {"7", "8", "9", "10", "11", "12"}, // second row
}


Ok so i have an array called numbers[] and i want to put it in the jtable. Im obviously not gonna put numbers[0], numbers[1], numbers [2]... and so on
I want to add this array using a for loop or something..
Was This Post Helpful? 0
  • +
  • -

#6 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: JTABLE problem

Posted 05 June 2014 - 03:22 PM

This will work just fine as well:

Integer[] numbers = {7, 8, 9 ,10, 11, 12};
Object[] [] data = {{"1", "2", "3", "4", "5", "6"}, numbers};


But I really still don't know exactly what you are wanting to do so you may need to explain a little more for me to help you better. At the moment I'm just shooting in the dark.

Other than that the DefaultTableModel also has a very handy insertRow method.
Was This Post Helpful? 0
  • +
  • -

#7 Andy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-February 14

Re: JTABLE problem

Posted 05 June 2014 - 03:31 PM

View PostRyano121, on 05 June 2014 - 03:22 PM, said:

This will work just fine as well:

Integer[] numbers = {7, 8, 9 ,10, 11, 12};
Object[] [] data = {{"1", "2", "3", "4", "5", "6"}, numbers};


But I really still don't know exactly what you are wanting to do so you may need to explain a little more for me to help you better. At the moment I'm just shooting in the dark.

Other than that the DefaultTableModel also has a very handy insertRow method.


Ok so what i have is this:

String[] columns = {"First Name", "Last Name", "Math", "Geography", "French", "Average"};

i am basically doing a markbook
now here, under first name and last name, there are a set number of strings which are stored in an array
under math, geography, french, and average, there are marks, meaning there is a set number of doubles which are stored in a double array

now when you say:
Integer[] numbers = {7, 8, 9 ,10, 11, 12};
Object[] [] data = {{"1", "2", "3", "4", "5", "6"}, numbers};


the entire row will become integeres, but i only want the first two columns to have names and the last 4 to have doubles.. how am i supposed to do that

And here is a picture of how the table looks like:
http://puu.sh/9gLby/90179f7e91.png
Was This Post Helpful? 0
  • +
  • -

#8 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: JTABLE problem

Posted 05 June 2014 - 03:45 PM

Ah ok I see. Yeah in that case you are much better of going down the Vector route (pretty much the same as an ArrayList). Its really not that bad and gives you a lot more flexibility.

Vector<Object> row = new Vector<Object>();
row.add(firstName);
row.add(lastName);
// then add the marks for this row using the add method again


Then you have one of two ways of adding it to the JTable:

1. Create a Vector of Vectors (Vector of row Vectors) and pass that to the constructor:

Vector<Vector> rows = new Vector<Vector>();
rows.add(row);

table = new JTable(rows, columns);


2. Add them dynamically to the DefaultTableModel after instantiation:

DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(row);

Was This Post Helpful? 0
  • +
  • -

#9 Andy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-February 14

Re: JTABLE problem

Posted 05 June 2014 - 03:56 PM

View PostRyano121, on 05 June 2014 - 03:45 PM, said:

Ah ok I see. Yeah in that case you are much better of going down the Vector route (pretty much the same as an ArrayList). Its really not that bad and gives you a lot more flexibility.

Vector<Object> row = new Vector<Object>();
row.add(firstName);
row.add(lastName);
// then add the marks for this row using the add method again


Then you have one of two ways of adding it to the JTable:

1. Create a Vector of Vectors (Vector of row Vectors) and pass that to the constructor:

Vector<Vector> rows = new Vector<Vector>();
rows.add(row);

table = new JTable(rows, columns);


2. Add them dynamically to the DefaultTableModel after instantiation:

DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(row);


am i supposed to replace the Vector variable with something? because i get an invalid assignment operator error when i compile
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,197
  • Joined: 20-September 08

Re: JTABLE problem

Posted 05 June 2014 - 04:09 PM

Vector<Vector<Object>> rows = new Vector<Vector<Object>>();
is what it needs to be
Was This Post Helpful? 0
  • +
  • -

#11 Andy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-February 14

Re: JTABLE problem

Posted 05 June 2014 - 05:25 PM

This is the error i get: invalid assignment operator
http://puu.sh/9gSws/dc5dcc02ff.png
Was This Post Helpful? 0
  • +
  • -

#12 Andy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-February 14

Re: JTABLE problem

Posted 05 June 2014 - 05:57 PM

View Postg00se, on 05 June 2014 - 04:09 PM, said:

Vector<Vector<Object>> rows = new Vector<Vector<Object>>();
is what it needs to be


Is it possible to do it without vectors?
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,197
  • Joined: 20-September 08

Re: JTABLE problem

Posted 05 June 2014 - 11:19 PM

Well, yes, with arrays. Check the available constructors in the API docs. But why do you want to?
Was This Post Helpful? 0
  • +
  • -

#14 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: JTABLE problem

Posted 06 June 2014 - 04:12 AM

You could do it without Vectors, but theres not a lot of point. You'll just be creating more work for yourself.

I don't know why you are getting that error either, Vector<Vector<Object>> rows = new Vector<Vector<Object>>(); compiles fine for me.

Are you using a really really old JDK? Try using Vector rows = new Vector(); instead. If that works then it seems you are using a really old version that doesn't support generics.
Was This Post Helpful? 0
  • +
  • -

#15 Andy123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 24-February 14

Re: JTABLE problem

Posted 06 June 2014 - 06:12 AM

View PostRyano121, on 06 June 2014 - 04:12 AM, said:

You could do it without Vectors, but theres not a lot of point. You'll just be creating more work for yourself.

I don't know why you are getting that error either, Vector<Vector<Object>> rows = new Vector<Vector<Object>>(); compiles fine for me.

Are you using a really really old JDK? Try using Vector rows = new Vector(); instead. If that works then it seems you are using a really old version that doesn't support generics.


Yeah for school we have to use ready for program java which is a really old program and is discontinued (so its not updated anymore)
Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1