0

So I'm tryin to enter some Datas in a form and stock them to display them in a array when i click of the button "Show" but i don't know how can i do that.

Here's my form :

import java.awt.*;
import java.awt.event.*;

public class test {
    public static void main(String[] args) {
        Frame frm=new Frame("Add employee");
        Label lbl = new Label();
        frm.add(lbl);
        frm.setSize(350,200);
        frm.setVisible(true);
        frm.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
        Panel p = new Panel();
        Panel p1 = new Panel();
        Label jFirstName = new Label("first name");
        TextField lFirstName = new TextField(20);
        Label jLastName =new Label("last name");
        TextField lLastName=new TextField(20);
        Label jAge= new Label("Age");
        TextField lAge = new TextField(20);
        Label jDate =new Label("Date");
        TextField lDate=new TextField(20);
        Label jType = new Label("employee type");
        TextField lType = new TextField(20);
        p.setLayout(new GridLayout(7,1));



        p.add(jFirstName);
        p.add(lFirstName);
        p.add(jLastName);
        p.add(lLastName);
        p.add(jAge);
        p.add(lAge);
        p.add(jDate);
        p.add(lDate);
        p.add(jType);
        p.add(lType);

        Button Submit=new Button("Submit");
        p.add(Submit);
        p1.add(p);
        frm.add(p1,BorderLayout.NORTH);
    }
}

Do you have any idea ?

agreüs
  • 109
  • 11
  • *"Do you have any idea ?"* of what you're asking, no; how it might be improved? Yes, you might like to do some research into the difference between Swing and AWT – MadProgrammer Nov 14 '17 at 00:48
  • So, after several more read throughs, you might want to have a read through [How to use buttons](https://docs.oracle.com/javase/tutorial/uiswing/components/button.html) and [How to write ActionListeners](https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html) – MadProgrammer Nov 14 '17 at 00:52
  • It can be usefull to begin thx – agreüs Nov 14 '17 at 00:56
  • Just to add to the wise advice of @MadProgrammer. 1st a 'copy/paste comment' I regularly offer.. **See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556)** Now. That accepted answer (by me) covers some of the pitfalls of using more than one frame & outlines many alternatives. In this specific case though (where one of the frames is intended to collect information from the user), it's often much better handled using a modal `JDialog` or a `JOptionPane`. Save yourself a lot of niggling problems by starting using the best tool for the job. – Andrew Thompson Nov 14 '17 at 01:15

1 Answers1

0

Try below code. I started with your code and modified it. Few things I'd suggest/point:

Consider using Swing instead of AWT (e.g. JFrame instead of Frame). Swing is richer. Try to break code into separate classes than writing everything in main() method. I removed panel p1 because it was redundant.

public class Test
{
  public static void main(String[] args)
  {
    final Frame frm=new Frame("Add employee");
    Label lbl = new Label();
    frm.add(lbl);
    frm.setSize(350,200);
    frm.setVisible(true);
    frm.addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent e){
        System.exit(0);
      }
    });
    Panel p = new Panel();
    //Panel p1 = new Panel();
    final Label jFirstName = new Label("first name");
    final TextField lFirstName = new TextField(20);
    final Label jLastName =new Label("last name");
    final TextField lLastName=new TextField(20);
    final Label jAge= new Label("Age");
    final TextField lAge = new TextField(20);
    final Label jDate =new Label("Date");
    final TextField lDate=new TextField(20);
    final Label jType = new Label("employee type");
    final TextField lType = new TextField(20);
    p.setLayout(new GridLayout(7,1));

    p.add(jFirstName);
    p.add(lFirstName);
    p.add(jLastName);
    p.add(lLastName);
    p.add(jAge);
    p.add(lAge);
    p.add(jDate);
    p.add(lDate);
    p.add(jType);
    p.add(lType);

    Button Submit=new Button("Submit");

    Submit.addActionListener(new ActionListener()
    {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        String[] formData = new String[5];
        formData[0] = jFirstName.getText() + ": " + lFirstName.getText();
        formData[1] = jFirstName.getText() + ": " + lLastName.getText();
        formData[2] = jAge.getText() + ": " + lAge.getText();
        formData[3] = jDate.getText() + ": " + lDate.getText();
        formData[4] = jType.getText() + ": " + lType.getText();

        TextArea textArea = new TextArea();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < formData.length; i++)
        {
          sb.append(formData[i]);
          sb.append("\n");
        }
        textArea.setText(sb.toString());

        final Dialog dialog = new Dialog(frm, "Form Data");
        dialog.setModal(true);
        dialog.addWindowListener(new WindowAdapter()
        {
          @Override
          public void windowClosing(WindowEvent e)
          {
            dialog.setVisible(false);
          }
        });
        dialog.add(textArea);
        dialog.pack();
        dialog.setVisible(true);
      }
    });

    p.add(Submit);
    //p1.add(p);
    frm.add(/*p1*/p,BorderLayout.NORTH);
  }
}
Prasad Karunagoda
  • 2,048
  • 2
  • 12
  • 16
  • Thank you for this code and your advices it's really helpful ! I'm a Java beginner that helped me a lot – agreüs Nov 14 '17 at 11:51