Hey Guys,
im struggling to load and display my .csv the right way. I have 4 columns, each with 10 words and I want to display each column in one textbox. (I found that textbox example here on the forums)
I know how to display the .csv but it won’t work when it comes to the textboxes, I always see just one word instead of all ten. here is the code im working on:
ArrayList<TextBox> textBoxes = new ArrayList<TextBox>();
float scrollSpeed = 10;
PFont font;
//------------------
String [] word;
String [] word1;
Table table;
float yPos=100;
int i;
void setup()
{
size(1280, 720, P2D);
smooth();
table = loadTable("01_11_biden.csv", "header");
for (TableRow row : table.rows()) {
String nomen = row.getString("Nomen");
String adjectiv = row.getString("Adjektiv");
nomen = nomen.toLowerCase();
adjectiv = adjectiv.toLowerCase();
word = splitTokens(nomen, "\n ");
word1 = splitTokens(adjectiv, "\n ");
//____how it works to display all ten words
// text(word[i], 100,yPos);
// text(word1[i], 100+100, yPos);
// yPos=yPos + 20;
}
TextBox textBox1 = new TextBox(20, 0, 200, height);
textBox1 .setTextCol (color(0,0,0));
textBox1 .setFont (createFont("basiersquare-medium-webfont.ttf", 30));
textBox1 .setMargin (10, 10);
textBox1 .text = word[i];
TextBox textBox2 = new TextBox(220, 0, 200, height);
textBox2 .setTextCol (color(0,0,0));
textBox2 .setFont (createFont("basiersquare-medium-webfont.ttf", 30));
textBox2 .setMargin (10, 10);
textBox2 .text = word1[i];
textBoxes.add(textBox1);
textBoxes.add(textBox2);
}
void draw()
{
background(255);
for (TextBox textBox : textBoxes)
{
textBox.draw();
}
}
void mouseWheel(MouseEvent event)
{
for (TextBox textBox : textBoxes)
{
if (textBox.isInside(mouseX, mouseY))
{
textBox.scroll(event.getCount()*scrollSpeed/2);
}
}
}
public class TextBox
{
public String text = "";
private PGraphics buffer = new PGraphics();
private PVector pos = new PVector();
private PVector size = new PVector();
private PVector margin = new PVector();
private float scroll = 0;
private color backCol = color(255,255,255,0);
private color edgeCol = color(255,255,255,0);
private color textCol = color(0);
private PFont font = new PFont();
public PVector getPos() {return pos;}
public PVector getSize() {return size;}
public void setPos (PVector pos) {setPos(pos.x, pos.y);}
public void setPos (float x, float y) {pos.x = x; pos.y = y;}
public void setMargin (PVector margin) {setMargin(margin.x, margin.y);}
public void setMargin (float w, float h) {margin.x = w; margin.y = h;}
public void scroll (float scroll) {this.scroll = max(this.scroll+scroll, -300); this.scroll = min(this.scroll+scroll,+390);} // Scroll cannot go below 0.
public void setBackCol (color c) {this.backCol = c;}
public void setEdgeCol (color c) {this.edgeCol = c;}
public void setTextCol (color c) {this.textCol = c;}
public void setFont (PFont font) {this.font = font;}
public void draw()
{
buffer.beginDraw();
{
buffer.clear();
buffer.background (backCol);
buffer.stroke (edgeCol);
buffer.fill (backCol);
buffer.rect (0, 0, buffer.width-1, buffer.height-1); // Border.
buffer.textFont (font);
buffer.fill (textCol);
buffer.textAlign (LEFT, CENTER);
buffer.text (text, margin.x, margin.y-scroll, buffer.width-margin.x, buffer.height+scroll);
}
buffer.endDraw();
image(buffer, pos.x, pos.y);
}
public boolean isInside(PVector checkPos) {return isInside(checkPos.x, checkPos.y);}
public boolean isInside(float checkPosX, float checkPosY)
{
boolean inWidth = checkPosX > pos.x && checkPosX < pos.x+size.x;
boolean inHeight = checkPosY > pos.y && checkPosY < pos.y+size.y;
return inWidth && inHeight;
}
public TextBox(PVector size, PVector pos) {this(pos.x, pos.y, size.x, size.y);}
public TextBox(float x, float y, float w, float h)
{
pos.x = x; pos.y = y; size.x = w; size.y = h;
buffer = createGraphics(floor(w), floor(h), P2D);
}
}
as you may see when I call:
textBox1 .text = word[I];
it will display just one word in the textbox.
I feel like I need to find another way to write this line with the yPos cause when I call this (without the textboxes) I see all ten words I want to see
text(word[i], 100,yPos);
yPos=yPos + 20;
but I didn’t find any solution to do so.
I tried also to use this method to load the table, but it didnt really change anything:
String lines[] = loadStrings("01_11_biden.csv");
String [][] csv;
int csvWidth=0;
String[] prep;
//calculate max width of csv file
for (int i=0; i < lines.length; i++) {
String [] chars=split(lines[i],',');
if (chars.length>csvWidth){
csvWidth=chars.length;
}
}
//create csv array based on # of rows and columns in csv file
csv = new String [lines.length][csvWidth];
//parse values into 2d array
for (int i=0; i < lines.length; i++) {
String [] temp = new String [lines.length];
temp= split(lines[i], ',');
for (int j=0; j < temp.length; j++){
csv[i][j]=temp[j];
}
}
prep = new String [lines.length];
for (int i=1; i < lines.length; i++) {
if (csv[i][1] != null) {
prep = split(csv[i][2], " ");
println(prep);
}
}
as above, the code is working but I don’t know how to use it in combination with the textbox I have
I feel like Im just missing one simple line of code. maybe an if statement or an for loop?
I already tried something like:
if (i < 0) {
yPos=yPos+20;
}
but thats not working. do you maybe know what im missing and can give me a hint to the right direction?