I came up with this loop here:
for(int i = 0; i < memberArrayList.size(); i++){
int j = 0;
memberTable.setValueAt(memberArrayList.get(i).getFirstName(), i, j);
j++;
memberTable.setValueAt(memberArrayList.get(i).getLastName(), i, j);
j++;
memberTable.setValueAt(memberArrayList.get(i).getEmail(), i, j);
j++;
memberTable.setValueAt(memberArrayList.get(i).getPhoneNumber(), i, j);
j++;
memberTable.setValueAt(memberArrayList.get(i).getLevel(), i, j);
}
It works but I'm sure there is a better way
The answer will depend on what type of TableModel you are using, for example, if you are using a DefaultTableModel you could use something like...
DefaultTableModel memberTable= ...;
for(int i = 0; i < memberArrayList.size(); i++){
Object[] rowData = new Object[]{
memberArrayList.get(i).getFirstName(),
memberArrayList.get(i).getLastName(),
memberArrayList.get(i).getEmail(),
memberArrayList.get(i).getPhoneNumber(),
memberArrayList.get(i).getLevel()
};
memberTable.addRow(rowData);
}
This assumes that you only want to add rows to the end of the table model ;)
Personally, I'd prefer to add each member to the table model as a single entity, but that's just me
Take a look at How to Use Tables for more details...
Swing components are using MVC pattern. JTable is the view, TableModels are the model that contains data.
you can subclass AbstractTableModel to achieve your goal.
public class MyTableModel extends AbstractTableModel {
// override these methods:
// getColumnCount, getRowCount, getValueAt
// and add data manipulation methods
}
then your table is created using MyTableModel
MyTableModel mtm = new MyTableModel(......);
JTable table = new JTable(mtm);
then call data manipulation methods in mtm. something like
mtm.addPerson(new Person(......));
Related
I have written a GUI Java program that manages a MySQL database. The user selects which columns (which tables and columns will be selected from the database) he/she wants to populate the JTable with.
I hard-coded the column names for the JTable so even if the user chooses to only display the data from a subset of columns, all of the column-names will be visible.
The problem is that when the user chooses columns in a different order than my JTable is anticipating, the data gets displayed in the wrong column.. It's a bit hard to explain so here's a screenshot of the genre data being loaded into the length column:
tableGenerator class:
package gui;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
public class TableGenerator
{
private ArrayList columnNames = new ArrayList();
private ArrayList data = new ArrayList();
private Vector columnNamesVector = new Vector();
private Vector dataVector = new Vector();
private int columns = 0;
private int rows = 0;
#SuppressWarnings("unchecked")
public TableGenerator(ResultSet rs)
{
try{
ResultSetMetaData md = rs.getMetaData();
columns = md.getColumnCount();
// Get column names
columnNames.add("Title");
columnNames.add("Year");
columnNames.add("Length");
columnNames.add("Genre");
columnNames.add("Actor");
columnNames.add("Producer");
columnNames.add("Director");
columnNames.add("Writer");
// Get row data
while (rs.next())
{
ArrayList row = new ArrayList(columnNames.size());
for (int i = 1; i <= columns; i++)
{
row.add(rs.getObject(i));
}
data.add( row );
rows++;
}
}
catch (SQLException e)
{
System.out.println( e.getMessage() );
}
// Create Vectors and copy over elements from ArrayLists to them
// Vector is deprecated but I am using them in this example to keep
// things simple - the best practice would be to create a custom defined
// class which inherits from the AbstractTableModel class
for (int i = 0; i < data.size(); i++)
{
ArrayList subArray = (ArrayList)data.get(i);
Vector subVector = new Vector();
for (int j = 0; j < subArray.size(); j++)
{
subVector.add(subArray.get(j));
}
dataVector.add(subVector);
}
for (int i = 0; i < columnNames.size(); i++ )
columnNamesVector.add(columnNames.get(i));
}
public Vector getColumns(){
return columnNamesVector;
}
public Vector getData(){
return dataVector;
}
public ArrayList getColumnNames(){
return columnNames;
}
public int getNumberOfRows(){
return rows;
}
}
I'm using the DefaultTableModel with some modifications.. :
model = new DefaultTableModel(rows, columns){
private static final long serialVersionUID = 1L;
#Override
public boolean isCellEditable(int row, int column) {
return false;
}
#Override
public Class<?> getColumnClass(int column) {
if (column < classes.length)
return classes[column];
return super.getColumnClass(column);
};};
Your query should always return the data for all columns. This means the data will be stored in the same manner in the TableModel.
You can then change the view for the columns to be displayed. That is you can remove TableColumns from the TableColumnModel of the JTable and only the data the user want to view will be displayed, even though it is still available in the model. Then means the user can click on any check box at any time and you don't need to redo the database query, only add the TableColumn back to the table.
Check out Table Column Manager for an example of this approach. This class uses a popup menu to manage the columns, but you can still use your check boxes. You just need to invoke the appropriate method of the TableColumnManager to hide/show a column. That is, assuming the labels of the check boxes match the headings in the table you can just use the check box text to hide/show a column.
The other approach is to NOT hard code the column names (if you build your query to only get specific columns) but instead get the column names from the meta data of the ResultSet. The TableFromDatabaseExample.java from Table From Database shows how this can be done. The code is generic so that appropriate renderers are used for Dates, Integers etc.
Java GUI aplication, load data to Jtable from a list
i have found the following link but i haven't found an answer: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
please can someone gice me an example how should i do it.
my objects have 5 fields:Name,Grade,Salary,BirthYear,Sex
and the list is readed from a file so i dont know how many ojects will the List have.
I am working in netbeans.
The tutorial you linked to has an example: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data. You should be able to adapt it to a List<Employee> easily:
public int getRowCount() {
return list.size();
}
public int getColumnCount() {
return 5;
}
public Object getValueAt(int row, int col) {
Employee employee = list.get(row);
if (col == 0) {
return e.getName();
}
else if (col == 1) {
return e.getGrade();
}
...
}
Using vector should help you.
Load an array to a Java table.
Vector model = new Vector();
Vector row = new Vector();
row.add("abce");
row.add("def");
row.add("ghi");
model.add(row);
row = new Vector();
row.add("sds");
row.add("sdfds");
row.add("24");
model.add(row);
JTable table = new JTable(model);
All the methods of Vector is synchronized. But, the methods of ArrayList is not synchronized.
I have List and List and I need to create JTable with theese two columns. I am confused with model, can anybofy show me how to do that please, I am new to swing and Java ?
Please check out my answer to some other question. Where I have presented a simple table model often use.
In your case you would create data in a following way:
//I assumed here list 1 and 2 have the same sizes
List<Object> list1 = getList1();
List<Object> list2 = getList2();
int rNo = list1.size();
List<List<Object>> data = new ArrayList<List<Object>>(rNo);
int cNo = 2;
for(int i = 0; i < rNo; i++)
{
List<Object> r = new ArrayList<Object>(cNo);
r.add(list1.get(i));
r.add(list2.get(i));
data.add(r);
}
tm.setData(data);
No worries, just set your desired component as a cell editor for that column. Simple ain't it.
Example Snippet
public class JTextFieldCellEditor extends DefaultCellEditor {
JTextField textField;
public JTextFieldCellEditor() {
super(new JTextField());
textField = (JTextField) getComponent();
}
}
Then include it like below,
TableColumn column = myTable.getColumnModel().getColumn(0);
column.setCellEditor(new JTextFieldCellEditor());
Further reading:
Here is your best bet, Swing tutorial for JTable.
I have a JTable using setAutoCreateRowSorter(true) and a RowSorterListener attached, per below, because I need to perform some operations elsewhere in my application upon a sort of the JTable.
I find that whenever I click a column header to sort, the JTable redisplays the rows in the proper order and the listener is called, but the TableModel I pull out is always the original pre-sort table model.
table.getRowSorter().addRowSorterListener(new RowSorterListener() {
#Override
public void sorterChanged(RowSorterEvent rsevent) {
rsevent.getSource().getModel(); // Nope, original ordering here
table.getModel(); // Same thing
}
};
How do I get the new post-sort ordering of the rows, as is now displayed in the JTable?
The data in the TableModel never changes, only the view of the data changes.
If you want the data from the model in the order it is displayed in the table then you just use:
table.getValueAt(row, column);
In other words you need to iterate through all the rows and columns to get the data in the currently viewed order.
I think you can use table.convertRowIndexToModel(int ...), however I think that there's a better solution available.
If you define your own convertRowIndexToModel() that's a quick lookup (perhaps through a map, O(1)), that solution should be sufficient. If it's O(n), then that's what needs to be fixed.
Simply go through a loop of the rows, convert each one and do the lookup using the resulting index.
Here's 2 methods in TableSorter that should be of interest:
private Row[] getViewToModel() {
if (viewToModel == null) {
int tableModelRowCount = tableModel.getRowCount();
viewToModel = new Row[tableModelRowCount];
for (int row = 0; row < tableModelRowCount; row++) {
viewToModel[row] = new Row(row);
}
if (isSorting()) {
Arrays.sort(viewToModel);
}
}
return viewToModel;
}
and:
private int[] getModelToView() {
if (modelToView == null) {
int n = getViewToModel().length;
modelToView = new int[n];
for (int i = 0; i < n; i++) {
modelToView[modelIndex(i)] = i;
}
}
return modelToView;
}
Hi I know this is a really late answer, but I tried the code used on the comment of #LazyCubicleMonkey and it did work here is my code in getting the row when the jTable is sorted.
DefaultTableModel search_model = (DefaultTableModel) jTable.getModel();
search_model.removeRow(jTable.convertRowIndexToModel(row));
jTable.setModel = (search_model)
I've populated a JTable through a DefaultTableModel with the (Object[][] data, String[] headers) constructor. Users can edit the table, and I want to be able to load the new data back into an array (Object[][]). Note that I'd rather not just update the array bit by bit, but be able to just completely load a new array from the table. How can this be done?
I take that back, on 2nd thoughts, you dont need any typecasting - TableModel is an interface that has all the 3 method calls you need. :)
Summary: Get the model for the table, check its class and typecast it to appropriate class (Abstract or Default TableModel), and use its methods to load a newly created array. Some psuedoCode:
public Object[][] getTableData (JTable table) {
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();
Object[][] tableData = new Object[nRow][nCol];
for (int i = 0 ; i < nRow ; i++)
for (int j = 0 ; j < nCol ; j++)
tableData[i][j] = dtm.getValueAt(i,j);
return tableData;
}
Your headers should not have changed by user-edits. Hope that helps. Regards, - M.S.
I'd like to suggest a small improvement on Manidip Sengupta's answer. Rather than casting table.getModel() to the appropriate class, it is better to simply work with TableModel. This makes the code more re-usable too (it doesn't matter which implementation of TableModel is actually being worked with).
public Object[][] getTableData (JTable table) {
TableModel dtm = table.getModel();
int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();
Object[][] tableData = new Object[nRow][nCol];
for (int i = 0 ; i < nRow ; i++)
for (int j = 0 ; j < nCol ; j++)
tableData[i][j] = dtm.getValueAt(i,j);
return tableData;
}
I've populated a JTable through a DefaultTableModel with the (Object[][] data, String[] headers)
The DefaultTableModel is a dynamic model, which means rows and columns can be added dynamically. Arrays are not dynamic so when you create a DefaultTableModel using arrays, the data from the arrays is copied to a Vector of Vectors.
I want to be able to load the new data back into an array (Object[][]).
I'd rather not just update the array bit by bit
Unfortunately you will have to update the array cell by cell since the data is not stored in a 2D array.
Or, since the DefaultTableModel does use a Vector of Vectors to store the data you can use the getDataVector() method to access the data. Then you get each row from the Vector and invoke the List.toArray() method on the row Vector before adding it to your array.
Either way you will need to loop through the Vectors in the model.
If you want to use the 2D array as storage for the TableModel, then you will need to create a custom TableModel that uses the supplied array for the data storage. After implemting all the required methods of the TableModel interface you will need to provide a getTableDataArray() method to return the reference to the array.
Not with the existing default table model. As a rule of thumb, you should ever use the default table model. You should implement your own table model (MyTableModel). You can either implement TableModel or extend AbstractTableModel. I suggest the latter as it provides some nice utility methods. There are 2 ways to achieve what you are doing
populate your new data into a new instance of MyTableModel and call JTable.setModel().
A second way is to create a method in MyTableModel call eg, replaceData(T[][] data). Hold a reference to the model that the table is current displaying. Whenever you want to replace the data, call replaceData(). Assuming you are extending AbstractTableModel, then call fireTableChanged() to notify the table.
With the out-of-the-box JTable implementation, you can't. When you initialize a JTable with Object[][] rowData that rowData is used by an anonymous AbstractTableModel instance but it's not accesible as such from outside.
public JTable(final Object[][] rowData, final Object[] columnNames) {
this(new AbstractTableModel() {
public String getColumnName(int column) { return columnNames[column].toString(); }
public int getRowCount() { return rowData.length; }
public int getColumnCount() { return columnNames.length; }
public Object getValueAt(int row, int col) { return rowData[row][col]; }
public boolean isCellEditable(int row, int column) { return true; }
public void setValueAt(Object value, int row, int col) {
rowData[row][col] = value;
fireTableCellUpdated(row, col);
}
});
}
You could consider to subclass both JTable and AbstractTableModel and "overwrite" this constructor in JTable to create your own TableModel implementation that will hold that Object[][] reference and return it with Object[][] getRowData(). Or just keep the rowData as a field in the JTable subclass itself after calling super(...) in that constructor -- if you don't really care about MVC.
But you need to make sure that after the table is edited, the initial model is preserved and not replaced by a new model object (of course, of a different type) with setModel. If this is the case, what you want is impossible to achieve -- you will need to iterate through all the cells.