-2

Tried to create a variable called CashInsert as double and then I did CashAmounttxt = CashInsert, but failed, also how do I add action listener so when button is clicked it will store the amount inserted in the box inside variable?

Here is my code:

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class CashPay extends javax.swing.JFrame {
    private JLabel cashamountlbl;
    private JButton Calculatebtn;
    private JLabel CashChangelbl;
    private JTextField CashAmounttxt;
    private double ChangeLeft;
    private double CashInsert;


    /**
    * Auto-generated main method to display this JFrame
    */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                CashPay inst = new CashPay();
                inst.setLocationRelativeTo(null);
                inst.setVisible(true);
            }
        });
    }

    public CashPay() {
        super();
         CashAmounttxt = CashInsert;
        initGUI();
    }

    private void initGUI() {
        try {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            getContentPane().setLayout(null);

            cashamountlbl = new JLabel();
            getContentPane().add(cashamountlbl);
            cashamountlbl.setText("Enter Cash Amount");
            cashamountlbl.setBounds(41, 65, 105, 34);
            cashamountlbl.setBackground(new java.awt.Color(128,0,255));

            CashAmounttxt = new JTextField();
            getContentPane().add(CashAmounttxt);
            CashAmounttxt.setBounds(215, 71, 87, 23);

            CashChangelbl = new JLabel();
            getContentPane().add(CashChangelbl);
            CashChangelbl.setBounds(190, 117, 158, 109);
            CashChangelbl.setBackground(new java.awt.Color(255,128,64));

            Calculatebtn = new JButton();
            getContentPane().add(Calculatebtn);
            Calculatebtn.setText("Calculate Total");
            Calculatebtn.setBounds(46, 203, 93, 23);

            pack();
            setSize(400, 300);
        } catch (Exception e) {
            //add your error handling code here
            e.printStackTrace();
        }
    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
PedramCarter
  • 101
  • 4
  • 1
    1) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). .. – Andrew Thompson Mar 22 '15 at 14:18
  • 1
    .. 3) Consider using a `JSpinner` with a [`SpinnerNumberModel`](https://docs.oracle.com/javase/8/docs/api/javax/swing/SpinnerNumberModel.html) instead. 4) Always copy/paste error and exception output! – Andrew Thompson Mar 22 '15 at 14:19
  • I can't this is an assignment I have 2 weeks to complete and I have documentations to go along with it so realistically has to be done in 1 week. – PedramCarter Mar 22 '15 at 14:36
  • `"I can't..."` -- You can't what? – Hovercraft Full Of Eels Mar 22 '15 at 14:37
  • Sorry I mean I have no time to like start basics (I know it sounds ultra stupid) got to dive right in there and put pressure on myself to learn this stuff, even if I don't learn this as long as I can do majority of it and some harder stuff with some help and guidance's would be much appreciated, If I sound rude I apologies but this means a lot to me I want to learn and do this. – PedramCarter Mar 22 '15 at 14:43
  • 1
    `"I have no time to like start basics (I know it sounds ultra stupid)..."` -- yes, you're right as this is the completely wrong approach. If you learn to do things the right way, the project will go much easier for you and the results will go much better. But thanks for posting this since we now know that we can avoid wasting our time trying to give you advise if you really don't want it. – Hovercraft Full Of Eels Mar 22 '15 at 14:51
  • Sorry, my post was a bit harsh, but still, I do strongly urge you to re-arrange your priorities. For one, if don't put in effort to learn the basics, you won't even be able to understand the advice that we give, and with programming as with math, all future knowledge is built on the foundation of current knowledge. You have to strive to learn to do things right if you desire to succeed in any fashion. – Hovercraft Full Of Eels Mar 22 '15 at 15:21
  • It's okay it's just I don't learn from reading or listening I usually watch youtube tutorials and the dudes talk and do at the same time and I understand what does what, just trying to learn here not trying to get people to do my work. – PedramCarter Mar 22 '15 at 15:29
  • `"I don't learn from reading..."` -- as with any learned skill, this **will** improve with practice. Please consider practicing this as much as possible, and writing a lot of code. Both will pay big dividends. – Hovercraft Full Of Eels Mar 22 '15 at 15:33

1 Answers1

2

Use Double.toString() to convert the double value to a string and JTextField.setText() to show it in the UI. For the opposite conversion, use JTextField.getText() and Double.parseDouble().

yole
  • 92,896
  • 20
  • 260
  • 197
  • I'm sorry I'm really a noob do you mind showing me how using my variable names? sorry I don't understand. – PedramCarter Mar 22 '15 at 14:10
  • 3
    What is the specific thing that you do not understand? – yole Mar 22 '15 at 14:12
  • where it's inserted, so for example if I'm on the public CasPay and my jtextfield variable is called "CashAmounttxt" and my double is called "InsertAmount" how do I do it in what order so "CashAmounttxt = Insertamount"? – PedramCarter Mar 22 '15 at 14:18
  • This depends on what exactly you want your program to do. You never explained that. – yole Mar 22 '15 at 14:21
  • Basically I have 4 classes, one is the items page using check boxes to select items (I have gotten that to work) when I click my button it adds the item prices, other class is cashpay other one is cardpay, and the other one is mainmenu. I'm trying to using a textfield to insert amount of cash which is stored in a variable, then I use total variable, and use the amount inserted minus the total which would then give me the change left. – PedramCarter Mar 22 '15 at 14:35
  • Then it looks like CashInsert should be a parameter to your constructor. Then you need to call the setText() method on your `CashAmounttxt` field and pass the value returned from Double.toString() as a parameter. – yole Mar 22 '15 at 14:39
  • What I don't understand is how do I work with my jtextfield?, I did what you said but I think I did it wrong here is what I did public CashPay(double CashInsert) { super(); CashAmounttxt =CashInsert.Double.toString(); initGUI(); } – PedramCarter Mar 22 '15 at 14:56
  • 1
    You really need to read a basic Java tutorial, in particular about the syntax of calling methods on objects. Once you understand the syntax, you will be able to literally translate my previous comment into Java. – yole Mar 22 '15 at 14:57
  • One+ up vote for giving an answer without spoon-feeding and for trying to get the OP to understand the basics needed to get to his solution. – Hovercraft Full Of Eels Mar 22 '15 at 15:05
  • So I did this, am I going at right direction? double CashInsert = Double.parseDouble(CashAmounttxt.getText()); – PedramCarter Mar 22 '15 at 15:28
  • This is the correct way to get the double value entered in the text field. – yole Mar 22 '15 at 15:28
  • Right just to confirm, the value entered in the jtextfield = a string theoretically and what I just did was convert what's inserted to a double and insert it into a variable? – PedramCarter Mar 22 '15 at 15:33
  • Thanks a lot I really am not stupid it's just hard for me to express myself and understand, I'm like a musician who uses a instrument to play what he feels or what's on his mind but he can't read notes. – PedramCarter Mar 22 '15 at 15:40