This is the output:
https://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-prn1/541619_462450913826230_932486202_n.jpg
This happens when I input "1" in the quantity textfield.
(The quantity textfield turns null after inputing a value)

But when I put "1" in the quantity textfield and click Oranges, it overwrites the Apples.
https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-ash4/299813_462450910492897_2137805386_n.jpg

WHat i wanted to happen is that it'll tally my selections. Like this:
((I ONLY EDITED THIS))
https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-prn1/602666_462450900492898_1863944306_n.jpg

These are my codes so far:

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class Main extends JFrame implements ActionListener{

    JLabel lblTitle, lblQty, lblTotal;
    JRadioButton radApple, radOrange, radPomelo;
    JTextField txtQty;
    JTextArea txtScreen;
    Button btnAdd, btnPrint, btnCancel, btnExit, btnStock;

    ButtonGroup bg;

    Main(){
        getContentPane();

        setSize(550,490);
        setVisible(true);
        setLayout(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Tricia's Grocery Store");

        add(lblTitle=new JLabel("Tricia's Grocery Store"));
        lblTitle.setFont(new Font("Broadway", Font.PLAIN, 35));
        lblTitle.setBounds(50,0,600,80);

        add(radApple=new JRadioButton("Apples ---------- P15.75"));
        add(radOrange=new JRadioButton("Oranges -------- P12.50"));
        add(radPomelo=new JRadioButton("Pomelos -------- P95.25"));

        radApple.setBounds(70,100,150,50);
        radOrange.setBounds(70,130,155,50);
        radPomelo.setBounds(70,160,155,50);

        bg = new ButtonGroup();
        bg.add(radApple);
        bg.add(radOrange);
        bg.add(radPomelo);

        add(lblQty=new JLabel("Quantity:"));
        lblQty.setBounds(70,200,150,50);

        add(txtQty=new JTextField(2));
        txtQty.setBounds(150,210,70,30);

        add(lblTotal=new JLabel("P0.00"));
        lblTotal.setBounds(150,500,500,500);

        add(btnAdd=new Button("Add Item"));
        btnAdd.setBounds(70,270,80,20);

        add(btnPrint=new Button("Print Receipt"));
        btnPrint.setBounds(150,270,80,20);

        add(btnCancel=new Button("Reset Values"));
        btnCancel.setBounds(70,290,80,20);

        add(btnExit=new Button("Close"));
        btnExit.setBounds(150,290,80,20);

        add(txtScreen=new JTextArea());
        txtScreen.setFont(new Font("Courier",Font.PLAIN,15));
        txtScreen.setBounds(250,100,270,320);
        //txtScreen.setEditable(false);


        btnAdd.addActionListener(this); 
        radApple.addActionListener(this);
        radOrange.addActionListener(this);
        radPomelo.addActionListener(this);
        txtQty.addActionListener(this);
    }

    public static void main(String[]args){
        Main main = new Main();
    }



    public void actionPerformed(ActionEvent e) {
        double appleprice=0, orangeprice=0, pomeloprice=0;
        int qty = Integer.parseInt(txtQty.getText());
        String strApples= "", strOranges = "", strPomelos = "";

        if (radApple.isSelected()){
            appleprice += 15.75 * qty;
            strApples = ("Apples ------  x" + txtQty.getText()+ " ----- P" + appleprice);
            txtScreen.setText(strApples + strOranges + strPomelos);
            txtQty.setText(null);
        }
        if (radOrange.isSelected()){
            orangeprice += 12.50 * qty;
            strOranges = ("Oranges ------  x" + txtQty.getText()+ " ----- P" + orangeprice);
            txtScreen.setText(strApples + strOranges + strPomelos);
            txtQty.setText(null);

        }
        if (radPomelo.isSelected()){
            pomeloprice += 95.25 * qty;
            strPomelos = ("Pomelos ------  x" + txtQty.getText()+ " ----- P" + pomeloprice);
            txtScreen.setText(strApples + strOranges + strPomelos);
            txtQty.setText(null);
        }

    }
}

Recommended Answers

All 12 Replies

that's because you use setText instead of append

commented: Compensating for inappropriate downvote +0

Instead of setText, use append to append your new text to the text that's already in the text area. (You may want a new line character "\n" as well)

commented: Doesn't deserve to be downvoted, the given answer is correct. +13
commented: Doesn't deserve downvote +14

Instead of

 txtScreen.setText(strApples + strOranges + strPomelos); //line 103

Try this:-

 txtScreen.setText(txtScreen.getText + "\n" +strOranges);

OR

txtScreen.append("\n");
txtScreen.append(strOranges);

Similarly at other places

commented: Duplicates info in previous posts. Spoon-feeds answer rather than helping learning. -3
commented: Not really spoonfeeding IMO plus the answer was posted at around the same time as other answers +14
commented: Agreed with Sanjay on this one, no evidence that IIM would have seen the earlier posts and his answer has helped the OP without obvious spoonfeeding IMO +11

THANK YOU SO MUCH @IIM

PROBLEM SOLVED. THANKS GUYS:))

@iciaguevara:- mark thread as solved if your problem is solved.

@james:- Please explain why downvoted.I just explained this how to do.Moreover,i was typing code and then posted.I like to code and then give solution rather than just giving solution without proof.I haven;t down voted your thread that you thought it was me who down voted.It might be the author of this post who down voted,not me.

OK. No I didn't assume you downvoted - all votes without a comment are anonymous by design. There's no connection between the votes on my/stultuske's posts, and the downvote on yours.
In this forum we try to avoid giving people code they can copy/paste without understanding. Your post didn't explain how to do it. It just says "copy this code and your homework will be OK" - no attempt to explain why. If it hadn't already got an upvote I may not have voted, but under the circumstances I didn't think I could let an upvote stand unchallenged.
The earlier posts were examples of giving the OP enough info to do a little research and solve the problem for themselves. Follow that example and you will see more upvotes.

It's not about some damm points.I am not here to earn points or something.But it is about helping others to understand.

that's because you use setText instead of append



Instead of setText, use append to append your new text to the text that's already in the text area. (You may want a new line character "\n" as well)

This is just a spoof of above line.After that my statement just tell how to do a line change.I am not copying entire code and making them just copy-paste my code.It is just a line in which i am pointing out error.First understand the difference between showing code and showing error-line code.

Stultiuske and I posted simultaneously, that's quite a common event. Maybe your post was also simultaneous, but that's irrelevant to the teach vs spoonfeed point. You don't have to agree with me; there are many other active members in this forum who will vote as they see fit. I'm now going to have dinner....

It is aready explained in Stultiuske post.so i just helped what exactly is the root cause.
Before judging others post check the one,this posted by you.That is an example of spoonfeeding.You are just printing complete code.I just gave a line reference.

If it hadn't already got an upvote I may not have voted, but under the circumstances I didn't think I could let an upvote stand unchallenged.

@James:- the above two link i gave were from the one that were upvoted and you were doing spoon feeding.Should i do the same thing?
There is a line between showing entire coding nad showing error line and how to change it.Understand the difference than judge.

Folks, let's not turn this thread into a warzone. I'll close the thread by saying this:

IMO no one in this thread is copying other solutions or posting ready-to-use entire solutions; neither James nor IIM. I can't control personal opinions (manifested via means of up/down-votes) but let's just admit that all of us, at some point in time have either posted short one-line answers or full code snippets to help out the OP. Let's not forget the real reason of why we actually spend our free time on this forum; to help out people without causing problems for others. I don't think anyone in this thread is guilty of anything. To offset random downvoting, I have upvoted the posts accordingly.

If someone wants to take this further, I would be more than happy to discuss things via PM.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.