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);
Related
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);
How do I insert a new blank line inside a PdfPTable column. \n and n number of spaces has not done the trick for me. This is the java code I am working with.
chtbc_report1_table.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"H.B.G", "Some Value", "lbs/kg", "Adult M : 120lbs \n Adult F : 90lbs"},
.....
.....
//Other rows
},
new String [] {
"Investigation", "Result", "Unit", "Weight"
}
));
I want to put a new line between "Adult M : 120lbs **\n** Adult F : 90lbs"
UPDATE
This is the code I have used for creating a PdfPTable
document.open();
PdfPTable pdfTable = new PdfPTable(chtbc_report1_table.getColumnCount());
for (int rows = 0; rows < chtbc_report1_table.getRowCount(); rows++) {
for (int cols = 0; cols < chtbc_report1_table.getColumnCount(); cols++) {
pdfTable.addCell(chtbc_report1_table.getModel().getValueAt(rows, cols).toString());
}
}
float[] columnWidths_pdfTable = new float[]{30f, 25f, 40f, 50f};
pdfTable.setWidths(columnWidths_pdfTable);
document.add(pdfTable);
document.close()
Any suggestions would help.
Oh, that is quite simple indeed. Try the following as the same works for me :-
Table Contents
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"Lorem Ipsum", "Next Line here \n, Another new line\n, Two new lines\n\nI will end here"},
{"Lorem Ipsum", "Another cell to demonstrate \n\n\n\n4 New lines above and two below \n\n"}
},
new String [] {
"Text", "Long Text"
}
));
Let me know, if that worked. And the PdfPtable should be the same.
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 need to remove rows form a JTable. I wrote the code like this:
DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
int x = 0;
int row = dtm.getRowCount();
while(row>=x){
dtm.removeRow(x);
x++;
}
But it generates an error, like:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException:
24 >= 24
A simpler solution is to use:
dtm.setRowCount(0);
This is also more efficient since the table only needs to repaint itself once, after all the rows have been deleted.
You can try this:
while(row>x){
dtm.removeRow(x);
x++;
}
UPDATE
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
model.addColumn("Col1");
model.addColumn("Col2");
model.addRow(new Object[]{"1", "2"});
model.addRow(new Object[]{"1", "2"});
table.setModel(model);
for(int index = 0; index<table.getRowCount();)
{
model.removeRow(index);
}
With Java installed it is easier. Sorry about the wrong answer.
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.