How to make a JTable entries from outside data? - java

I'm looking for a way to make data entries for a table from data collected from a SQL database. I have the code made for grabbing the data from the database but I need help with turning that data into table entries

First create an object to represent each row in the table.
public class MyData {
private String name;
private String desc;
//add getters/setters
}
Then you should create a table model
public class MyTableModel extends AbstractTableModel {
List<MyData> list = new ArrayList<>();
public void addRow(MyData data) {
list.add(data);
fireTableRowsInserted(list.size()-1, list.size()-1);
}
//then fill in the required method
public String getColumnName(int col) {
switch (col) {
case 0:
return "Name";
case 1:
return "Description";
}
return "";
}
public int getRowCount() {
return list.size();
}
public int getColumnCount() {
return 2; //or however many columns you need
}
public Object getValueAt(int row, int col) {
MyData data = list.get(row);
if (col == 0) {
return data.getName();
} else if (col == 1) {
return data.getDesc();
}
return null;
}
}
Then use that model in your table;
MyTableModel tableModel = new MyTableModel();
JTable table = new JTable(tableModel);
Then you you can read data from the database you just have to call
tableModel.add(data);

Related

Java Table model data disappearing

I've run into a very strange issue. I wrote a model for a table and for the most part it works perfect. When I update the row data via the setValueAt function it works and the data in the ArrayList is updated and complete. But when i run the getTotalPay function the same array is empty and i get a null value exception.
I've rewrote the code a number of different ways and tried a couple of different approaches to the problem but the problem persists. I know i've done something wrong i just cannot figure out what the problem is. After search the net and not finding a solution i'm posting this.
Things i've tried are replacing the variable, putting the data into two different variables.
Here is the model
public class WeeklyWagesModel extends AbstractTableModel {
private String defaultValue = "Click to edit";
private String[] headings = { "Date From", "Date To", "Wages", "Pay Date" };
private ArrayList<String[]> theData = new ArrayList<String[]>();
public WeeklyWagesModel() {
theData.add(createDefaultRow());
}
#Override
public int getColumnCount() {
return headings.length;
}
public String getColumnName(int col) {
return headings[col];
}
#Override
public int getRowCount() {
return theData.size();
}
#Override
public Object getValueAt(int row, int col) {
return theData.get(row)[col];
}
public boolean isCellEditable(int row, int col) {
return true;
}
public void setValueAt(Object value, int row, int col) {
theData.get(row)[col] = String.valueOf(value);
if (row == (theData.size() - 1)) {
theData.add(createDefaultRow());
}
fireTableDataChanged();
}
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
public ArrayList<String[]> getData() {
return theData;
}
public String getTotalPay() {
System.out.println("Total Pay: " + theData.size());
BigDecimal total = BigDecimal.ZERO;
for (String[] eachRow : theData) {
if (StringUtils.isNumeric(eachRow[2])) {
total.add(new BigDecimal(eachRow[2]));
}
}
return NumberFormat.getCurrencyInstance().format(total);
}
public String getTotalWeeks() {
return "";
}
private String[] createDefaultRow() {
String[] newRow = new String[headings.length];
for (int i = 0; i < newRow.length; i++) {
newRow[i] = defaultValue;
}
return newRow;
}
}

Reload JTable's Object[][] after data has changed

I am wondering if it is possible to refresh a Table's GUI when the Object[][] used to initially populate it has changed.
Object[][] calculationsTableData;
public Interface() {
...
analysisPanel.add(calculationsTable());
...
}
JScrollPane calculationsTable() {
populateCalculationsTableData();
...
calculationsTable = new JTable(calculationsTableData, calculationsColumnNames);
...
}
void populateCalculationsTableData(){
Object[][] temp = new Object[x.numsSize][7];
for (int i = 0; i < x.numsSize; i++) {
temp[i][0] = df.format(x.nums[i]);
...
}
calculationsTableData = temp;
}
populateCalculationsTableData() is called when the x object has changed to repopulate calculationsTableData
repaint() and revalidate() methods do not appear to be effective in this scenario and neither does fireTableDataChanged() as this is not a table model.
By default JTable creates a new instance of DefaultTableModel AbstractTableModel [1] implementation if no table model is explicitely set. If the table needs to be updated you have to work with its model.
You could...
Set a new DefaultTableModel:
void populateCalculationsTableData() {
...
calculationsTableData = temp;
TableModel model = new DefaultTableModel(calculationsTableData, calculationsColumnNames);
calculationsTable.setModel(model);
}
Or you could...
Create the table using a DefaultTableModel:
TableModel model = new DefaultTableModel(calculationsTableData, calculationsColumnNames);
calculationsTable = new JTable(model);
Cast the table model as DefaultTableModel and set its data and columns like this:
void populateCalculationsTableData() {
...
calculationsTableData = temp;
DefaultTableModel model = (DefaultTableModel)calculationsTable.getModel();
model.setDataVector(calculationsTableData, calculationsColumnNames);
}
Suggested readings
JTable(Object[][] rowData, Object[] columnNames)
DefaultTableModel#setDataVector(Object[][] dataVector, Object[] columnIdentifiers)
How to Use Tables
[1]: After checking JTable(Object[][] rowData, Object[] columnNames) source code I've realized that no DefaultTableModel is created but a new anonymous inner class extending from AbstractTableModel instead.
I would create your own table model to wrap the object arrays, and which exposed the fireTableDataChanged() method:
private static class ObjectArrayModel extends AbstractTableModel {
private final Object[][] rowData;
private final Object[] columnNames;
private ObjectArrayModel(Object[][] rowData, Object[] columnNames) {
this.rowData = rowData;
this.columnNames = columnNames;
}
#Override
public void fireTableDataChanged() {
super.fireTableDataChanged();
}
public String getColumnName(int column) {
this.fireTableDataChanged();
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);
}
}
... when the data changes you should be able to call fireTableDataChanged() on this and it will notify the table it needs to redraw itself.

Not able to get the correct row from a JTable after Sorting it (Swing)

I got an AbstractTableModel, like this:
public class TableModelClienteFornecedor extends AbstractTableModel {
private List<ClienteFornecedorDTO> linhas;
private String[] colunas = new String[]{"Nome", "Conta"};
public TableModelClienteFornecedor() {
linhas = new ArrayList<>();
}
#Override
public int getRowCount() {
return linhas.size();
}
#Override
public int getColumnCount() {
return colunas.length;
}
#Override
public String getColumnName(int column) {
return colunas[column];
}
#Override
public Class getColumnClass(int column) {
return (getValueAt(0, column).getClass());
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
ClienteFornecedorDTO cf = linhas.get(rowIndex);
switch (columnIndex) {
case 0:
return cf.getNome();
case 1:
return cf.getConta();
default:
throw new IndexOutOfBoundsException("Coluna incorreta");
}
}
public void clear(JTable table) {
table.setRowSorter(null);
int indiceAntigo = this.getRowCount();
linhas.clear();
int indiceNovo = this.getRowCount();
this.fireTableRowsDeleted(indiceAntigo, indiceNovo);
}
public boolean isEmpty() {
return linhas.isEmpty();
}
public void add(ClienteFornecedorDTO cf) {
linhas.add(cf);
int index = this.getRowCount();
fireTableRowsInserted(index, index);
}
public void addList(List<ClienteFornecedorDTO> list, JTable table) {
int tamanhoAntigo = this.getRowCount();
linhas.addAll(list);
int tamanhoNovo = this.getRowCount() - 1;
this.fireTableRowsInserted(tamanhoAntigo, tamanhoNovo);
table.setAutoCreateRowSorter(true);
}
public ClienteFornecedorDTO get(int i) {
return linhas.get(i);
}
}
And the code below works ok to fill the my Jtable with data:
private void realizarBusca(String nome) {
IContaDAO dao = new ContaDAO();
boolean isFornecedor = radioFornecedor.isSelected();
List<ClienteFornecedorDTO> retorno =
dao.retornaContaClienteFornecedor(isFornecedor, nome);
tableModelClienteFornecedor.clear();
tableModelClienteFornecedor.addList(retorno, tableClienteFornecedor);
tableClienteFornecedor.updateUI();
}
Everything's working fine to me, and when I sort my Jtable visibily it's ok too, the problem is when I click on a specific row from my Jtable after I sorted it, the row it's not updated.
Anyone can help me with that?
I would appreciate, 'cause I'm on it since yesterday and still wasn't able to find a way to solve it.
Look at the methods convertRowIndexToModel() and convertRowIndexToView() in JTable.
When the table is sorted, the indices of the rows in the view don't match with the indices in the model anymore, and you have to use the above methods to convert from index to view and vice-versa.
For example, if you call JTable.getSelectedRow(), you'll get the view index of the selected row. You'll have to convert it to the model index (using convertRowIndexToModel()) to be able to get the selected object from the list in your model.

Create custom table model with column header in java

I'm developing a software for my friend.I have developed 90% of my project and my problem is I have a table which I fill with data in database.I have used a custom table model to fill jtable. It fills data perfectly but the problem is it doesn't contain proper column header instead of that it contain A,B,C for column headers .
here is my custom table model class.
public class SellUpdateModel extends AbstractTableModel
{
private Vector<Vector> data;
public SellUpdateModel(String Jid)
{
data = new Vector<>();
data = new JobDetailsDAO().get_ItemDescriptionAndQuantity(Jid); //Retrive data from databse and fill it to vector.
}
#Override
public boolean isCellEditable(int rowIndex, int columnIndex)
{
return false; // does not allowed to edit cells
}
#Override
public int getRowCount()
{
return data.size();
}
#Override
public int getColumnCount()
{
return 3;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex)
{
return data.elementAt(rowIndex).elementAt(columnIndex);
}
public SellUpdateModel deleteRow(int row, Vector pData)
{
data.remove(data.elementAt(row)); // remove a record from vector
data.add(pData); // add a record to vector
return this;
}
}
You have to override getColumnName(int columnIndex).
public class SellUpdateModel extends AbstractTableModel {
private final String[] tableHeaders = {"X", "Y", "Z"};
#Override
public String getColumnName(int columnIndex) {
return tableHeaders[columnIndex];
}
}

Output the content of a LinkedHasHSet into a JTable

I have a LinkedHashSet of Book objects. The Book objects have the following fields:
private int id;
private String author = "";
private String title = "";
private boolean isRead;
private String dateStamp = "";
private static int counter = 0;
I want them to go into my JTable which has the following columns:
String [] columnNames = {"ID","Title", "Author", "Status", "Date Read"};
How can I do this? And is it possible to have the isRead field being editable through a checkbox in the table?
You need to have a class that extends AbstractTableModel. This class should use your LinkedHashSet as the data source for your table. The basic implementation provided by AbstractTableModel should suit most of your needs. If not, then you can override the methods you need to customize.
This tutorial should help you understand how JTable objects work.
This is a sample model I have created for the table.
public class CustomModel extends AbstractTableModel {
private Object[] colNames ={"ID","Title", "Author", "Status", "Date Read"};
private LinkedHashSet<CustomClass> data;
public TableModelTop() {
this.data = getDataForDropList();
}
public int getRowCount() {
return data.size();
}
public int getColumnCount() {
return colNames.length;
}
#Override
public String getColumnName(int columnIndex) {
return (String) colNames[columnIndex];
}
#Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
// Set Values here;
}
public Object getValueAt(int rowIndex, int columnIndex) {
// Get row Values here;
}
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
#Override
public Class<?> getColumnClass(int columnIndex) {
// Depending on the type of the column. Return data type;
}
/**
* Populate the data from here.
* #return LinkedHashSet<CustomClass>
*/
private LinkedHashSet<CustomClass> getDataForDropList() {
LinkedHashSet<CustomClass> modelList = new LinkedHashSet<CustomClass>();
for(int i = 0; i< 5; i++) {
// Create custom Object and add them to the LinkedHashSet.
// Create a CustomClass object and add it to the LinkedHashSet
modelList.add(customClassObject);
}
// Finally return the llinkedhashset
return modelList;
}
}
After this just call the table model and assign this to the JTable.
JTable table = new JTable(new CustomModel());
As a concrete example of using AbstractTableModel, you can leverage the toArray() method inherited by LinkedHashSet to simplify the implementation of getValueAt(), as shown in this related EnvTableTest. The default JTable renderer and editor use JCheckBox for TableModel elements of type Boolean.class, as shown in this example.

Categories

Resources