I have an ArrayList<AbstractDrawablePoint> nodePoints which changes dynamically/can have anywhere from 1 to n number of points within it.
I would like to have a JTable fill dynamically based on the number of points within nodePoints. Currently, I can hardcode an Object [][] to do so like the following:
String[] columnNames = {"u", "v"};
Object[][] nodeData = new Object[][] {
{nodePoints.get(0).getU(), nodePoints.get(0).getV() },
{nodePoints.get(1).getU(), nodePoints.get(1).getV() }
};
JTable table = new JTable(nodeData, columnNames);
However, I would like to fill nodeData dynamically instead of hard coding like I did.
Based on #g00se's answer I came up with the following:
String[] columnNames = {"u", "v"};
DefaultTableModel tableModel = new DefaultTableModel(columnNames,0);
for (AbstractDrawablePoint node: nodePoints) {
Object [] currentNode = {node.getU(),node.getV()};
tableModel.addRow(currentNode);
}
JTable table = new JTable(tableModel);
Related
I want to remove this code and set to default values in the table
model2.setColumnIdentifiers(new
Object[]{"Order_ID","Description","Quantity","Amount","Order_Time","Cost"});
public void finduserday(){
ArrayList<dayday> list = userlistdaysearch (((JTextField)jDateChooser_searchdate.getDateEditor().getUiComponent()).getText());
DefaultTableModel model2 = new DefaultTableModel();
model2.setColumnIdentifiers(new Object[]{"Order_ID","Description","Quantity","Amount","Order_Time","Cost"});
Object [] row= new Object[6];
for(int i=0;i<list.size();i++){
row[0]=list.get(i).getOrder_ID();
row[1]=list.get(i).getDescription();
row[2]=list.get(i).getQuantity();
row[3]=list.get(i).getAmount();
row[4]=list.get(i).getOrder_Time();
row[5]=list.get(i).getCost();
model2.addRow(row);
}
jTable_dayday.setModel(model2);
}
i am writing in binary file objects by gui and when i list them in table only one line appears like this
not all data appers
and here is the code to list
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
Object rowData[] = new Object[5];
model.setRowCount(0);
Apparels a = new Apparels();
ArrayList<Apparels> app = new ArrayList<Apparels>();
app = a.listApparels();
for (Apparels x : app) {
rowData[0] = x.getStockid();
rowData[1] = x.getPricePerItem();
rowData[2] = x.getQuantity();
rowData[3] = x.getType();
rowData[4] = x.getCateogryname();
model.addRow(rowData);
}
You're re-using rowData to fill the properties, which means that for each row, it will have the same data
Maybe try using something more like...
for (Apparels x : app) {
Object rowData[] = new Object[5];
rowData[0] = x.getStockid();
rowData[1] = x.getPricePerItem();
rowData[2] = x.getQuantity();
rowData[3] = x.getType();
rowData[4] = x.getCateogryname();
model.addRow(rowData);
}
The primary issue is, the reference to rowData never changes, but the content in each element does, so once you've finished looping the, model will have a list of references all pointing to the same instance of rowData
I Have a hash Table
hashtable c = new Hashtable();
Employee emp = new Employee("E1001","Sky");
c.put("E1001",emp);
Then I have a JTable
Object[][] data = {
{"", ""},
};
String[] headers = {"Employee Code", "Employee First Name"};
JTable table = new JTable(data, headers);
I Cant seem to Figure out how to add the hashtable items into the JTable
If I'm reading this right, something like...
Object[][] data = new Object[c.size()][2];
int row = 0;
for (Object key : c.keySet()) {
data[row][0] = key;
Employee emp = (Employee)c.get(key);
data[row][1] = ...; // Get name from Employee object...
// Personally, I prefer to assign the Employee object to
// the column of the row and use a TableCellRenderer to
// renderer it
}
String[] headers = {"Employee Code", "Employee First Name"};
JTable table = new JTable(data, headers);
Should work...
Now, if you want a stronger relationship between the HashMap and TableModel (so you could add content to the table and it would update the HashMap), you're going to need to use an AbstractTableModel and get your hands dirty mapping the content between the requirements of the model and the HashMap
I'm trying to populate a table with data from a database however i am having some issues with it. Could someone provide me with an example? (so the table takes in an Object[][] parameter for the data). I have the following basic code to display a table ;
class table extends JFrame
{
JTable table;
public table()
{
setLayout(new FlowLayout());
String[] columnNames = {"test","test","test"};
Object[][] data= {{"test","test","test"},{"test","test","test"}};
table = new JTable(data,columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500,100));
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
}
Two years ago, during my time in technical school, I wrote a little library help solve some of the problems proposed by the exercises, which included a a DatabaseTableModel.
The class extends from AbstractTableModel, which means you can set it as the your JTable's data source.
Here's the algorithm that constructs a model from a ResultSet:
public final void constructModel(ResultSet rs) throws SQLException {
ResultSetMetaData rsmd = rs.getMetaData();
rs.last();
rowCount = rs.getRow();
int columnCount = rsmd.getColumnCount();
// DatabaseColumn simply holds a name and a Class<?>.
columns = new DatabaseColumn[columnCount];
// This is the Object[][] array that you were talking about.
// It holds all the data from the ResultSet.
data = new Object[columnCount][rowCount];
for (int i = 0; i < columnCount; ++i) {
// Figure out the column name and type.
int j = i + 1;
String colName = rsmd.getColumnLabel(j);
Class<?> colClass = String.class;
try {
colClass = Class.forName(rsmd.getColumnClassName(j));
} catch (ClassNotFoundException ex) {
colClass = String.class;
}
columns[i] = new DatabaseColumn(colName, colClass);
// Get the data in the current column as an Object.
rs.beforeFirst();
for (int k = 0; rs.next(); ++k) {
data[i][k] = rs.getObject(j);
}
}
// Notify listeners about the changes so they can update themselves.
fireTableStructureChanged();
}
The class worked when I used it in school, but it isn't exactly production code. When I look at it today, I start to see problems.
One problem is that it is loading the entire contents of the ResultSet into memory. Could get ugly pretty quickly.
Also, the algorithm isn't exactly optimal. It loops around with the database cursor as if it was nothing; I suppose that it would be less costly for the database if it had retrieved all the objects in the current row first and assigned them to their appropriate columns before moving on to the next row.
Nevertheless, I think it is a good enough starting point.
I am trying to add values to a Jtable, the values are fetched from arrayList,
How do you do that
I tried making Object[][] data; and the populate it inside a loop, but it does not work, How do you fix this?
String[] columns = {"Field String","Field Double"," Field Double"};
Object[][] data;
Iterator<Node> itr = arrayList.iterator();
while (itr.hasNext()) {
Node el = itr.next();
double a = el.getval();
data[i][1] = el.getstring();
data[i][2] = a;
data[i][3] = a*4;
i++;
}
JFrame frame = new JFrame("Title ");
JTable tablE = new JTable(data, columnas);
JPanel panel = new JPanel();
panel.add(table);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
How do you populate "data" inside a while loop?
Use a DefaultTableModel and add rows of data using its addRow(Object[]) or addRow(Vector) method. Set this as your JTable's model. The API and the JTable tutorial can get you started.
For e.g.,
ArrayList arrayList = new ArrayList();
String[] columns = {"Field String","Field Double"," Field Double"};
DefaultTableModel model = new DefaultTableModel(columns, 0);
for (Object item : arrayList) {
Object[] row = new Object[3];
//... fill in row with info from item
model.addRow(row);
}
JTable table = new JTable(model);
This demonstrates doing it with a for loop, but a while loop would be similar.