Abstract Table Model isCellEditable() Overriding did not make cells editable - java

I am implementing a JTable with a TableModel.
I've got a problem to make cells editable on this specific one.
Here is my isCellEditable overriding of the TableModel
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
The Jtable instanciate with this TableModel doesn't make cells editable.
First I instanciate my JScrollPane with The JTable in it :
caseTM = new CaseTableModel(factory,caseList);
caseListTable = new JTable(caseTM);
caseScrollPanel = new JScrollPane(caseListTable);
Here is the code of the CaseTableModel :
public class CaseTableModel extends AbstractTableModel {
Factory f;
ArrayList<Case> cList;
public CaseTableModel(Factory f,ArrayList<Case> caseList) {
this.f=f;
cList=caseList;
}
#Override
public Class<?> getColumnClass(int columnIndex) {
return cList.get(0).getClass();
}
#Override
public int getColumnCount() {
return 14;
}
#Override
public String getColumnName(int columnIndex) {
switch (columnIndex) {
case 0:
return "Numéro d'Affaire";
case 1:
return "Client";
case 2:
return "Projet";
case 3:
return "Position";
case 4:
return "Status";
case 5:
return "Date Début";
case 6:
return "Date Fin";
case 7:
return "Nombre de jours facturés";
case 8:
return "Daily Rate";
case 9:
return "IGD";
case 10:
return "Autres Conditions";
case 11:
return "Echeance de paiement";
case 12:
return "Date de paiment";
case 13:
return "Montant HT";
case 14:
return "Montant TTC";
default:
return "FAUX!";
}
}
#Override
public int getRowCount() {
return cList.size();
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
return cList.get(rowIndex).getNumber();
case 1:
return cList.get(rowIndex).getClient();
case 2:
return cList.get(rowIndex).getProject();
case 3:
return cList.get(rowIndex).getPositon();
case 4:
return cList.get(rowIndex).getStatus();
case 5:
return cList.get(rowIndex).getStartDate();
case 6:
return cList.get(rowIndex).getEndDate();
case 7:
return cList.get(rowIndex).getNumberOfDayInvoice();
case 8:
return cList.get(rowIndex).getDailyRate();
case 9:
return cList.get(rowIndex).getDailyIndemnity();
case 10:
return cList.get(rowIndex).getOtherConditions();
case 11:
return cList.get(rowIndex).getPaymentDueDate();
case 12:
return cList.get(rowIndex).getPaymentDate();
case 13:
return cList.get(rowIndex).getAmountTaxFree();
case 14:
return cList.get(rowIndex).getAmountWithTax();
default:
return "FAUX!";
}
}
#Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
#Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
cList.get(rowIndex).setNumber((String) aValue);
break;
case 1:
cList.get(rowIndex).setClient((String) aValue);
break;
case 2:
cList.get(rowIndex).setProject((String) aValue);
break;
case 3:
cList.get(rowIndex).setPositon((String) aValue);
break;
case 4:
cList.get(rowIndex).setStatus((String) aValue);
break;
case 5:
cList.get(rowIndex).setStartDate((Integer) aValue);
break;
case 6:
cList.get(rowIndex).setEndDate((Integer) aValue);
break;
case 7:
cList.get(rowIndex).setNumberOfDayInvoice((Integer) aValue);
break;
case 8:
cList.get(rowIndex).setDailyRate((Double) aValue);
break;
case 9:
cList.get(rowIndex).setDailyIndemnity((Double) aValue);
break;
case 10:
cList.get(rowIndex).setOtherConditions((String) aValue);
break;
case 11:
cList.get(rowIndex).setPaymentDueDate((Integer) aValue);
break;
case 12:
cList.get(rowIndex).setPaymentDate((Integer) aValue);
break;
case 13:
cList.get(rowIndex).setAmountTaxFree((Double) aValue);
break;
case 14:
cList.get(rowIndex).setAmountWithTax((Double) aValue);
break;
default:
break;
}
f.updateCase(cList.get(rowIndex));
}
public void fireTableDataChangedForThisEmployee(Employee e){
cList = f.getCasesForThisEmployee(e);
super.fireTableDataChanged();
}
}

Related

How to show the number of each row

Hello I want to show the index number of each row in the table I tried with for loop but I had no success can you tell me how to do that ?
I need a logic that return the number of each row in the line "case 0: return getRowCount();" it return the total of rows only
public class TableModel extends AbstractTableModel{
UsersDao ud = new usersDao();
private List<Users> users;
public TableModel() throws Exception {
this.users = (ArrayList<Users>)ud.getUsersList();
}
private DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
#Override
public int getRowCount() {
return users.size();
}
#Override
public int getColumnCount() {
return 10;
}
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
Users u = users.get(rowIndex);
switch(columnIndex){
case 0: return getRowCount();
case 1: return u.getName();
case 2: return u.getAge();
case 3: return u.getGender();
default: return "";
}
}
public String getColumnName(int column){
switch(column){
case 0: return "NO";
case 1: return "NAME";
case 2: return "AGE";
case 3: return "GENDER";
default: return "";
}
}
public void addUser(Users u){
users.add(u);
fireTableRowsInserted(users.size()-1, users.size()-1);
}
public void deletePatient(Users u){
users.remove(p);
fireTableRowsInserted(users.size()-1, users.size()-1);
}
}
I think you have a problem in getValueAt
#Override
public Object getValueAt(int rowIndex, int columnIndex) {
Users u = users.get(rowIndex);
switch(columnIndex){
case 0: return rowIndex; // return rowIndex rather than the total number of rows
case 1: return u.getName();
case 2: return u.getAge();
case 3: return u.getGender();
default: return "";
}
}

Switch while loop

I have a basic method that implements controlling of application menu using switch
public void applicationMenu(String input) {
switch (input) {
case "1":
findGroups();
break;
case "2":
findStudentsByCourseName();
break;
case "3":
addNewStudent();
break;
case "4":
deleteStudentById();
break;
case "5":
addStudentToCourse();
break;
case "6":
removeStudentCourse();
break;
default:
printDefault();
break;
}
}
I use this method with a while loop to call my application menu
public void callMenu() {
boolean exit = false;
while (!exit) {
viewProvider.printMainMenu();
String input = viewProvider.readString();
if (input.equals("7")) {
exit = true;
}
applicationMenu(input);
}
}
How can I trigger exit from switch case but keep the structure of two methods at the same time?
This should work:
public boolean applicationMenu(String input) {
boolean shouldContinue = true;
switch (input) {
case "1":
findGroups();
break;
case "2":
findStudentsByCourseName();
break;
case "3":
addNewStudent();
break;
case "4":
deleteStudentById();
break;
case "5":
addStudentToCourse();
break;
case "6":
removeStudentCourse();
break;
case "7":
shouldContinue = false;
break;
default:
printDefault();
break;
}
return shouldContinue;
}
...
public void callMenu() {
while (true) {
viewProvider.printMainMenu();
String input = viewProvider.readString();
if (!applicationMenu(input)) {
break;
}
}
}
As stated in comments, you could throw an Exception, but I typically don't like to do that if i'm not in an actual error state. It makes more sense to me to use a return value and evaluate the result to determine if the program should terminate:
public void callMenu() {
boolean exit = false;
while (!exit) {
viewProvider.printMainMenu();
exit = applicationMenu(viewProvider.readString());
}
}
public boolean applicationMenu(String input) {
switch (input) {
case "1":
findGroups();
return false;
case "2":
findStudentsByCourseName();
return false;
case "3":
addNewStudent();
return false;
case "4":
deleteStudentById();
return false;
case "5":
addStudentToCourse();
return false;
case "6":
removeStudentCourse();
return false;
case "7":
return true;
default:
printDefault();
}
return false;
}

How can I implement a string stack with a switch statement?

I would like to implement my StringStack in a switch statement how can i make this work in java. it sais i cant push() and argument with a char value. What is the best way around this for my validation method?
package xmlvalidator;
public class BasicXmlValidator implements XmlValidator {
#Override
public String[] validate(String xmlDocument) {
// TODO Auto-generated method stub
int charIndex = 0;
char currentCharacter;
String characterString;
while (charIndex < xmlDocument.length()) {
currentCharacter = xmlDocument.charAt(charIndex);
characterString = Character.toString(currentCharacter);
switch (currentCharacter) {
case '(': StringStack.push(currentCharacter);
break;
case '[': StringStack.push(currentCharacter);
break;
case '{': StringStack.push(currentCharacter);
break;
case ')': StringStack.push(currentCharacter);
break;
case ']': StringStack.push(currentCharacter);
break;
case '}': StringStack.push(currentCharacter);
break;
}
}
return null;
}
}
package xmlvalidator;
import static java.lang.System.*;
public class BasicStringStack implements StringStack {
public int count; // Number of Items in the array
public String[] stackItems; // The array that holds the stack items
public BasicStringStack(int initialSize) {
count = 0;
stackItems = new String[initialSize];
}
#Override
public void push(String item) {
// TODO Auto-generated method stub
if (count == stackItems.length) {
int newLength = (stackItems.length + 1);
String[] tempArray = new String[newLength];
arraycopy(stackItems, 0, tempArray, 0, stackItems.length);
stackItems = tempArray;
}
stackItems[count++] = item;
}
#Override
public String pop() {
if (count == 0) {
return null;
} else {
return stackItems[--count];
}
}
#Override
public String peek(int position) {
if ((position > count - 1) || (position < 0)) {
return null; // outside Bounds
} else {
return stackItems[count - position - 1];
}
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return count;
}
}
You have the current char stored in a string using characterString = Character.toString(currentCharacter); just use it to push into stack.
StringStack.push(currentCharacter);
change to
StringStack.push(currentString);
Your problem is exactly as Java describes it to you. You pass a char to a method that expects String. The best fix would be
Stringstack.push(characterString);
Aside: because switch lets you fall through, you can rewrite it as
switch (currentCharacter) {
case '(':
case '[':
case '{':
case ')':
case ']':
case '}':
StringStack.push(characterString);
break;
}
Further aside: I did not address other potential problems in the code, but sought only to address the question asked.

Error Parsing XML response attributes with ksoap2 returns ClassCastException

I'm trying to figure out how to cast the response of the consume of a webservice, when casting the response envelope.bodyIn to my extended class object "BiometricConfigurationResponse" i'm getting this error:
java.lang.ClassCastException: org.ksoap2.serialization.SoapObject cannot be cast to org.tempuri.BiometricConfigurationResponse
The service is responding well and if i not cast it i get the dump right.
Any ideas?
This is what i'm doing:
BiometricConfigurationResponse response= null;
SoapObject obj = new SoapObject (wsNameSpace, methodName);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.implicitTypes = true;
envelope.dotNet = true;
envelope.setOutputSoapObject(obj);
envelope.addMapping(wsNameSpace, "BiometricConfigurationResponse", new BiometricConfigurationResponse().getClass());
HttpTransportSE androidHttpTransport = new HttpTransportSE(wsURL);
androidHttpTransport.debug = true;
try {
String soapAction=wsNameSpace + methodName;
androidHttpTransport.call(soapAction, envelope);
System.out.println(androidHttpTransport.requestDump);
response = (BiometricConfigurationResponse)envelope.bodyIn;
}
catch (Exception e) {
e.printStackTrace();
}
And this is my custom class
package org.tempuri;
import java.util.Hashtable;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
public final class BiometricConfigurationResponse extends SoapObject {
private int coderAlgorithm;
private int templateFormat;
private boolean juvenileMode;
private int qualityThreshold;
private boolean retryAcquisition;
private boolean acceptBadQualityEnrollment;
private boolean showQualityBar;
private boolean showQualityThreshold;
private int timeout;
private int timeoutQualityCoder;
private int enrollSecurityLevel;
private boolean securityLevelCompatibility;
private boolean liveImage;
private java.lang.String setCulture;
private int authenticationScore;
public BiometricConfigurationResponse() {
super("", "");
}
public void setCoderAlgorithm(int coderAlgorithm) {
this.coderAlgorithm = coderAlgorithm;
}
public int getCoderAlgorithm(int coderAlgorithm) {
return this.coderAlgorithm;
}
public void setTemplateFormat(int templateFormat) {
this.templateFormat = templateFormat;
}
public int getTemplateFormat(int templateFormat) {
return this.templateFormat;
}
public void setJuvenileMode(boolean juvenileMode) {
this.juvenileMode = juvenileMode;
}
public boolean getJuvenileMode(boolean juvenileMode) {
return this.juvenileMode;
}
public void setQualityThreshold(int qualityThreshold) {
this.qualityThreshold = qualityThreshold;
}
public int getQualityThreshold(int qualityThreshold) {
return this.qualityThreshold;
}
public void setRetryAcquisition(boolean retryAcquisition) {
this.retryAcquisition = retryAcquisition;
}
public boolean getRetryAcquisition(boolean retryAcquisition) {
return this.retryAcquisition;
}
public void setAcceptBadQualityEnrollment(boolean acceptBadQualityEnrollment) {
this.acceptBadQualityEnrollment = acceptBadQualityEnrollment;
}
public boolean getAcceptBadQualityEnrollment(boolean acceptBadQualityEnrollment) {
return this.acceptBadQualityEnrollment;
}
public void setShowQualityBar(boolean showQualityBar) {
this.showQualityBar = showQualityBar;
}
public boolean getShowQualityBar(boolean showQualityBar) {
return this.showQualityBar;
}
public void setShowQualityThreshold(boolean showQualityThreshold) {
this.showQualityThreshold = showQualityThreshold;
}
public boolean getShowQualityThreshold(boolean showQualityThreshold) {
return this.showQualityThreshold;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public int getTimeout(int timeout) {
return this.timeout;
}
public void setTimeoutQualityCoder(int timeoutQualityCoder) {
this.timeoutQualityCoder = timeoutQualityCoder;
}
public int getTimeoutQualityCoder(int timeoutQualityCoder) {
return this.timeoutQualityCoder;
}
public void setEnrollSecurityLevel(int enrollSecurityLevel) {
this.enrollSecurityLevel = enrollSecurityLevel;
}
public int getEnrollSecurityLevel(int enrollSecurityLevel) {
return this.enrollSecurityLevel;
}
public void setSecurityLevelCompatibility(boolean securityLevelCompatibility) {
this.securityLevelCompatibility = securityLevelCompatibility;
}
public boolean getSecurityLevelCompatibility(boolean securityLevelCompatibility) {
return this.securityLevelCompatibility;
}
public void setLiveImage(boolean liveImage) {
this.liveImage = liveImage;
}
public boolean getLiveImage(boolean liveImage) {
return this.liveImage;
}
public void setSetCulture(java.lang.String setCulture) {
this.setCulture = setCulture;
}
public java.lang.String getSetCulture(java.lang.String setCulture) {
return this.setCulture;
}
public void setAuthenticationScore(int authenticationScore) {
this.authenticationScore = authenticationScore;
}
public int getAuthenticationScore(int authenticationScore) {
return this.authenticationScore;
}
public int getPropertyCount() {
return 15;
}
public Object getProperty(int __index) {
switch(__index) {
case 0: return new Integer(coderAlgorithm);
case 1: return new Integer(templateFormat);
case 2: return new Boolean(juvenileMode);
case 3: return new Integer(qualityThreshold);
case 4: return new Boolean(retryAcquisition);
case 5: return new Boolean(acceptBadQualityEnrollment);
case 6: return new Boolean(showQualityBar);
case 7: return new Boolean(showQualityThreshold);
case 8: return new Integer(timeout);
case 9: return new Integer(timeoutQualityCoder);
case 10: return new Integer(enrollSecurityLevel);
case 11: return new Boolean(securityLevelCompatibility);
case 12: return new Boolean(liveImage);
case 13: return setCulture;
case 14: return new Integer(authenticationScore);
}
return null;
}
public void setProperty(int __index, Object __obj) {
switch(__index) {
case 0: coderAlgorithm = Integer.parseInt(__obj.toString()); break;
case 1: templateFormat = Integer.parseInt(__obj.toString()); break;
case 2: juvenileMode = "true".equals(__obj.toString()); break;
case 3: qualityThreshold = Integer.parseInt(__obj.toString()); break;
case 4: retryAcquisition = "true".equals(__obj.toString()); break;
case 5: acceptBadQualityEnrollment = "true".equals(__obj.toString()); break;
case 6: showQualityBar = "true".equals(__obj.toString()); break;
case 7: showQualityThreshold = "true".equals(__obj.toString()); break;
case 8: timeout = Integer.parseInt(__obj.toString()); break;
case 9: timeoutQualityCoder = Integer.parseInt(__obj.toString()); break;
case 10: enrollSecurityLevel = Integer.parseInt(__obj.toString()); break;
case 11: securityLevelCompatibility = "true".equals(__obj.toString()); break;
case 12: liveImage = "true".equals(__obj.toString()); break;
case 13: setCulture = (java.lang.String) __obj; break;
case 14: authenticationScore = Integer.parseInt(__obj.toString()); break;
}
}
public void getPropertyInfo(int __index, Hashtable __table, PropertyInfo __info) {
switch(__index) {
case 0:
__info.name = "coderAlgorithm";
__info.type = Integer.class; break;
case 1:
__info.name = "templateFormat";
__info.type = Integer.class; break;
case 2:
__info.name = "juvenileMode";
__info.type = Boolean.class; break;
case 3:
__info.name = "qualityThreshold";
__info.type = Integer.class; break;
case 4:
__info.name = "retryAcquisition";
__info.type = Boolean.class; break;
case 5:
__info.name = "acceptBadQualityEnrollment";
__info.type = Boolean.class; break;
case 6:
__info.name = "showQualityBar";
__info.type = Boolean.class; break;
case 7:
__info.name = "showQualityThreshold";
__info.type = Boolean.class; break;
case 8:
__info.name = "timeout";
__info.type = Integer.class; break;
case 9:
__info.name = "timeoutQualityCoder";
__info.type = Integer.class; break;
case 10:
__info.name = "enrollSecurityLevel";
__info.type = Integer.class; break;
case 11:
__info.name = "securityLevelCompatibility";
__info.type = Boolean.class; break;
case 12:
__info.name = "liveImage";
__info.type = Boolean.class; break;
case 13:
__info.name = "setCulture";
__info.type = java.lang.String.class; break;
case 14:
__info.name = "authenticationScore";
__info.type = Integer.class; break;
}
}
}
After several days of research i understood that WSDL autogenerated code parse Properties but not Atrributes. So what i did was this:
First i generate the stub code from my WSDL in this website: http://www.wsdl2code.com/pages/home.aspx
My webservice name is "nutriment" son when you call it in MainActivity you have to do this:
nutriment nws= new nutriment();
BiometricConfigurationResponse respuesta = nws.GetBiometricConfiguration();
Log.i(TAG, String.valueOf(respuesta.coderAlgorithm));
Log.i(TAG, String.valueOf(respuesta.templateFormat));
But it responses 0 in all cases because the WDSL stub files are parsing PROPERTIES NOT ATRIBUTES, so when you open the serialization generated file you'll got something like this:
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
import java.util.Hashtable;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
public class BiometricConfigurationResponse implements KvmSerializable {
public int coderAlgorithm;
public int templateFormat;
public BiometricConfigurationResponse(){}
public BiometricConfigurationResponse(SoapObject soapObject)
{
if (soapObject == null)
return;
if (soapObject.hasProperty("coderAlgorithm"))
{
Object obj = soapObject.getProperty("coderAlgorithm");
if (obj != null && obj.getClass().equals(SoapObject.class)){
SoapPrimitive j =(SoapPrimitive) obj;
coderAlgorithm = Integer.parseInt(j.toString());
}
else if (obj!= null && obj instanceof Number){
coderAlgorithm = (Integer) obj;
}
}
if (soapObject.hasProperty("templateFormat"))
{
Object obj = soapObject.getProperty("templateFormat");
if (obj != null && obj.getClass().equals(SoapPrimitive.class)){
SoapPrimitive j =(SoapPrimitive) obj;
templateFormat = Integer.parseInt(j.toString());
}else if (obj!= null && obj instanceof Number){
templateFormat = (Integer) obj;
}
}
}
#Override
public Object getProperty(int arg0) {
switch(arg0){
case 0:
return coderAlgorithm;
case 1:
return templateFormat;
}
return null;
}
#Override
public int getPropertyCount() {
return 15;
}
#Override
public void getPropertyInfo(int index, #SuppressWarnings("rawtypes") Hashtable arg1, PropertyInfo info) {
switch(index){
case 0:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "coderAlgorithm";
break;
case 1:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "templateFormat";
break;
}
#Override
public void setProperty(int arg0, Object arg1) {
}
#Override
public String getInnerText() {
// TODO Auto-generated method stub
return null;
}
#Override
public void setInnerText(String arg0) {
// TODO Auto-generated method stub
}
}
The trick is to modify the parsing so instead of get the properties(e.g.):
if (soapObject.hasProperty("coderAlgorithm")){
Object obj = soapObject.getProperty("coderAlgorithm");
if (obj != null && obj.getClass().equals(SoapObject.class)){
SoapPrimitive j =(SoapPrimitive) obj;
coderAlgorithm = Integer.parseInt(j.toString());
}
else if (obj!= null && obj instanceof Number){
coderAlgorithm = (Integer) obj;
}
}
Get the ATTRIBUTES (e.g.):
if (soapObject.hasAttribute("coderAlgorithm")) {
Object obj = soapObject.getAttribute("coderAlgorithm");
if (obj != null && obj.getClass().equals(SoapObject.class)){
SoapPrimitive j =(SoapPrimitive) obj;
coderAlgorithm = Integer.parseInt(j.toString());
}
else if (obj!= null && obj instanceof Number){
coderAlgorithm = (Integer) obj;
}
else if (obj!= null && obj instanceof String){
coderAlgorithm = Integer.parseInt(obj.toString());
}
}

onOptionsItemSelected(MenuItem item) strange behavior

I've created a menu with a intents to access different activities, but I have a strange behavior, it always goes through all the cases of the switch statement after the statement selected , I've reviewed the value of the variable item and is correct, any ideas what could be wrong?
the snippet of code that represents the menu is:
public static final int wiifidi = 0;
public static final int cuentaint = 1;
public static final int cajerosint = 2;
public static final int indicadoresint = 3;
public static final int promoint = 5;
public static final int contactoint = 4;
....
....
....
#Override
//add the items to the menu on the class
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0,wiifidi, 0, R.string.menu_wifi);
menu.add(0,cuentaint, 0, R.string.menu_cuenta);
menu.add(0,cajerosint,0,R.string.menu_cajeros);
menu.add(0,indicadoresint,0,R.string.menu_indicadores);
menu.add(0,contactoint,0,R.string.menu_contacto);
menu.add(0,promoint,0,R.string.menu_promo);
return result;
}
#Override
//handle everything that happens when an item of menu is selected
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(getApplicationContext(), "el item es " +item.getItemId(), Toast.LENGTH_LONG).show();
switch (item.getItemId()) {
case wiifidi:
wifistatus();
case cuentaint:{
consulta();
}
case cajerosint:{
cajero();
}
case indicadoresint:{
indicador();
}
case contactoint:{
contacto();
}
case promoint:{
promocion();
}
}
return super.onOptionsItemSelected(item);
}
Remember to break out of your switches.
switch (item.getItemId())
{
case wiifidi:
wifistatus();
break;
case cuentaint:
consulta();
break;
case cajerosint:
cajero();
break;
case indicadoresint:
indicador();
break;
case contactoint:
contacto();
break;
case promoint:
promocion();
break;
}
Specify break
case wiifidi:
wifistatus();
break;

Categories