An integer not written to the label

if (combostyle.getSelectedItem().equals(" ") || (comboSize.getSelectedItem().equals(" ")) || (comboclr.getSelectedItem().equals(" ")) ) {
    lblqtot.setText(String.valueOf("Please complete the form"));           
}

else if (comboquant.getSelectedItem().equals("15")) {
    int totals = Integer.parseInt(lblTotal.getText());
    int quantity = 15;

            int total = totals * quantity;
            String total2 = String.valueOf(total);

            lblqtot.setText(total2);
            label1.setText(total2);
              repaint();
              this.repaint();
              super.repaint();
                   }
}    

      

The problem you have is that the label for total (lblqtot) is not written with the value total2. Basically, I have a total price that I need to multiply by a combo box, so if the combo box selection (for quantity) is 15, then the sum is multiplied by 15 to give the total. Hope this makes sense ...

At the moment, however, nothing is happening with the label, and yet I am not getting any errors?

+3


source to share


2 answers


the problem might be in your JLabel - it doesn't update even if you assigned a new value to it. The JLabel lives inside a JFrame, which, once created, tends to keep its elements as they are. What you need to do is update the frame:



frame.invalidate(); frame.validate(); frame.repaint();

0


source


Your problem is what you are calling getSelectedItem()

that returns Object

. Either you can convert it to int

(for example ((Integer)comboquant.getSelectedItem()).intValue() == 15

), or if the index of the element matches its value, you can use getSelectedIndex()

(or getSelectedIndex() + 1

if you start with 1

).



Alternatively, if you are manipulating an array with values int

like they are in a list, you can simply do it like this:arr[comboStyle.getSelectedIndex()]

0


source







All Articles