Incrementing a value within a jTable
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
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
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
java loops for-loop jtable
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
add a comment |
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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