|
 |
|
สอบถามการคำนวณราคาถายในตารางโดยใช้ for วนหน่อยครับ Java GUI |
|
 |
|
|
 |
 |
|
Code (Java)
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();
for (int i = 0 ; i < nRow ; i++)
{
//dtm.getValueAt(i,0);
}
ตัวอย่างการ Loop ค่าใน jTable ครับ ที่เหลือคุณก็เอาค่ามา Sum กันครับ
|
 |
 |
 |
 |
Date :
2015-02-25 21:47:26 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 2 เขียนโดย : theerat56 เมื่อวันที่ 2015-03-08 22:38:32
รายละเอียดของการตอบ ::
Code ปุ่มเพิ่มสินค้าครับ ขอบคุณ Admin ครับ
Code (Java)
Object[] data = new Object[0];
DefaultTableModel std = new DefaultTableModel();
std = (DefaultTableModel) table2.getModel();
std.addRow(data);
int row = table.getSelectedRow();
if(row==-1){
JOptionPane.showMessageDialog(null,"กรูณาค้นสินค้าและเลือกสินค้าก่อน");
DefaultTableModel tablestd = new DefaultTableModel();
tablestd = (DefaultTableModel) table2.getModel();
int row3 = table2.getSelectedRow(); //ดึงหมายเลขแถวที่ผู้ใช้เลือก
tablestd.removeRow(row3);
count--;
txtno.setText(""+count);
return;
}else{
int row1 = table2.getRowCount()-1;
String id = table.getValueAt(row, 0).toString();
String name = table.getValueAt(row, 1).toString();
String unit = table.getValueAt(row, 2).toString();
String price = table.getValueAt(row, 3).toString();
String amount = table.getValueAt(row, 4).toString();
String unitbuy2 = JOptionPane.showInputDialog("ใส่จำนวนที่ต้องการซื้อ");
if(unitbuy2.equalsIgnoreCase("")){
JOptionPane.showMessageDialog(null,"กรุณากรอกจำนวน");
DefaultTableModel tablestd = new DefaultTableModel();
tablestd = (DefaultTableModel) table2.getModel(); //ดึงหมายเลขแถวที่ผู้ใช้เลือก
tablestd.removeRow(row1);
txtno.setText(""+count);
return;
}
txtno.setText(""+count);
table2.setValueAt(count++, row1, 0);
table2.setValueAt(id, row1, 1);
table2.setValueAt(name, row1,2);
table2.setValueAt(unit, row1, 3);
int unitbuy1 = Integer.parseInt(unitbuy2);
int amount1 = Integer.parseInt(amount);
if(unitbuy1>amount1){
JOptionPane.showMessageDialog(null,"สินค้าไม่พอขาย");
DefaultTableModel tablestd = new DefaultTableModel();
tablestd = (DefaultTableModel) table2.getModel(); //ดึงหมายเลขแถวที่ผู้ใช้เลือก
tablestd.removeRow(row1);
count--;
txtno.setText(""+count);
return;
}
int pricebuy = Integer.parseInt(price);
int sum = unitbuy1 * pricebuy;
table2.setValueAt(unitbuy1, row1, 4);
table2.setValueAt(sum, row1, 5);
String showtotal2 = table2.getValueAt(row1, 5).toString();
int sum1;
sum1 = Integer.parseInt(showtotal2);
total = total + sum1;
String show = String.valueOf(total);
showtotal1.setText(show);
callcount();
}
|
 |
 |
 |
 |
Date :
2015-03-08 22:39:42 |
By :
theerat56 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|