Home > Net >  Show data in a text field from an advanced for loop
Show data in a text field from an advanced for loop

Time:11-11

A shown in the code below I want the data to be inputted into the textfield when the button is pressed at the moment I have it printing to console how do I add to the textfield ?? is it text.add ??

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import javax.swing.*;


public class displayGui extends JFrame implements WindowListener, ActionListener {

    JButton showData;

    JButton hideData;

    private static final int FRAME_WIDTH = 100;
    private static final int FRAME_HEIGHT = 100;
    JTextField text = new JTextField(20);
    ArrayList<Phone> phones = new ArrayList<>();
    public displayGui() {

        phones.add(new Phone("Sony", "Experia X", 32, 12.5 , 4.6 , "Yes" , 150));
        phones.add(new Phone("Sony", "Experia Y", 64, 14.2 , 5.6 , "Yes" , 175));
        phones.add(new Phone("Samsung", "Galaxy M", 64, 14.5 , 5.4 , "Yes" , 180));
        phones.add(new Phone("Nokia", "3330", 16, 13.2 , 2.3 , "No" , 90));
        phones.add(new Phone("Motorola", "M1", 8, 11.3 , 4.9 , "Yes" , 100));
        phones.add(new Phone("Iphone", "6", 32, 13.5 , 6.4 , "Yes" , 250));
        phones.add(new Phone("Alcatel", "A3", 8, 9.3 , 2.4 , "No" , 50));
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();

        addWindowListener(this);
        showData = new JButton("Show Data");
        add(showData);
        panel.add(text);
        showData.addActionListener(this);


        panel.add(showData);
        hideData = new JButton("Hide Data");
        panel.add(hideData);
        hideData.addActionListener(this);
        frame.add(panel);

        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    @Override

    public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(showData)) {
            if (showData.isSelected()){

                for (Phone phone : phones){
                    System.out.println("Phone Make: "   phone.getMake());
                    System.out.println("Phone Model: "   phone.getModel());
                    System.out.println("Phone Memory: "   phone.getMemory());
                    System.out.println("Phone Camera: "   phone.getCamera());
                    System.out.println("Phone screen-size: "   phone.getScreensize());
                    System.out.println("Is the phone a smart phone: "   phone.getSmart());
                    System.out.println("The phone costs: "   phone.getPrice());
                };

            }
        }



        for (Phone phone : phones){
            System.out.println("Phone Make: "   phone.getMake());
            System.out.println("Phone Model: "   phone.getModel());
            System.out.println("Phone Memory: "   phone.getMemory());
            System.out.println("Phone Camera: "   phone.getCamera());
            System.out.println("Phone screen-size: "   phone.getScreensize());
            System.out.println("Is the phone a smart phone: "   phone.getSmart());
            System.out.println("The phone costs: "   phone.getPrice());
        }

        text.setText("Testing");

    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {
        dispose();
        System.exit(0);
    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}
import java.io.*;
import java.util.ArrayList;

public class Tester {
    public static void main(String[] args) throws IOException, ClassNotFoundException {

        ArrayList<Phone> phones = new ArrayList<>();
        phones.add(new Phone("Sony", "Experia X", 32, 12.5 , 4.6 , "Yes" , 150));
        phones.add(new Phone("Sony", "Experia Y", 64, 14.2 , 5.6 , "Yes" , 175));
        phones.add(new Phone("Samsung", "Galaxy M", 64, 14.5 , 5.4 , "Yes" , 180));
        phones.add(new Phone("Nokia", "3330", 16, 13.2 , 2.3 , "No" , 90));
        phones.add(new Phone("Motorola", "M1", 8, 11.3 , 4.9 , "Yes" , 100));
        phones.add(new Phone("Iphone", "6", 32, 13.5 , 6.4 , "Yes" , 250));
        phones.add(new Phone("Alcatel", "A3", 8, 9.3 , 2.4 , "No" , 50));

        try {
            System.out.println("Check to see if file exists");
            FileInputStream fileIn = new FileInputStream("phone.ser");
            System.out.println("It exists");
            ObjectInputStream objectIn = new ObjectInputStream(fileIn);
            System.out.println("Reading now");


            phones = (ArrayList<Phone>) objectIn.readObject();
            for (Phone phone : phones){
                System.out.println("Phone Make: "   phone.getMake());
                System.out.println("Phone Model: "   phone.getModel());
                System.out.println("Phone Memory: "   phone.getMemory());
                System.out.println("Phone Camera: "   phone.getCamera());
                System.out.println("Phone screen-size: "   phone.getScreensize());
                System.out.println("Is the phone a smart phone: "   phone.getSmart());
                System.out.println("The phone costs: "   phone.getPrice());
            }

            objectIn.close();
            } catch (FileNotFoundException e1) {
            System.out.println("file does not exist");

            try {
                System.out.println("Creating it now ....");
                FileOutputStream fileOut = new FileOutputStream("phone.ser");

                ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);

                objectOut.writeObject(phones);

                objectOut.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            ///deserialization///
            try {
                FileInputStream fileIn = new FileInputStream("phone.ser");

                ObjectInputStream objectIn = new ObjectInputStream(fileIn);
                Phone newPhone = (Phone) objectIn.readObject();

                objectIn.close();
                System.out.println("Phone Make: "   newPhone.getMake());
                System.out.println("Phone Model: "   newPhone.getModel());
                System.out.println("Phone Memory: "   newPhone.getMemory());
                System.out.println("Phone Camera: "   newPhone.getCamera());
                System.out.println("Phone screen-size: "   newPhone.getScreensize());
                System.out.println("Is the phone a smart phone: "   newPhone.getSmart());
                System.out.println("The phone costs: "   newPhone.getPrice());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}

import java.io.*;
import java.util.ArrayList;

public class MainSer {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        new displayGui();

        ArrayList<Phone> phones = new ArrayList<>();
        phones.add(new Phone("Sony", "Experia X", 32, 12.5 , 4.6 , "Yes" , 150));
        phones.add(new Phone("Sony", "Experia Y", 64, 14.2 , 5.6 , "Yes" , 175));
        phones.add(new Phone("Samsung", "Galaxy M", 64, 14.5 , 5.4 , "Yes" , 180));
        phones.add(new Phone("Nokia", "3330", 16, 13.2 , 2.3 , "No" , 90));
        phones.add(new Phone("Motorola", "M1", 8, 11.3 , 4.9 , "Yes" , 100));
        phones.add(new Phone("Iphone", "6", 32, 13.5 , 6.4 , "Yes" , 250));
        phones.add(new Phone("Alcatel", "A3", 8, 9.3 , 2.4 , "No" , 50));



        //serialization//
        try {
            FileOutputStream fileOut = new FileOutputStream("phone.ser");
            ObjectOutputStream Out = new ObjectOutputStream(fileOut);
            Out.writeObject(phones);
            Out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        ////deserialization///
        try {
            FileInputStream fileIn = new FileInputStream("phone.ser");

            ObjectInputStream objectIn = new ObjectInputStream(fileIn);

            phones = (ArrayList<Phone>) objectIn.readObject();
            for (Phone phone : phones){
                System.out.println("Phone Make: "   phone.getMake());
                System.out.println("Phone Model: "   phone.getModel());
                System.out.println("Phone Memory: "   phone.getMemory());
                System.out.println("Phone Camera: "   phone.getCamera());
                System.out.println("Phone screen-size: "   phone.getScreensize());
                System.out.println("Is the phone a smart phone: "   phone.getSmart());
                System.out.println("The phone costs: "   phone.getPrice());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        }
    }


import java.io.Serializable;

public class Phone implements Serializable {
    private String make;
    private String model;
    private double memory;
    private double camera;
    private double screensize;
    private String smart;
    private int price;

    public Phone(String make , String model , double memory , double camera, double screensize, String smart , int price){
        this.make = make;
        this.model = model;
        this.memory = memory;
        this.camera = camera;
        this.screensize = screensize;
        this.smart = smart;
        this.price = price;

    }

    public String getMake() {
        return make;
    }

    public String getModel() {
        return model;
    }

    public double getMemory() {
        return memory;
    }

    public double getCamera() {
        return camera;
    }

    public double getScreensize() {
        return screensize;
    }

    public String getSmart() {
        return smart;
    }

    public int getPrice() {
        return price;
    }
}

When I run it I currently have it running onto the console. But needs to be entered into the text field. Thanks

CodePudding user response:

It appears to me that you looked at an old Swing tutorial. A Swing application does not need to extend class data hidden

And with the data displayed:

data displayed

  • Related