0

I am having a problem initializing a JTable from a FOR with BD or an array.

My problem is the example tableFilterDemo.java, I need the funcionality of this, but when I want load data of my BD or an arraylist I have the problem.

I need load the array of objects with a FOR getting all the lines of file or rows of table

private Object[][] data = {
    { "Mary", "Campione", "Snowboarding"},
    { "John", "guifru", "skyiin"},};
4
  • 2
    What does "FOR" or "BD" mean? Commented Aug 11, 2011 at 14:48
  • This question is very unclear. What problem are you having? Post the error. Commented Aug 11, 2011 at 14:55
  • BD (database), for(int i = 0; i; i++) Commented Aug 11, 2011 at 14:56
  • 1
    Quit cross posting: coderanch.com/t/548958/GUI/java/Initialize-table-array-objects Commented Aug 11, 2011 at 15:01

3 Answers 3

5

You can always use the DefaultTableModel which uses a Vector of Vectors to hold the data in the TableModel. So for each row in the table you create a Vector and add each column to the Vecter. Then you add the row Vector to a second Vector. This way you don't need to hardcode the size of the Arrays in advance. Table From Database shows how you can use this approach.

Or you can always use a custom TableModel if you want to display custom Ojects that are stored in an ArrayList. The Row Table Model provides some general support for storing objects in an ArrayList. You would also want to look at the BeanTableModel for a full implementation and a further example of how to do a custom implementation.

Sign up to request clarification or add additional context in comments.

Comments

2

You have two ways:

  • you set the objects directly to the JTable by invoking the right constructor (which is JTable(Object[][] rowData, Object[] columnNames)) and this is quite easy and good but just manages static tables
  • you use a model, like in the example you are talking about: you use a class that extends AbstractTableModel in which you override the right methods to map the bidimensional collection to the right behaviors.

Comments

1

I'm not sure that I have understood your problem. Anyway, if your table have 3 columns and you have defined the table model as defined here, you have to itrate your array and put the values in the table in this way:

for(int i=0; i < data.length; i++){
  for(int j=0; j < data[i].length; j++){
    table.setValueAt(data[i][j], i, j);
  }
}

This is the declaration of the method: setValueAt(Object aValue, int row, int column)

If you want to convert an arrayList to an array you can:

ArrayList<String> arrayList;
String[] data = new String[arrayList.size()];
data = arrayList.toArray(data);

Bye Luca

8 Comments

its ok but there are a problem data have not data, is empty. I need add to data the info from a database or arraylist
@Esteban c Can you give me the declaration of your arraylist?
fila[0] = rs.getString(1); fila[1] = rs.getString(2); fila[2] = rs.getString(3); fila[3] = rs.getString(4);
@Esteban c Are you Italian? Anyway is fila and array? String[] fila
@Estenan c I modified the answer but I'm not sure I understood your problem
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.