How to set a custom object in a JTable row - java

I should tell this first, this is NOT about Rendering a Table cell.
Here is the TableModel that i'm building using a 2D array based on a User object in my DB.
List<User> userList = userManagerService.getAllUsers();
/* String[] col_user = {"Username", "Name", "Phone", .... } */
String[][] data = new String[userList.size()][col_user.length];
int i = 0;
for (User user : userList) {
String[] userdata = new String[col_user.length];
userdata[0] = user.getUserUsername();
userdata[1] = user.getUserName();
userdata[2] = user.getUserPhone();
userdata[3] = user.getUserNic();
userdata[4] = user.getUserAddress();
userdata[5] = user.getUserEmail();
data[i++] = userdata;
}
VstTableItemModel tiModel = new VstTableItemModel(data, col_user);
dataTable.setModel(tiModel);
My problem is how can i get a User object back, using the selected row in the Table. Note that i can't make a new User object and populate it with the row data. I must get the queried User object(objects in userList). So, is their any way to set a Object with a table row ?
Here is my VstTableItemModel class.
public class VstTableItemModel extends AbstractTableModel {
ArrayList<Object[]> data;
String[] header;
public VstTableItemModel(Object[][] obj, String[] header) {
this.header = header;
data = new ArrayList<Object[]>();
for (int i = 0; i < obj.length; ++i) {
data.add(obj[i]);
}
}
#Override
public int getRowCount() {
return data.size();
}
#Override
public int getColumnCount() {
return header.length;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data.get(rowIndex)[columnIndex];
}
#Override
public String getColumnName(int index) {
return header[index];
}
}

Instead of splitting the User object up before you create the model, add it directly to the model and allow the model to do the work for you...
For example
public class VstTableItemModel extends AbstractTableModel {
private List<User> users;
public VstTableItemModel(List<User> users) {
this.users = new ArrayList<User>(users);
}
#Override
public int getRowCount() {
return users.size();
}
#Override
public int getColumnCount() {
return 6;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object value = "??";
User user = users.get(rowIndex);
switch (columnIndex) {
case 0:
value = user.getUserUsername();
break;
case 1:
value = user.getUserName();
break;
case 2:
value = user.getUserPhone();
break;
case 3:
value = user.getUserNic();
break;
case 4:
value = user.getUserAddress();
break;
case 5:
value = user.getUserEmail();
break;
}
return value;
}
#Override
public Class<?> getColumnClass(int columnIndex) {
return // Return the class that best represents the column...
}
/* Override this if you want the values to be editable...
#Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
//....
}
*/
/**
* This will return the user at the specified row...
* #param row
* #return
*/
public User getUserAt(int row) {
return users.get(row);
}
}
This way, you should be able to do something like...
List<User> userList = userManagerService.getAllUsers();
VstTableItemModel tiModel = new VstTableItemModel(userList);
Now when you need to...you can grab a the user that is represent at a specific row...
User user = tiModel.getUserAt(rowIndex);

Related

Recieving a ClassCastException when using getValueAt in AbstractTable Model

I have a client/server program built. The object of the program is to send an array of data across to the client using sockets, display the data into a table then be able to edit it and send it back for the server to store. The program compiles fine but when run the client doesn't output the array to the table. It displays an error:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to client.Network
at client.MyTableModel.getValueAt(MyTableModel.java:77)
MyTableModel class:
package client;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
public class MyTableModel extends AbstractTableModel {
void setData(ArrayList<Network> serversArray) {
data = serversArray;
System.out.print(this.data +"\n");
System.out.print(serversArray + "\n");
}
ArrayList<Network> data = new ArrayList();
String[] headers = {"NetworkID","Nodes","Hubs","Switches","Structure","Country","Status"};
#Override
public int getColumnCount() {
return headers.length;
}
#Override
public int getRowCount() {
return data.size();
}
#Override
public void setValueAt(Object value, int row, int col) {
switch (col) {
case 0:
data.get(row).setNetworkID((int) value);
break;
case 1:
data.get(row).setNodes((int) value);
break;
case 2:
data.get(row).setHubs((int) value);
break;
case 3:
data.get(row).setSwitches((int) value);
break;
case 4:
data.get(row).setStructure("");
break;
case 5:
data.get(row).setCountry("");
break;
case 6:
data.get(row).setStatus("");
break;
}
fireTableCellUpdated(row, col);
}
#Override
public String getColumnName(int col)
{ return headers[col]; }
#Override
public Object getValueAt(int row, int col) {
switch (col) {
case 0:
return data.get(row).getNetworkID();
case 1:
return data.get(row).getNodes();
case 2:
return data.get(row).getHubs();
case 3:
return data.get(row).getSwitches();
case 4:
return data.get(row).getStructure();
case 5:
return data.get(row).getCountry();
case 6:
return data.get(row).getStatus();
}
return 0;
}
}
Network class:
public class Network implements Serializable{
private int NetworkID;
private int Nodes;
private int Hubs;
private int Switches;
private String Structure;
private String Country;
private String Status;
public int getNetworkID(){
System.out.println(this.NetworkID);
return this.NetworkID;
}
public int getNodes(){
return this.Nodes;
}
public int getHubs(){
return this.Hubs;
}
public int getSwitches(){
return this.Switches;
}
public String getStructure(){
return this.Structure;
}
public String getCountry(){
return this.Country;
}
public String getStatus(){
return this.Status;
}
public void setNetworkID(int networkID){
this.NetworkID = networkID;
System.out.print(this.NetworkID);
}
public void setNodes(int nodes){
this.Nodes = nodes;
}
public void setHubs(int hubs){
this.Hubs = hubs;
}
public void setSwitches(int switches){
this.Switches = switches;
}
public void setStructure(String structure){
this.Structure = structure;
}
public void setCountry(String country){
this.Country = country;
}
public void setStatus(String status){
this.Status = status;
}
}
Any assistance would be much appreciated!
update:
private void displayNetworksActionPerformed(java.awt.event.ActionEvent evt) {
pw.println("SendCSV");
// receieve data array from server
try {
serversArray = (ArrayList<Network>)ois.readObject();
System.out.print(serversArray);
statusBar.setText("Object Recieved from Server!");
} catch (ClassNotFoundException ex) {
statusBar.setText("Didnt Recieve Object");
} catch (IOException ex) {
statusBar.setText("Unable to Request");
}
// 4. update jtable
this.myTableModel.setData(serversArray);
this.myTableModel.fireTableDataChanged();
}
Update:
I am still having an issue with this, is there any chance someone could assist with a solution?

updating jTable row if record is already present

I am working on invoice and have added jTable to my frame for multiple items, like I have 4 fields for user "item, quantity, rate, price" and user fills it all and click on add button then these fields are added to jTable with item id inlcuded.
now the problem I am facing when user enters the same item whereas that item is already added in the list and it duplicates the row in jTable.
All i want to update that row if the item is same, update its quantity field with adding new quantity value and price value.
i.e.
id |Item |qty| rate| Price|
--------------------------------------
12 |saee |3 | 300| 900 |
now if user enters the same item with 5 quantity, it should update same item or row with plus quantity and price like.
--------------------------------------
id |Item |qty| rate| Price|
--------------------------------------
12 |saee |8 | 300| 24000|
but instead of this, it adds a second row in jTable.
here is my code for adding items in to jTable
DefaultTableModel model = (DefaultTableModel) tbl_sale.getModel();
model.addRow(new Object[]{lbl_id.getText(), txt_item.getText(), txt_qty.getText(), txt_rate.getText(), txt_rs.getText()});
can anyone tell me what should i do?
Sometimes I think we forget that we're operating in an OO environment and the power that something like that can bring
What I mean is, you data can easily be encapsulated into an object, for example...
public class Order {
private int id;
private String item;
private int quanity;
private double unitPrice;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getItem() {
return item;
}
public void setItem(String item) {
this.item = item;
}
public int getQuanity() {
return quanity;
}
public void setQuanity(int quanity) {
this.quanity = quanity;
}
public double getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
public double getTotalPrice() {
return getUnitPrice() * getQuanity();
}
}
This also has the capacity to calculate the total price dynamically, rather than needing us to do it.
DefaultTableModel is good for representing disjointed pieces of information, but not so good at representing an object per row, to this end, I use an AbstractTableModel and customise it for the needs of the object, something like...
public class OrderTabelModel extends AbstractTableModel {
protected static final String COLUMN_NAMES[] = {"ID", "Item", "Qty", "Rate", "Price"};
protected static final Class COLUMN_TYPES[] = {int.class, String.class, int.class, double.class, double.class};
private List<Order> orders;
public OrderTabelModel() {
orders = new ArrayList<>(25);
}
public void add(Order order) {
orders.add(order);
fireTableRowsInserted(orders.size() - 1, orders.size() - 1);
}
public void remove(Order order) {
int row = orders.indexOf(order);
if (row >= 0) {
orders.remove(order);
fireTableRowsDeleted(row, row);
}
}
public void update(Order order) {
int row = orders.indexOf(order);
if (row >= 0) {
fireTableRowsUpdated(row, row);
}
}
public Order getOrderAt(int row) {
return orders.get(row);
}
#Override
public int getRowCount() {
return orders.size();
}
#Override
public int getColumnCount() {
return COLUMN_NAMES.length;
}
#Override
public Class<?> getColumnClass(int columnIndex) {
return COLUMN_TYPES[columnIndex];
}
#Override
public String getColumnName(int column) {
return COLUMN_NAMES[column];
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
Object value = null;
Order order = getOrderAt(rowIndex);
switch (columnIndex) {
case 0:
value = order.getId();
break;
case 1:
value = order.getItem();
break;
case 2:
value = order.getQuanity();
break;
case 3:
value = order.getUnitPrice();
break;
case 4:
value = order.getTotalPrice();
break;
}
return value;
}
}
Now, when you want to add/remove/update a given object, you can just call add/remove/update methods of the OrderTabelModel.
Be Sure though, when you want to update an Order, you're interacting with an instance from the model, otherwise things won't go as you expect...
OrderTableModel model = ...;
//...
Order order = model.getOrderAt(0);
order.setQuanity(10);
model.update(order);

how updadate mysql db using jtable

i want to update my database using jtable, table r disply but not update please provide mi solution for it
i am doing following code but it cant update my database and how can fire query for that my database contain id,name,password,email,phone_no
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
public class JtExample extends JFrame {
JTable tbldetails;
DefaultTableModel dtm ;
public int editcol1;
public int editrow;
public JtExample() {
setVisible(true);
setSize(500,500);
setTitle("login Frame");
setLocationRelativeTo(null);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dtm = new DefaultTableModel(); //dtm consiste row and clonum
String rowheader[] = {"ID","Name" ,"Password", "Email","phn_no"};
dtm.addColumn("ID");
dtm.addColumn("Name");
dtm.addColumn("address");
dtm.addColumn("Email");
dtm.addColumn("phn_no");
dtm.addRow(rowheader);
add();
dtm.addTableModelListener(new TableModelListener ()
{
#Override
public void tableChanged(TableModelEvent arg0) {
int editcol1 =tbldetails.getEditingColumn();
int editrow =tbldetails.getEditingRow();
TableCellEditor tce = tbldetails.getCellEditor(editrow , editcol1);
System.out.println(tce.getCellEditorValue());
}
});
tbldetails = new JTable(dtm);
tbldetails.setBounds(100,100,500,200);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://Localhost:3306/mydata","root","root");
PreparedStatement ps=con.prepareStatement(" update employee set editcol1=? where editrow=?");
int editcol1 = 0;
String tce = null;
ps.setString(editcol1, tce);
int i=ps.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
add(tbldetails);
}
public void add()
{
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://Localhost:3306/mydata","root","root");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select *from employee");
while(rs.next())
{
dtm.addRow(new Object[]{rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5)});
}
con.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String args[])
{
new JtExample();
}
}
public static void main(String args[])
{
new JtExample();
}
}
Note: There is more then one way to skin this cat
My first thought is, don't use a DefaultTableModel, instead, use a AbstractTableModel, this will give you greater control of the model and changes to its state.
Start by defining a Plain Old Java Object (POJO) which represents your data. Personally I prefer to start with an interface, this allows me to define mutable and non-mutable versions depending on my requirements
Something like...
public class Employee {
private String id; //??
private String name;
private String password; // Probably should be a char[]
private String email;
private String phoneNumber;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public Employee(String id, String name, String password, String email, String phoneNumber) {
this.id = id;
this.name = name;
this.password = password;
this.email = email;
this.phoneNumber = phoneNumber;
}
}
...for example
Next, you need to define a TableModel which is capable of supporting this data...
public class EmployeeTableModel extends AbstractTableModel {
private String columnNames[] = {"ID","Name" ,"Password", "Email","phn_no"};
private List<Employee> employees;
public EmployeeTableModel() {
employees = new ArrayList<Employee>(25);
}
public EmployeeTableModel(List<Employee> employees) {
this.employees = employees;
}
public void add(Employee employee) {
employees.add(employee);
fireTableRowsInserted(employees.size() - 1, employees.size() - 1);
}
public void remove(Employee employee) {
int index = employees.indexOf(employee);
employees.remove(employee);
fireTableRowsDeleted(index, index);
}
#Override
public int getRowCount() {
return employees.size();
}
#Override
public int getColumnCount() {
return columnNames.length;
}
#Override
public String getColumnName(int column) {
return columnNames[column];
}
public Employee getEmployeeAt(int row) {
return employees.get(row);
}
#Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
Employee emp = getEmployeeAt(rowIndex);
Object value = null;
switch (columnIndex) {
case 0:
value = emp.getId();
break;
case 1:
value = emp.getName();
break;
case 2:
value = emp.getPassword();
break;
case 3:
value = emp.getEmail();
break;
case 4:
value = emp.getPhoneNumber();
break;
}
return value;
}
}
We're going to add to this later, but for now, this gives us the basics we need...
When you load the data from the database, you could use something like...
EmployeeTableModel model = new EmployeeTableModel();
try (ResultSet rs = st.executeQuery("select *from employee")) {
while(rs.next())
{
model.add(new Employee(
rs.getString(1),
rs.getString(2),
rs.getString(3),
rs.getString(4),
rs.getString(5)));
}
} finally {
tbldetails.setModel(model);
}
So, now we have a self contained unit of work, in our Employee class, a TabelModel which can support it and a means by which you can load the data, now, you need some way to intercept the changes to the data and update the database.
To this end, we're going to update the EmployeeTableModel
public class EmployeeTableModel extends AbstractTableModel {
//...
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex > 0; // id should not be editable here...
}
#Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Employee emp = getEmployeeAt(rowIndex);
switch (columnIndex) {
case 1:
emp.setName(aValue.toString());
break;
case 2:
emp.setPassword(aValue.toString());
break;
case 3:
emp.setEmail(aValue.toString());
break;
case 4:
emp.setPhoneNumber(aValue.toString());
break;
}
update(emp);
fireTableCellUpdated(rowIndex, columnIndex);
}
This will call the update method every time a cell is updated. To this, we pass the Employee object. Based on the value of the id property, you will either need to update or insert a new record.
This is a very simple example, because of the nature of JDBC, the JDBC call could take a period of time to execute. I might be tempted to have some kind of (blocking) queue, onto which I could add Employee objects.
This queue would be processed by another Thread (or SwingWorker or some such), which would pop off the next object and process it, triggering an event callback (to which the TableModel would be listening) with the updated data. The TableModel would then be able to update itself accordingly...
Another idea is to simply have a "save" button, which the user can click. You would then simply iterate through the list of Employees and update them. For this, I would have a simple boolean flag for each object, which would be set to true whenever any of the set methods are called
public class Employee {
private boolean changed = false;
public boolean hasChanged() {
return changed;
}
public void setName(String name) {
this.name = name;
changed = true;
}
Take a closer look at How to Use Tables for moe details

Initialising a Subclass that extends an Abstract class

I have a TableModel class that extends the AbstrctTableModel class in java swing but when I try to initialise the subclass which is not abstract, the Netbeans 8.0 IDE complains that I am initialising an abstract class.
the code snippets are provide below.
public class TableModel extends AbstractTableModel {
private List<List<Object>> dataList = new ArrayList<>();
private String[] header = { "ID","SUBJECT","LETTTER FROM","LETTER DATE","DATE RECEIVED",
"REMARKS","DATE DISPATCHED","DESTINATION OFFICE"};
private int minRowCount = 5;
public TableModel()
{ }
public List<List<Object>> getDataList() {
return dataList;
}
public void setDataList(List<List<Object>> dataList) {
this.dataList = dataList;
fireTableDataChanged();
fireTableStructureChanged();
}
public void setHeader(String[] header) {
this.header = header;
}
public String[] getHeader() {
return header;
}
#Override
public int getRowCount() {
return Math.max(minRowCount, dataList.size());
}
#Override
public int getColumnCount() {
return header.length;
}
#Override
public String getColumnName(int col) {
return header[col];
}
#Override
public Object getValueAt(int row, int col) {
Object value = null;
if(row < dataList.size())
{value = dataList.get(row).get(col);}
return value;
}
#Override
public Class<?> getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);
if (o != null)
{
return o.getClass();
}
}
return Object.class;
}
}
this is the code that initialises the Table Model class.
private TableColumnAdjuster tca;
/**
* Creates new form MyJFrame
*/
private TableModel tableModel ;
public MyJFrame() throws CorruptIndexException, LockObtainFailedException, IOException,
ParseException, java.text.ParseException, SQLException
{
tableModel = new TableModel(); // the Netbeans IDEcomplains about this code
initComponents();
}
You don't provide any info to specify the exact nature of the complaint, but I'm betting that the AbstractTableModel has an abstract method that your TableModel has failed to override. It would be a compilation error in that case.
If that's true, provide a concrete implementation for that method and you'll be able to take the next step.

Writing information from SQL database to list

I'm working on my homework and I can't complete one piece of my program ...
I have JTable class which makes table in my code ... i have to write method which takes information from sql database and writes it in list
method MUST look like:
public static List selectAnswers (int questionId) throws SQLException, IOException
following code is written by me:
public static List<AnswerRow> selectAnswers (int questionId) throws SQLException, IOException
{
Connection veza = connectToDatabase();
Properties query = new Properties();
AnswersTableModel atm = new AnswersTableModel();
String selectAnswers = query.getProperty("selectAnswers");
PreparedStatement preparedStatement = veza.prepareStatement(selectAnswers);
ResultSet rs = preparedStatement.executeQuery();
List<AnswerRow> lista = new ArrayList<AnswerRow>();
while(rs.next()){
String answerText = rs.getString("answerText");
boolean isRight = rs.getBoolean("answerRight");
?????????????????????????????????????????????????
}
closeConnectionToDatabase(veza);
return lista;
}
????? field is missing and i dont know what to write there to write information answeText and isRight into AnswerRow class , into AnswerTableModel, into list ...
Code which makes JTable (and is given to me and cannot be changed by my teacher) is here:
package hr.tvz.java.deveti.model;
import java.util.List;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
public class AnswersTableModel extends AbstractTableModel {
private Object[][] answers;
private String[] columnNames;
public AnswersTableModel (String[] colNames){
super();
columnNames = colNames;
}
public AnswersTableModel() {
super();
this.columnNames = new String[AnswerRow.TABLE_COLUMNS];
this.columnNames[0] = "Odgovor";
this.columnNames[1] = "Točan/Netočan";
}
public java.lang.Class<?> getColumnClass(int columnIndex) {
return getValueAt(0, columnIndex).getClass();
}
public int getColumnCount() {
return AnswerRow.TABLE_COLUMNS;
}
public int getRowCount() {
if (answers != null)
return answers.length;
else
return 0;
}
public Object getValueAt(int row, int column) {
return answers[row][column];
}
public String getColumnName(int column){
return columnNames[column];
}
public void setValueAt (Object aValue, int rowIndex, int columnIndex){
answers[rowIndex][columnIndex] = aValue;
}
public boolean isCellEditable (int rowIndex, int columnIndex){
return true;
}
public void addNewRow(){
Object[] o = new Object[] {"", false};
if ((answers == null) || (answers.length == 0)) {
answers = new Object[][] {o};
}else{
Object[][] answersTemp = new Object[answers.length + 1][AnswerRow.TABLE_COLUMNS];
for (int i = 0; i < answers.length; i++)
answersTemp[i] = answers[i];
answersTemp[answersTemp.length - 1] = o;
answers = answersTemp;
}
}
public List<AnswerRow> getAnswerRows() {
List<AnswerRow> list = new ArrayList<>();
for (Object[] oRow : answers) {
AnswerRow row = new AnswerRow();
row.setAnswer((String) oRow[0]);
row.setRight((boolean) oRow[1]);
list.add(row);
}
return list;
}
public void setAnswerRows(List<AnswerRow> answerRows){
if (answerRows.size() == 0 ) {
this.answers = new Object[0][0];
return;
}
this.answers = new Object[answerRows.size()][AnswerRow.TABLE_COLUMNS];
for (int i = 0; i < answers.length; i++){
answers[i][0] = answerRows.get(i).getAnswer();
answers[i][1] = answerRows.get(i).isRight();
}
this.columnNames = new String[AnswerRow.TABLE_COLUMNS];
this.columnNames[0] = "Odgovor";
this.columnNames[1] = "Točno/Netočno";
}
public class AnswerRow {
public static final int TABLE_COLUMNS = 2;
private boolean isRight;
private String answer;
public AnswerRow(){
answer = "";
isRight = false;
}
public AnswerRow(String answer, boolean isRight){
this.answer = answer;
this.isRight = isRight;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer){
this.answer = answer;
}
public boolean isRight(){
return isRight;
}
public void setRight(boolean isRight){
this.isRight = isRight;
}
}
}
Please help me .. thanks !
List<AnswerRow> lista = new ArrayList<AnswerRow>();
while(rs.next()){
String answerText = rs.getString("answerText");
boolean isRight = rs.getBoolean("answerRight");
//Create AnswerRow instance and set values to it and Add it to list.
AnswersTableModel .AnswerRow ansrow = atm.new AnswerRow();
ansrow.setAnswer(answerText);
ansrow.setRight(isRight);
//Add it to list.
lista.add(ansrow);
}
One thing I am not sure is why you have AnswersTableModel and what you do with that.
Use public AnswerRow(String answer, boolean isRight) constructor to create object
AnswerRow ar=null;
while(rs.next()){
String answerText = rs.getString("answerText");
boolean isRight = rs.getBoolean("answerRight");
ar=new AnswerRow(answerText, isRight);
lista.add(ar);
}

Categories