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
Related
Trying to add a column name to the table but it is not coming. only data column is coming in jtable. please help
code
String [][] data = {{"", ""}, {"", ""}};
String [] column = {"We", "Did"};
DefaultTableModel model = new DefaultTableModel(data,column);
jt = new JTable(model);
jt.setBounds(100, 100, 500, 200);
ta1.add(jt);
It is only showing empty which is for data but for column it is not showing.
String[] headers= {"header1","header2,....};
model.setColumnIdentifiers(headers);
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);
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 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.