How do I dynamically display contents in tables in Java - java

I need to display some content in a tabular form dynamically in Java. The content includes data that is fetched from an API in JSON format. At the end of each row I need to display a checkbox as well. The number of rows is dynamic and the columns are fixed. How do I do this?
The table will have the following columns:
Index
Username
Upload date
Percentage
Matched results
[Checkbox]

Finally found the answer to this problem. We can dynamically create a table using the class DefaultTableModel and the Java swing class JTable.
model = new DefaultTableModel();
Set the columns
model.setColumnIdentifiers(new Object[]{"Index","User","Reg no.","Match"});
jTable1.setModel(model);
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
jTable1.setFillsViewportHeight(true);
jScrollPane1.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jScrollPane1.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
Add the rows from the JSON response
for(int i=0;i< jsonArr.size();i++)
{
str = jsonArr.get(i).toString();
jsonObj = (JSONObject)parser.parse(str);
String sub = jsonObj.get("subid").toString();
String uname = jsonObj.get("username").toString();
String regno = jsonObj.get("regno").toString();
String percent = jsonObj.get("percent").toString();
model.addRow(new Object[]{sub,uname,regno,percent});
}

Related

Convert text to a Hyperlink using CSV writer in java

I have a java class using csv writer which creates csv file.As know everything will be in text format,I want a specific column data to be a hyperlink.For example consider a table with 1 rows and 4 columns
OUTPUT is : when opened in excel .Each entry will be in a new cell
Col1 Col2 Col3 Col4
OUTPUT when opened in Open Office
Col1,Col2,Col3,Col4
The above data is when I open my csv file in excel.This creation is done by my csv writer in java.
What I wanted here is that Col3 should be a hyperlink
Below is my code
public String[] convertEntry(Info ItemInfo) {
String[] columns = new String[NUMBER_OF_COLUMNS];
columns[1] = "Col1;
columns[2] = "Col2";
columns[3] = "Col3";
columns[4] = "Col4";
return columns;
}
Hope am clear.Any help is appreciated
It's ugly but:
this,is,a,=HYPERLINK("http://www.google.com"),test,csv
Will automatically create it. Otherwise you can turn on the configuration for autocompletion in Excel so they always change. But CSV is supposed to be text, I would suggest leaving it as just the URL and having the user know what they are doing to change it.
EDIT:
public String[] convertEntry(Info ItemInfo) {
String[] columns = new String[NUMBER_OF_COLUMNS];
columns[1] = "Col1;
columns[2] = "Col2";
columns[3] = "=HYPERLINK(" + ItemInfo.Col3 + ")";
columns[4] = "Col4";
return columns;
}
Just wrap the value in the =HYPERLINK() function and it will automatically be a link in excel.

How to add CheckBox object in table in JAVA Swing

I am having a table which is getting data from database. But I want to add a row with checkbox having attributes as name But everytime I run the program it show the value as
javax.swing.JCheckBox[ , 0, 0, 0x0, invalid, alignmentX = 0.0, alignmentY = 0.5, border = java................
Here is the code.
while(rs.next()) {
Vector row = new Vector();
String name = rs.getString("name");
String catid = rs.getString("catalogid");
String brand = rs.getString("brand");
String counter = rs.getString("counter");
String qty = rs.getString("qty");
String price = rs.getString("column_price");
row.add(name);
row.add(catid);
row.add(brand);
row.add(counter);
row.add(qty);
row.add(price);
cb = new JCheckBox(name, true);
row.add(cb);
model.addRow(row);
}
You don't add components to the TableModel of a JTable. You add data and use renderers to render the data.
So in your case you need to:
add Boolean.TRUE as the data to the TableModel.
override the getColumnClass(...) method of the TableModel to return Boolean.class so the table can render the Boolean object as a check box.
Read the Swing tutorial on How to Use Tables for more information and examples to get you started.

Appending a new row to a JTable at runtime

I am developing a Java application that uses a JTable. I want to allow the user to enter data in a JTable that can later be printed or saved. The issues is: Suppose the user has entered some data and the cursor is on the last cell of the row in a table (I have uploaded an image-suppose the cursor is on the highlighted cell).How do I make it in such a way that when a user presses the Enter Button(Keyboard button) the application will add/append a new row that is empty to the table so that the user can fill in other data.
Attachment
Try this...
JTable table = new JTable();
DefaultTableModel tblModel = new DefautlTableModel(0,0);
table.setModel(tblModel);
//an action or click then you should run this code
tblModel.addRow(new Object[]{""});
DefaultTableModel tbm = (DefaultTableModel) jTable1.getModel();
Vector rowData = null;
tbm.addRow(rowData);

Reading a Access file using Jackcess and creating a Jtable with the data

I'm currently working on a Java application that reads an Access file and builds a Jtable model using the data that's collected. I've previously done the same with an Excel file but when I tried with Jackcess it was slightly diffrent and I've ran into some questionmarks.
My work so far:
public class AccessModel{
public DefaultTableModel getAccessModel() throws IOException {
Database db = DatabaseBuilder.open(new File("MyFile.accdb"));
Vector<String> columnNames = new Vector<String>();
Vector<String> vector = new Vector<String>();
Vector<Vector<String>> data = new Vector<Vector<String>>();
StringBuilder output = new StringBuilder();
Table table = db.getTable("Table1");
for (Column column : table.getColumns()) { // get the table column names
output.append(column.getName());
output.append("\n");
columnNames.add(column.getName());
}
for (Column column : table.getColumns()) { // get the column rows and values
vector.add(column.getRowValue(table.getNextRow()).toString());
}
data.add(vector);
// return the model to Gui
DefaultTableModel accessModel = new DefaultTableModel(data, columnNames);
return accessModel;
}
}
As you can see this method will only iterate trough the first row, then exit the loop. I'm either blind to an abvious solution due to 12 hours of straight work, or I'm doing something terribly wrong.
I've stumbled across some half-good solutions where an Iterator is used, but I cannot get the hang of it. Any suggestions on this? Or should I stay on lane with my current line of thought?
JTable (value for view is stored in XxxTableModel, in your case is used DefaultTableModel) is row bases Object,
TableColumn (value is stored in TableColumnModel) to divide row(s) to the columns
you would need to create two Objects,
Vector<String> columnNames (is only one row) for columns identifiers from Table table = db.getTable("Table1");
loop inside Table table = db.getTable("Table1"); to fill two dimensional Vector<Vector<Object>> data = new Vector<Vector<Object>>(); by using Vector<Object> vector = new Vector<Object>();, notice 1st. code line insode loop must be vector = new Vector<Object>();, you have to create a new Vector otherwise you'll add the same rown_times, last code line should be data.add(vector)
.
everything (I'm still think so) is described in Oracle tutorial How to use Tables

Dynamically add images to JTable cells

I am dynamically adding data to a cell with the following code:
for(int i = 0; i < matchedSlots.size(); i++)
{
String title = matchedSlots.get(i).getTitle();
String director = matchedSlots.get(i).getDirector();
int rating = matchedSlots.get(i).getRating();
int runTime = matchedSlots.get(i).getRunningTime();
DefaultTableModel tm = (DefaultTableModel) searchResults.getModel();
tm.addRow(new Object[] {title,director,rating,runTime});
}
what do I need to add to the above to be able to add an image in the first cell of each row
By default JTable can render Images. You just need to override getColumnClass() in the TableModel and return Icon.class for 1st column.
Look at Renderers and Editors for more details.
ImageIcon image = new ImageIcon("image.gif");
...
tm.addRow(new Object[] {image,title,director,rating,runTime});
You may need to change your table model to account for the new column if you haven't already.
This short article should help you with the image renderer: http://mdsaputra.wordpress.com/2011/06/13/swing-hack-show-image-in-jtable/

Categories