Home > other >  Java Jtable Not Updating External Values
Java Jtable Not Updating External Values

Time:01-16

Im coding this math progam where the user buys and sells stocks and the table calculates the valuation based on price and amount of stocks the user has. (valuation = price*amt)

I used a dropdown box to allow users to choose which stock he would like to buy / sell but now i realised that since the program only enters 1 if statement at a time, the valuation for the other stocks do not get updated. (the price changes based on random number generated each day)

int selectedOption = jComboBox.getSelectedIndex();
        int newAMT = Integer.parseInt(buySellField.getText()); 
        System.out.print(selectedOption);

        if (selectedOption == 0){
          Object abcAmtPulled = jTable.getModel().getValueAt( 0, 2);
          int i = (int) abcAmtPulled;
          int finalABCAmt = newAMT   i;
          Object valuationABC = df.format(newABCPrice*finalABCAmt);
          jTable.getModel().setValueAt(finalABCAmt, 0, 2);
          jTable.getModel().setValueAt(valuationABC, 0, 3);
        }
        else if (selectedOption == 1){
          Object bmcAmtPulled = jTable.getModel().getValueAt( 1, 2);
          int i = (int) bmcAmtPulled;
          int finalBMCAmt = newAMT   i;
          Object valuationBMC = df.format(newBMCPrice*finalBMCAmt);
          jTable.getModel().setValueAt(finalBMCAmt, 1, 2);
          jTable.getModel().setValueAt(valuationBMC, 1, 3);
        }


I cant move the valuation calculation out of the if statements but i was wondering if there was a way to calculate the other valuations(in the other if statements) without manually getting value of all the prices and amount and multiplying in every if statement?

[enter image description here]

CodePudding user response:

Based on your tiny code fragment, and not a minimal reproducible example that we can copy into our IDE, compile, run, and test, you can try this:

    int selectedOption = jComboBox.getSelectedIndex();
    int newAMT = Integer.parseInt(buySellField.getText());
    System.out.print(selectedOption);

    if (selectedOption == 0) {
        Object abcAmtPulled = jTable.getModel().getValueAt(0, 2);
        int i = (int) abcAmtPulled;
        int finalABCAmt = newAMT   i;
        jTable.getModel().setValueAt(finalABCAmt, 0, 2);
    } else if (selectedOption == 1) {
        Object bmcAmtPulled = jTable.getModel().getValueAt(1, 2);
        int i = (int) bmcAmtPulled;
        int finalBMCAmt = newAMT   i;
        jTable.getModel().setValueAt(finalBMCAmt, 1, 2);
    }

    Object abcAmtPulled = jTable.getModel().getValueAt(0, 2);
    int i = (int) abcAmtPulled;
    Object valuationABC = df.format(newABCPrice * i);
    jTable.getModel().setValueAt(valuationABC, 0, 3);
    
    Object bmcAmtPulled = jTable.getModel().getValueAt(1, 2);
    i = (int) bmcAmtPulled;
    Object valuationBMC = df.format(newBMCPrice * i);
    jTable.getModel().setValueAt(valuationBMC, 1, 3);
  • Related