0

I am goint to write a method, say public static JTable getJTableFromArrayList(ArrayList list), that can easily build a JTable from ArrayList to compare various of datas. This ArrayList is supposed to held any type of Object.

What I can come up with now is to use Gson.

  1. Use Gson.toJsonTree(Object src) method to parse the ArrayList<T> src into a JsonElement.

  2. Build JsonObject[] from the JsonElement

  3. Use JsonObject.entrySet() to get a Set<Map.Entry<String,JsonElement>>

  4. Then build the JTable from a self-defined AbstractTableModel, while the row s and columns and titles are got from the Set.

Before I start it, I want to know if there is a simpler and more direct way to do this rather than play with Gson? And also, if others have accomplished this already, I would be glad to use ready-made tools.

bijiDango
  • 1,385
  • 3
  • 14
  • 28

3 Answers3

2

As shown here, let your TableModel contain the Map and use the keySet() to index the rows in your implementation of getValueAt().

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

Don't know what GSON is but I don't think you need to convert your data.

You can use a TableModel that uses an ArrayList to hold custom Objects.

The Row Table Model does most of the work for you. You will just need to implement a couple of methods (instead of creating an entire TableModel) to identify the columns of data that you want to see from your custom object. The JButtonTableModel.java source is a complete example that shows how you might do this.

I don't know how to get the field names

Then maybe you can use the Bean Table Model which does all the work for you and uses reflection to create the TableModel.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I knew that some other guys should have done this already, I read the page and the models seems rather good. Now I will have to change the `T` to a bean, but this is really not a problem. I hope I can up these answer 10 times. – bijiDango Dec 11 '13 at 07:51
1

Use Gson.toJsonTree(Object src) method to parse the ArrayList src into a JsonElement.

Just adapt the ArrayList<T> to the TableModel instead of converting the list to Gson.

Use JsonObject.entrySet() to get a Set<Map.Entry<String,JsonElement>>

A TableModel needs it's elements in a predictable order, because it can access them by the row index. Thus you have to find a way how to map the row indexes to the set's elements. But if you just adapt the ArrayList to the TableModel this will also be no issue.

It seems that you are looking for a TableCellRenderer, because you said that you want to display arbitrary objects the way like Gson creates the element tree. So I would implement it this way:

class JsonTableCellRenderer extends DefaultTableCellRenderer {

   private String[] propertyColumns;
   private Gson gson;

   public JsonTableCellRenderer(String... propertyColumns) {
    this.propertyColumns = propertyColumns;
    gson = new Gson();
   }

   public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
    JsonElement element = gson.toJsonTree(value);
    if (element instanceof JsonObject) {
        JsonObject jsonObject = (JsonObject) element;
        String propertyName = propertyColumns[column];
        JsonElement propertyElement = jsonObject.get(propertyName);
        String propetyValue = propertyElement.toString();
        return super.getTableCellRendererComponent(table, propetyValue, isSelected, hasFocus, row, column);

    }
        // implement this behavior, e.g throw exception
    return null;
}

}
René Link
  • 48,224
  • 13
  • 108
  • 140
  • The problem is I am facing several `ArrayList` and different `T`. I don't know how to get the field names and there many other problems. It won't be problem if I just want to show a certain ArrayList in Jtable. – bijiDango Dec 11 '13 at 07:14
  • 1
    @bijiDango ok so your problem is that you want array lists with arbitrary object types to be displayed in the JTable. The way they get displayed is controlled by a `TableCellRenderer`. Why not implementing a `JsonTableCellRenderer`? I updated my answer. – René Link Dec 11 '13 at 07:33
  • Camickr's `Bean Table Model` is exactly what I want. You method is good though. But here you need `String... propertyColumns` to function well. – bijiDango Dec 11 '13 at 08:02