Bob
Bob

Reputation: 15

How to add data from another class to JTextField

I have a Gadget, Mobile, MP3 and GadgetShop class. The Gadget is the super class, the Mobile and MP3 are Subclasses of the Superclass and the GadgetShop is the GUI I made.

I just need my GadgetShop to gather the information to place it in the TextFields I've made when I click the button I've made. I have no idea how to pass the information.

I'm using BlueJ by the way.

This is my Mobile Class:

/**Mobile phone class that:
 * allows calling credit to be applied that is more than 0
 * shows remaining calling credit
 * allows a phone number to be added
 * shows duration of phone call 
 */
public class Mobile extends Gadget
{
    private int credit;
    private int duration;
    private String number = "";

    /**
    * Constructor for objects of class Mobile.
    */
    public Mobile(double ThePrice, int TheWeight, String TheModel, String TheSize) 
    {
        // initialise instance variables
        super(ThePrice, TheWeight, TheModel, TheSize);
    }


    /**
    * Insert the duration of the call.
    */
    public void insertDuration(int length)
    {
        System.out.println("A duration of " + length + " minutes for the phone call has been inserted!");
        duration = length;
    }

    /**
    * Insert the phone number.
    */
    public void insertNumber(String numberAdder)
    {
        System.out.println("The phone number " + numberAdder + " has been inserted!");
        number = numberAdder;
    }

    /**
    * Insert credit for the call.
    * A positive amount will need to be added in order for successfull credit to be applied,
    * otherwise an error message is displayed.
    */   
    public void insertCredit(int calls)
    {
        if(calls > 0) {
            System.out.println("Successfully added " + calls + " pounds of credit!");
            credit = credit + calls;
        }
        else {
            System.out.println("You need to insert a positive amount more than " + credit + " pounds.");
    }
    }

    /**
    * Make the phone call.
    * If the credit is equal to or more than the duration, a message displays the phone number and duration of the call.
    * If the credit is 0, a message will be displayed to insert more than 0.
    * The number of minutes the call lasted is reduced by the amount that was available.
    */
    public void makePhoneCall()
    {
        if(credit == 0)
        System.out.println("Insert more than 0 credit to make a phone call!");
    else {
        if(credit >= duration) {
        System.out.println("The phone number " + number + " has been dialed and is being called for " + duration + " minutes");
        credit = credit - duration;
        duration = 0;
    }
    else {
        if(credit < duration)
        System.out.println("You do not have enough credit to make a phone call! Your credit is " + credit + " pounds");
    }
    }
    }

    public void mobilePrint()
    /**
    * Print the details of the mobile.
    **/
    {
       System.out.println("The price of the mobile is " + price + " pounds"); 
       System.out.println("The weight is " + weight + " grams");
       System.out.println("The model is " + model); 
       System.out.println("The size is " + size);
    }
}

This is my GadgetShop:

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;

public class GadgetShop implements ActionListener
{
    private JTextField model, price, weight, size, credit, memory, phoneNo, duration, download, displayNumber;
    private JButton addMobile, addMP3, clear, displayAll;

    //These JTextField's are for the labels
    private JTextField model2, price2, weight2, size2, credit2, memory2, phoneNo2, duration2, download2, displayNumber2;

    private JFrame frame;
    private ArrayList<Gadget> gadgetDetails;

    public GadgetShop()
    {
        makeFrame();
    }

    public void addGadget(Gadget newGadget)
    {
        ArrayList<Gadget> GadgetList = new ArrayList<Gadget>();
        Gadget Object = new Gadget(1.00,20,"model","big");
        GadgetList.add(Object);
        model.setText("hi");
    }

    public void makeFrame()
    {
        frame = new JFrame("Gadget Shop");
        Container contentPane = frame.getContentPane();

        model = new JTextField(10);
        price = new JTextField(10);
        weight = new JTextField(10);
        size = new JTextField(10);
        credit = new JTextField(10);
        memory = new JTextField(10);
        phoneNo = new JTextField(10);
        duration = new JTextField(10);
        download = new JTextField(10);
        displayNumber = new JTextField(10);

        //Display Model text box and model info    
        model2 = new JTextField("Model:");        
        contentPane.add(model2);
        model2.setOpaque(false);         
        contentPane.add(model);  

        price2 = new JTextField("Price:");
        contentPane.add(price2);
        price2.setOpaque(false);
        contentPane.add(price);

        weight2 = new JTextField("Weight:");
        contentPane.add(weight2);
        weight2.setOpaque(false);
        contentPane.add(weight);

        size2 = new JTextField("Size:");
        contentPane.add(size2);
        size2.setOpaque(false);
        contentPane.add(size);

        credit2 = new JTextField("Credit:");
        contentPane.add(credit2);
        credit2.setOpaque(false);
        contentPane.add(credit);

        memory2 = new JTextField("Memory:");
        contentPane.add(memory2);
        memory2.setOpaque(false);
        contentPane.add(memory);

        phoneNo2 = new JTextField("Phone Number:");
        contentPane.add(phoneNo2);
        phoneNo2.setOpaque(false);
        contentPane.add(phoneNo);

        duration2 = new JTextField("Duration:");
        contentPane.add(duration2);
        duration2.setOpaque(false);
        contentPane.add(duration);       

        download2 = new JTextField("Download:");
        contentPane.add(download2);
        download2.setOpaque(false);
        contentPane.add(download);

        displayNumber2 = new JTextField("Display Number:");
        contentPane.add(displayNumber2);
        displayNumber2.setOpaque(false);
        contentPane.add(displayNumber);

        addMobile = new JButton("Add Mobile Number");
        contentPane.add(addMobile);
        addMobile.addActionListener(this);

        addMP3 = new JButton("Add MP3 Song");
        contentPane.add(addMP3);
        addMP3.addActionListener(this);

        clear = new JButton("Clear");
        contentPane.add(clear);
        clear.addActionListener(this);

        displayAll = new JButton("Display All");
        contentPane.add(displayAll);
        displayAll.addActionListener(this);

        contentPane.setLayout (new GridLayout(4,4));

        frame.pack();
        frame.setVisible(true);
    }

        public void actionPerformed(ActionEvent event)
    {
        String command = event.getActionCommand();
        if (command.equals("Add Mobile Number")) {
            addMobile();
        }  
    }

    public void addMobile()
    {
        model.setText("");
    }


    public void clearText()
    {
        model.setText("");
        price.setText("");
        weight.setText("");
        size.setText("");
        credit.setText("");
        memory.setText("");
        phoneNo.setText("");
        duration.setText("");
        download.setText("");
        displayNumber.setText("");
    }
}

Where I have the:

public void addMobile()
{
    model.setText("");
}

I want to add the details of the Mobile Phone

Upvotes: 0

Views: 245

Answers (1)

Justas S
Justas S

Reputation: 594

Similarly how you do it in the addGadget method you could add a Mobile parameter to addMobile

public void addMobile(Mobile mobile)
{
    // You should add getter methods for attribute 
    //in the Mobile class to get meaningful values here instead of toString
    model.setText(mobile.toString()); 
}

Upvotes: 1

Related Questions