Filling Dinamyc Arrays[][]


#1

hello! i need some help to fill a two dimensional array in a dinamyc way. I want to add a new Cols in a JTable when and object is created, and fill the rows with the values of the object. for that i want to use a two dimensional array, but i dont know exactly how to fill it in a dinamyc way. the first [] correspond to columns, so the length is add one per object, and in the second [] i want the obj values


Object[][]a;

ArrayList<Obj>o = new ArrayList<Obj>();

void setup() {
}

void draw() {
  
}

void fillArray(){
  
}

void mousePressed() {
  o.add(new Obj(random(width), random(height)));
  
  
}

class Obj {

  float x;
  float y;

  Obj(float _x, float _y) {
    x = _x;
    y = _y;
  }
}

thanks!


#2

I didn’t know about that Java Swing class JTable before:

After a quick reading, I spotted 1 of its overloaded constructors had an option to use the container Vector:

So I decided to create a 2D Vector container to hold instances of Point2D.Float class:

Then I noticed that even though adding new Vector rows were working alright, it failed to account to the number of Point2D.Float objects in each of those rows!

That is, dynamically increasing the number of columns wouldn’t make the JTable to “see” them!

After some Internet research, I’ve found out that in order for the JTable to increase its column count I’d need to invoke its method addColumn(), passing a new instance of TableColumn to it specifying the index of the Vector container holding the column title:

So this is the JTable demo sketch I’ve come w/: :crazy_face:

/**
 * JTable Example (v1.0)
 * GoToLoop (2018/Dec/31)
 * https://Discourse.Processing.org/t/filling-dinamyc-arrays/7049/2
 */

// Imports for creating a frame:
import javax.swing.JFrame;
import javax.swing.JScrollPane;

// Imports for creating a table:
import javax.swing.JTable;
import javax.swing.table.TableColumn;

// Imports for creating the data grid of points:
import java.util.Vector;
import java.awt.geom.Point2D;

// Fields for holding the table and its data grid + column titles:
final Vector<Vector<Point2D.Float>> points = new Vector<Vector<Point2D.Float>>();
final Vector<String> titles = new Vector<String>();
final JTable table = new JTable(points, titles);

void setup() {
  noLoop();

  // Adds 3 rows of points to table:
  for (int i = 0; i < 3; ++i)  points.add(new Vector<Point2D.Float>());

  // Creates a separate frame to display the table:
  final JFrame f = new JFrame();
  f.setSize(1200, 300);
  f.add(new JScrollPane(table));
  f.setVisible(true);
}

void draw() {
  background((color) random(#000000));
}

void mousePressed() {
  redraw = true;

  final String title = str(frameCount - 2); // choose column title.
  titles.add(title); // adds a column title.
  table.addColumn(new TableColumn(table.getColumnCount()));

  // Adds an extra point column to each table row:
  for (final Vector<Point2D.Float> row : points) {
    final Point2D.Float point = new Point2D.Float(random(width), random(height));
    row.add(point);
  }
}

#3

thanks alot i was actually looking for this particular formula!!!


#4

!! thanks!! but actually i said it wrong… :sweat_smile:

my colums have the names of the variables

String [] columns = {"ID", "TYPE", "VISIBLE"};

and when i create a new object, i want a new ROW, not column, and fill the ROWS with those values…


#5

That makes the code lighter. No need for TableColumn this time. :sweat_smile:

Just a simple call to JTable::updateUI() instead after appending a new row to the grid container: :call_me_hand:
Docs.Oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/JTable.html#updateUI()

Here’s the new version of “JTable Example” that dynamically appends rows instead of columns: :wink:

/**
 * JTable Example II (v1.0.2)
 * GoToLoop (2019/Jan/01)
 * https://Discourse.Processing.org/t/filling-dinamyc-arrays/7049/5
 */

// Imports for creating a frame:
import javax.swing.JFrame;
import javax.swing.JScrollPane;

// Import for creating a table:
import javax.swing.JTable;

// Imports for creating the data grid:
import java.util.Vector;
import static java.util.Arrays.asList;

// Fields for holding the table and its data grid + column titles:
final Vector<Vector<Object>> grid = new Vector<Vector<Object>>();
final Vector<String> titles = new Vector<String>(asList("ID", "TYPE", "VISIBLE"));
final JTable table = new JTable(grid, titles);

void setup() {
  looping = false;

  // Creates a separate frame to display the table:
  final JFrame f = new JFrame();
  f.setSize(1200, 600);
  f.add(new JScrollPane(table));
  f.setVisible(true);
}

void draw() {
  background((color) random(#000000));
}

void mousePressed() {
  redraw = true;

  final Vector<Object> row = new Vector<Object>(titles.size()); // new row.

  row.add(frameCount - 2); // "ID"
  row.add(platformNames[(int) random(platformNames.length)]); // "TYPE"
  row.add(random(1) < .5); // "VISIBLE"

  grid.add(row); // appends new row to the data grid.
  table.updateUI(); // notifies that the table has changed & needs repainting.
}

#6

A Python Mode version as well: :snake:

"""
 * JTable Example II (v1.0)
 * GoToLoop (2019/Jan/08)
 * https://Discourse.Processing.org/t/filling-dinamyc-arrays/7049/6
"""

# Python's own random function imports:
from random import randint, choice

# Imports for creating a table and its frame:
from javax.swing import JTable, JFrame, JScrollPane
from javax.swing.table import DefaultTableModel

# Import for creating the data grid:
from java.util import Vector

# Fields for holding the table and its data grid + column titles:
grid, titles = Vector(), Vector(('ID', 'TYPE', 'VISIBLE'))
# table = JTable(grid, titles) # Not working! Calls wrong constructor sig!
table = JTable(DefaultTableModel(dataVector=grid, columnIdentifiers=titles))

def setup():
    noLoop()

    # Creates a separate frame to display the table:
    f = JFrame(size=(1200, 600))
    f.add(JScrollPane(table))
    f.show()


def draw():
    background(randint(PImage.ALPHA_MASK, 0))


def mousePressed():
    redraw()

    row = Vector(titles.size()) # new row.

    row.add(frameCount - 2) # 'ID'
    row.add(choice(platformNames)) # 'TYPE'
    row.add(random(1) < .5) # 'VISIBLE'

    grid.add(row) # appends new row to the data grid.
    table.updateUI() # notifies that the table has changed & needs repainting.