2011-11-22 3 views
0

"Sale"버튼을 클릭하면 표가 첫 번째 행에 표시되어야하고, 어떤 항목을 판매했는지, 얼마인지를 표시해야합니다. 하지만 두 번째 클릭하면 마지막 행을 삭제하고 새 테이블을 만듭니다. 새 데이터로 새 행을 만들려면 어떻게해야합니까? 이 내 코드입니다 :JButton을 클릭 한 후 JTable을 업데이트하십시오.

if (e.getSource()==sale) 
    { 
     int tempcod=0,tempcant=0; //tempcod is the code of the product, 
     temocant is the number of products sold 

    final DefaultTableModel model = new DefaultTableModel(); 

     model.addColumn("Product"); 
     model.addColumn("Price"); 

     try 
     { 
     tempcod=Integer.parseInt(cod.getText()); 
     tempcant=Integer.parseInt(quantity.getText()); 
     } 
     catch(NumberFormatException a) 
     { 
      JOptionPane.showMessageDialog(null, "Invalid Number"); 
     } 

     for (int j=0;j<nulo; j++) //nulo is the number of items at sale 
     { 

      if (tempcod==codigo[j]) //id the code i wrote exist in 
             the list of products 
      { 
       for (int k=0; k<nulo; k++) 
       { 
        if (tempcod==ventaActual[0][k]) 
        { 
         ventaActual[1][k]+=tempcant; 
        } 
        else 
        { 
        ventaActual[0][k]=tempcod; 
        ventaActual[1][k]=tempcant; 
        } 
       } 

       model.insertRow(model.getRowCount(), new Object[]{item[j],price[j]*tempcant});//this line creates the row of the table. 
      //item[] is the name of the item, price[] is the price of the item 
       JTable Lista= new JTable(model); 
       lista1=new JScrollPane(Lista); 
       lista1.setBounds(170,150,200,200); 
       lista1.setEnabled(false); 
       lista1.setVisible(true); 
       this.add(lista1); 

       a+=(tempcant*precio[j]); 
       String b=Integer.toString(a); 
       tot.setText(b); 
       ventaInforme=ventaActual; 
      } 
      else 
      { 
       cod.setText(null); 
       cantidad_n.setText(null); 
      } 
     } 
    } 

답변

3

model.insertRow에 대한 귀하의 호출이 테이블에 행을 삽입하고 자동으로 업데이트하기 위해 테이블에 필요한 이벤트를 발생한다. 그러나 기존 테이블에서 기존 모델을 가져온 다음 해당 모델을 업데이트해야합니다.

DefaultTableModel model = (DefaultTableModel) myTable.getModel();

원하는 코드가 아니라면 생성 한 코드를 제거하고 컨테이너에 새 테이블을 추가해야합니다.

+0

당신은 선생님입니다. – aerojun

관련 문제