Results 1 to 2 of 2
  1. #1
    Jackie88 is offline Member
    Join Date
    Jul 2014
    Posts
    1
    Rep Power
    0

    Default Autofill a JTextField once another JTextField has been typed into

    Hi there

    I am trying to get a JTextField to autofill with the result of a calculation made from user input in another JTextField

    I am building a virtual ticket machine, where i want the user to select ticket type, type in the quantity of tickets they would like, and then so as soon as they have clicked out of the quantity textfield, I want the payment required textfield to autofil with the correct cost of the users selection, I have wrote the code to do the calculation for the cost, I just dont know how to put the cost in the payment required textfield at this stage.

    Here is my code for this bit;
    Java Code:
    public void actionPerformed(ActionEvent e) {
            
            String TypeName = (String)TicketTypeList.getSelectedItem();
            typeselectedTxt.setText(TypeName);
            int arrayIndex = Arrays.asList(typeStrings).indexOf(TypeName);
            
            String quantityStr = noOfTickets.getText();
            Double quantity = Double.parseDouble(quantityStr);
                
            if (noOfTickets.getText().equals(null) || noOfTickets.getText().equals("")){
                showMessageDialog(null, "Please enter a quantity");
                
            } else {
            
                switch (arrayIndex){
                case 0: 
                    amountRequired = 15.00*quantity;
                break;
                case 1:
                    amountRequired = 10.20*quantity;
                break;
                case 2:
                    amountRequired = 5.70*quantity;
                 break;
                case 3:
                    amountRequired = 30.85*quantity;
                break;
            }  
        
            moneyRequiredTxt.setText(Double.toString(amountRequired));
    
    
    .....

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default Re: Autofill a JTextField once another JTextField has been typed into

    Sounds like a job for a FocusListener. You could also do it with a DocumentListener or an ActionListener.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. JTextField ???
    By Nhatmeister in forum New To Java
    Replies: 6
    Last Post: 11-23-2013, 07:52 PM
  2. Replies: 3
    Last Post: 11-14-2011, 05:13 AM
  3. Replies: 0
    Last Post: 08-30-2011, 09:18 AM
  4. Replies: 1
    Last Post: 01-30-2009, 06:44 PM
  5. JtextField
    By kashifu in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2008, 04:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •