Incrementing a value within a jTable











up vote
0
down vote

favorite
1












I am working on a project for tracking items, for example a basket for a shopping app.
I have decided to use a table as this is the best method I've found for being able to both add items and remove them using only a touch screen. However, I'm currently having issues with accruing the quantity of an item.
I've used the following code to update the cell which matches a specific items quantity but it seems to stop working when it hits 2 items, without producing an error message.
As a test I tried increasing the initial quantity of an item from 1 to 10 and changing the code so that instead of accruing by 1 each click, it subtracted. This however didn't work as expected instead the quantity goes straight from 10 to 0. Please someone help. I've copied in all the code other than the from code below.



 public void checkmaster(String button){

int MatchFound = 0;
String itemnumber="";
//use button to find item no
try{
BufferedReader in2 = new BufferedReader(new FileReader("C:\Users\kyleg\Desktop\LiveOrderTestFiles\Buttons\"+button+".txt"));
String line2;
System.out.println("reading file");
while((line2 = in2.readLine()) != null){

itemnumber=line2;

}
in2.close();} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

}

//check master
int MasterTableRowCount = jTable2.getRowCount();


for(int i=0; i<MasterTableRowCount; i++){
System.out.println(i);
int row = i ;

String value = jTable2.getModel().getValueAt(row, 0).toString();

if(value.equals(itemnumber)){
int Quantity = Integer.parseInt(jTable2.getModel().getValueAt(row, 2).toString());
updateMasterTable(row,Quantity);
MatchFound=1;
}

}

if(MatchFound==0){addToMasterTable(itemnumber);}

}

public void updateMasterTable(int row,int Quantity){

int NewQuantity = Quantity-1;
jTable2.setValueAt(NewQuantity, row, 1);

}

public void addToMasterTable(String itemnumber){

DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
int rownumber = jTable1.getRowCount();
Vector row = new Vector();
row.add(itemnumber);
row.add("10");
row.add(rownumber+1);

model.addRow(row);

}


The checkmaster class is called when a button is clicked on my frame. It carries a number which is used to check a storage location to see which item is connected to that button and then uses that items number to fill out the table.










share|improve this question
























  • The code above still uses the version of starting at 10 and negating. sorry I didn't realise
    – Kyle Gittings
    Nov 15 at 6:00















up vote
0
down vote

favorite
1












I am working on a project for tracking items, for example a basket for a shopping app.
I have decided to use a table as this is the best method I've found for being able to both add items and remove them using only a touch screen. However, I'm currently having issues with accruing the quantity of an item.
I've used the following code to update the cell which matches a specific items quantity but it seems to stop working when it hits 2 items, without producing an error message.
As a test I tried increasing the initial quantity of an item from 1 to 10 and changing the code so that instead of accruing by 1 each click, it subtracted. This however didn't work as expected instead the quantity goes straight from 10 to 0. Please someone help. I've copied in all the code other than the from code below.



 public void checkmaster(String button){

int MatchFound = 0;
String itemnumber="";
//use button to find item no
try{
BufferedReader in2 = new BufferedReader(new FileReader("C:\Users\kyleg\Desktop\LiveOrderTestFiles\Buttons\"+button+".txt"));
String line2;
System.out.println("reading file");
while((line2 = in2.readLine()) != null){

itemnumber=line2;

}
in2.close();} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

}

//check master
int MasterTableRowCount = jTable2.getRowCount();


for(int i=0; i<MasterTableRowCount; i++){
System.out.println(i);
int row = i ;

String value = jTable2.getModel().getValueAt(row, 0).toString();

if(value.equals(itemnumber)){
int Quantity = Integer.parseInt(jTable2.getModel().getValueAt(row, 2).toString());
updateMasterTable(row,Quantity);
MatchFound=1;
}

}

if(MatchFound==0){addToMasterTable(itemnumber);}

}

public void updateMasterTable(int row,int Quantity){

int NewQuantity = Quantity-1;
jTable2.setValueAt(NewQuantity, row, 1);

}

public void addToMasterTable(String itemnumber){

DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
int rownumber = jTable1.getRowCount();
Vector row = new Vector();
row.add(itemnumber);
row.add("10");
row.add(rownumber+1);

model.addRow(row);

}


The checkmaster class is called when a button is clicked on my frame. It carries a number which is used to check a storage location to see which item is connected to that button and then uses that items number to fill out the table.










share|improve this question
























  • The code above still uses the version of starting at 10 and negating. sorry I didn't realise
    – Kyle Gittings
    Nov 15 at 6:00













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I am working on a project for tracking items, for example a basket for a shopping app.
I have decided to use a table as this is the best method I've found for being able to both add items and remove them using only a touch screen. However, I'm currently having issues with accruing the quantity of an item.
I've used the following code to update the cell which matches a specific items quantity but it seems to stop working when it hits 2 items, without producing an error message.
As a test I tried increasing the initial quantity of an item from 1 to 10 and changing the code so that instead of accruing by 1 each click, it subtracted. This however didn't work as expected instead the quantity goes straight from 10 to 0. Please someone help. I've copied in all the code other than the from code below.



 public void checkmaster(String button){

int MatchFound = 0;
String itemnumber="";
//use button to find item no
try{
BufferedReader in2 = new BufferedReader(new FileReader("C:\Users\kyleg\Desktop\LiveOrderTestFiles\Buttons\"+button+".txt"));
String line2;
System.out.println("reading file");
while((line2 = in2.readLine()) != null){

itemnumber=line2;

}
in2.close();} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

}

//check master
int MasterTableRowCount = jTable2.getRowCount();


for(int i=0; i<MasterTableRowCount; i++){
System.out.println(i);
int row = i ;

String value = jTable2.getModel().getValueAt(row, 0).toString();

if(value.equals(itemnumber)){
int Quantity = Integer.parseInt(jTable2.getModel().getValueAt(row, 2).toString());
updateMasterTable(row,Quantity);
MatchFound=1;
}

}

if(MatchFound==0){addToMasterTable(itemnumber);}

}

public void updateMasterTable(int row,int Quantity){

int NewQuantity = Quantity-1;
jTable2.setValueAt(NewQuantity, row, 1);

}

public void addToMasterTable(String itemnumber){

DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
int rownumber = jTable1.getRowCount();
Vector row = new Vector();
row.add(itemnumber);
row.add("10");
row.add(rownumber+1);

model.addRow(row);

}


The checkmaster class is called when a button is clicked on my frame. It carries a number which is used to check a storage location to see which item is connected to that button and then uses that items number to fill out the table.










share|improve this question















I am working on a project for tracking items, for example a basket for a shopping app.
I have decided to use a table as this is the best method I've found for being able to both add items and remove them using only a touch screen. However, I'm currently having issues with accruing the quantity of an item.
I've used the following code to update the cell which matches a specific items quantity but it seems to stop working when it hits 2 items, without producing an error message.
As a test I tried increasing the initial quantity of an item from 1 to 10 and changing the code so that instead of accruing by 1 each click, it subtracted. This however didn't work as expected instead the quantity goes straight from 10 to 0. Please someone help. I've copied in all the code other than the from code below.



 public void checkmaster(String button){

int MatchFound = 0;
String itemnumber="";
//use button to find item no
try{
BufferedReader in2 = new BufferedReader(new FileReader("C:\Users\kyleg\Desktop\LiveOrderTestFiles\Buttons\"+button+".txt"));
String line2;
System.out.println("reading file");
while((line2 = in2.readLine()) != null){

itemnumber=line2;

}
in2.close();} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

}

//check master
int MasterTableRowCount = jTable2.getRowCount();


for(int i=0; i<MasterTableRowCount; i++){
System.out.println(i);
int row = i ;

String value = jTable2.getModel().getValueAt(row, 0).toString();

if(value.equals(itemnumber)){
int Quantity = Integer.parseInt(jTable2.getModel().getValueAt(row, 2).toString());
updateMasterTable(row,Quantity);
MatchFound=1;
}

}

if(MatchFound==0){addToMasterTable(itemnumber);}

}

public void updateMasterTable(int row,int Quantity){

int NewQuantity = Quantity-1;
jTable2.setValueAt(NewQuantity, row, 1);

}

public void addToMasterTable(String itemnumber){

DefaultTableModel model = (DefaultTableModel) jTable2.getModel();
int rownumber = jTable1.getRowCount();
Vector row = new Vector();
row.add(itemnumber);
row.add("10");
row.add(rownumber+1);

model.addRow(row);

}


The checkmaster class is called when a button is clicked on my frame. It carries a number which is used to check a storage location to see which item is connected to that button and then uses that items number to fill out the table.







java loops for-loop jtable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 11:03









Brian Tompsett - 汤莱恩

4,1631336100




4,1631336100










asked Nov 15 at 5:52









Kyle Gittings

11




11












  • The code above still uses the version of starting at 10 and negating. sorry I didn't realise
    – Kyle Gittings
    Nov 15 at 6:00


















  • The code above still uses the version of starting at 10 and negating. sorry I didn't realise
    – Kyle Gittings
    Nov 15 at 6:00
















The code above still uses the version of starting at 10 and negating. sorry I didn't realise
– Kyle Gittings
Nov 15 at 6:00




The code above still uses the version of starting at 10 and negating. sorry I didn't realise
– Kyle Gittings
Nov 15 at 6:00

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313225%2fincrementing-a-value-within-a-jtable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313225%2fincrementing-a-value-within-a-jtable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?