Question:
How/what do I need to do to get java-ascii-table to display testObject's field values in given context?
Background:
This is a small program I build to test a 'Displayer class' I've been working on. In the application that I am building/test Displayer for, I'm reading data from a .csv, then assigning this to instances of Product & storing these instances in an ArrayList (it's like an inventory).
On this current iteration I'm using java-ascii-table. This small test program recreates my basic need: to create a table displaying, the field values (ID, name, category, price) of objects held in an ArrayList.
Information on java-ascii-table can be found here:
https://code.google.com/p/java-ascii-table/
And here:
http://bethecoder.com/applications/products/asciiTable.action
This is the example I'm basing my code off of (It's the 5th example on the first link):
//Example5
//The following example shows rendering the ASCII Table from list of java beans.
Employee stud = new Employee("Sriram", 2, "Chess", false, 987654321.21d);
Employee stud2 = new Employee("Sudhakar", 29, "Painting", true, 123456789.12d);
List<Employee> students = Arrays.asList(stud, stud2);
IASCIITableAware asciiTableAware =
new CollectionASCIITableAware<Employee>(students,
//properties to read
"name", "age", "married", "hobby", "salary");
ASCIITable.getInstance().printTable(asciiTableAware);
asciiTableAware =
new CollectionASCIITableAware<Employee>(students,
//properties to read
Arrays.asList("name", "age", "married", "hobby", "salary"),
Arrays.asList("STUDENT_NAME", "HIS_AGE")); //custom headers
ASCIITable.getInstance().printTable(asciiTableAware);
//It prints the following tables in the console.
+----------+-----+---------+----------+----------------+
| NAME | AGE | MARRIED | HOBBY | SALARY |
+----------+-----+---------+----------+----------------+
| Sriram | 2 | false | Chess | 987,654,321.21 |
| Sudhakar | 29 | true | Painting | 123,456,789.12 |
+----------+-----+---------+----------+----------------+
+--------------+---------+---------+----------+----------------+
| STUDENT_NAME | HIS_AGE | MARRIED | HOBBY | SALARY |
+--------------+---------+---------+----------+----------------+
| Sriram | 2 | false | Chess | 987,654,321.21 |
| Sudhakar | 29 | true | Painting | 123,456,789.12 |
+--------------+---------+---------+----------+----------------+
My Code:
Main
create instance of ArrayListMaker, calls methods in Displayz
This is the method in question:
- Displayz.displayProduct2(arrayListMaker);
This simply displays a 'logo', not important:
- Displayz.displaySurvivalStoreLogo();
Code
package playGround2;
public class Main {
public static void main(String[] arg) {
ArrayListMaker arrayListMaker = new ArrayListMaker();
Displayz.displayProduct2(arrayListMaker);
Displayz.displaySurvivalStoreLogo();
}
}
ArrayListMaker
Each instance of ArrayListMaker has it's own ArrayList, testObjectsList. testObjectsList is an ArrayList of instances of TestObject.
package playGround2;
import java.util.ArrayList;
public class ArrayListMaker {
public ArrayList<TestObject> testObjectsList;
public ArrayListMaker() {
testObjectsList = new ArrayList<TestObject>();
testObjectsList.add( new TestObject("11","One", "This", "10"));
testObjectsList.add( new TestObject("12", "Two", "That", "20"));
testObjectsList.add( new TestObject("13", "Three", "Other", "30"));
testObjectsList.add( new TestObject("14", "four", "something", "40"));
testObjectsList.add( new TestObject("15", "five", "else", "50"));
testObjectsList.add( new TestObject("16", "six", "over-there", "60"));
testObjectsList.add( new TestObject("17", "seven", "Who", "70"));
testObjectsList.add( new TestObject("18", "eight", "Why", "80"));
}
public ArrayList<TestObject> getTestObjects() {
return this.testObjectsList;
}
}
TestObject
POJO. Fields for: ID, name, catagory, price...setters, getters, etc...
package playGround2;
public class TestObject {
private String ID;
private String name;
private String category;
private String price;
/********************constructors********************/
public TestObject() {
// TODO Auto-generated constructor stub
}
public TestObject(String ID, String name, String category, String price) {
this.setID(ID);
this.setName(name);
this.setCategory(category);
this.setPrice(price);
}
/********************get & set********************/
/**********ID**********/
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
/**********name**********/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**********category**********/
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
/**********price**********/
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
Displayz
Has methods for displaying data(& a 'logo').
This is the method I need to create the table. I have written some code based the above example. But since this is new to me, I might be way off.
- displayProduct2()
Code
package playGround2;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.bethecoder.ascii_table.ASCIITable;
import com.bethecoder.ascii_table.impl.*;
import com.bethecoder.ascii_table.spec.*;
public class Displayz {
public static void displaySurvivalStoreLogo() {
BufferedImage bufferedImage = new BufferedImage(144, 32, BufferedImage.TYPE_INT_RGB);
Graphics graphics = bufferedImage.createGraphics();
graphics.setFont(new Font("Dialog", Font.PLAIN, 24));
Graphics2D graphics2d = (Graphics2D) graphics;
graphics2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics2d.drawString("SurvivalStore", 6, 24);
try {
ImageIO.write(bufferedImage, "png", new File("text.png"));
}
catch (IOException e) {
e.printStackTrace();
}
for (int y = 0; y < 32; y++) {
StringBuilder stringBuilder = new StringBuilder();
for (int x = 0; x < 144; x++) {
stringBuilder.append(bufferedImage.getRGB(x, y) == -16777216 ? " " : bufferedImage.getRGB(x, y) == -1 ? "#" : "*");
}
if (stringBuilder.toString().trim().isEmpty()) {
continue;
}
System.out.println(stringBuilder);
}
} //end of displaySurvivalStore
public static void displayProduct2(ArrayListMaker arrayListMaker) {
IASCIITableAware asciiTableAware = new CollectionASCIITableAware<TestObject>(arrayListMaker.getTestObjects(),"ID", "name", "category", "price");
ASCIITable.getInstance().printTable(asciiTableAware);
// In this argument(Arrays.asList("name", "category", "price")), Arrays in underlined in red
asciiTableAware = new CollectionASCIITableAware<TestObject>(arrayListMaker.getTestObjects(), Arrays.asList("ID", "name", "category", "price"), ASCIITable.getInstance().printTable(asciiTableAware);
}
}