-1

I am trying to make a program that I enter first name and last name and so on and then being about to view that information again. I am using Java and Jframes
i have setup my interface with JTextfileds and Jlabels and a button
i am trying to make an array of objects so each array number will contain (firstname, lastname , and so on) and then i am going to print it out into a Jlist. i want to make it so when i prees my button it will input everything in my Jtextinputs in the first array but i don't know how to do the array with objects i have started one but i need help thanks

i know that i need to use

   ArrayList<> mylist = new ArrayList<Data>();  
   Data myTask = new Data("imput", "input");
   myList.add(myTask);

But I don't understand how I use it

Thanks

that is my data.java file

package components;

Import Java.Io.Serializable;

public class Data implements Serializable {
    static final long serialVersionUID = 1;
    String name;
    String description;

    public Task(String Fname, String Lname) {
        this.name = Fname;
        this.description = Lname;
    }

    public String getName() {
        return this.name;
    }

    public String getDescription() {
        Return this.description;
    }

interface file

public class DataDemo extends JPanel implements ActionListener {
    private static final long serialVersionUID = 1L;
    //Name Label and TextField
    protected static JLabel LbName;
    protected static JTextField txtInputName;
    //Description Label and TextField
    protected static JLabel LbDescription;
    protected static JTextField txtInputDescription;
    //Submit Button
    static JButton btnSubmit;

    @SuppressWarnings("unchecked")
    public DataDemo() {

        //Name Label and TextField 
        LbName = new JLabel("Name:");
        txtInputName = new JTextField(200);

        //Description Label and TextField
        LbDescription = new JLabel("Description:");
        txtInputDescription = new JTextField(20);

        //Submit Button
        btnSubmit = new JButton("Done");
        btnSubmit.addActionListener(this);

        //Add Components to this container, using the default FlowLayout.
        setLayout(null);
        //Name
        add(LbName);
        add(txtInputName);
        //Description
        add(LbDescription);
        add(txtInputDescription);
        //button Submit
        add(btnSubmit);
    }


    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == btnSubmit)

    }


    private static void createAndShowGUI() {

        //Create and set up the window.
        JFrame frame = new JFrame("DataDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        DataDemo newContentPane = new DataDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setSize(500, 500);
        frame.setVisible(true);
        //(x,y,with,height)
        //Name interface position 
        LbName.setBounds(50, 70, 200, 25);
        txtInputName.setBounds(200, 70, 200, 25);
        //Description interface position 
        LbDescription.setBounds(50, 100, 200, 25);
        txtInputDescription.setBounds(200, 100, 200, 25);
        //button Submit
        btnSubmit.setBounds(200, 150, 200, 25);
    }


    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28
user3504020
  • 49
  • 1
  • 4
  • `txtInputName.setBounds(200,70,200,25);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson May 05 '14 at 11:42
  • my problem is that i need get data from the JTextField and then put it into the array – user3504020 May 05 '14 at 11:50

1 Answers1

1

Did you try using

ArrayList<Data> mylist = new ArrayList<Data>(); 

That should allow you to create a proper Iterator and get your Data-Objects. Otherwise you will have to cast all Objects you retrieve from your list to Data, i.e.

ArrayList<> mylist = new ArrayList<>();
//add your Data-Objects here

for (Object o : mylist)
{
    Data d = (Data) o;
    //do something with d
}
Christian
  • 1,589
  • 1
  • 18
  • 36