views:

731

answers:

2

Guys, kindly help me how to pass the values of my inputs in my JTextField(ID,LastName,FirstName,Course,Year) into my ArrayList without replacing the existing elements. At the same i'll be using my ArrayList stored values to append in my JTextArea(summary)

////// PALOS TEXTFIELD

List<Form> myList = new ArrayList<Form>();


     id = new JTextField(20);


     id.addKeyListener(new KeyAdapter()
     {
      public void keyTyped(KeyEvent ke){
       char char1 = ke.getKeyChar();
       if((!(Character.isDigit(char1))) && (char1 != '\b') ){ 
         ke.consume(); 
        }
       } 
      }); 
      id.addActionListener(handler);
      fname = new JTextField(20);
      fname.setFont(new Font("TimesRoman", Font.PLAIN,14));
      fname.setHorizontalAlignment(JTextField.CENTER);
      fname.setBorder(BorderFactory.createEtchedBorder(3, Color.green, Color.white));

      fname.addKeyListener(new KeyAdapter()
      {
       public void keyTyped(KeyEvent ke){
        char char1 = ke.getKeyChar();
        if((!(Character.isLetter(char1))) && 
          (char1 != '\b') ) 
          { 
          ke.consume(); 
          } 
          } 
          public void keyReleased(KeyEvent e){} 
          public void keyPressed(KeyEvent e){} 
          }); 
      fname.addActionListener(handler);

    lname = new JTextField(20);

    lname.addKeyListener(new KeyAdapter()
     {
      public void keyTyped(KeyEvent ke){
       char char1 = ke.getKeyChar();
       if((!(Character.isLetter(char1))) && 
        (char1 != '\b') ) 
        { 
         ke.consume(); 
        } 
       } 
        public void keyReleased(KeyEvent e){} 
        public void keyPressed(KeyEvent e){} 
          }); 
    lname.addActionListener(handler);

    year = new JTextField(20);

    year.addKeyListener(new KeyAdapter()
     {
     public void keyTyped(KeyEvent ke){
      char char1 = ke.getKeyChar();
       if((!(Character.isDigit(char1))) && 
        (char1 != '\b') ) 
        { 
         ke.consume(); 
        } 
       } 
       public void keyReleased(KeyEvent e){} 
       public void keyPressed(KeyEvent e){} 
        }); 
    year.addActionListener(handler);

    course = new JTextField(20);

     course.addKeyListener(new KeyAdapter()
     {
      public void keyTyped(KeyEvent ke){
      char char1 = ke.getKeyChar();
       if((!(Character.isLetter(char1))) && 
        (char1 != '\b') ) 
        { 
         ke.consume(); 
        } 
       } 
        public void keyReleased(KeyEvent e){} 
        public void keyPressed(KeyEvent e){} 
       }); 
     course.addActionListener(handler); 

////PALOS BUTTONS

    addB = new JButton(namesB[1]);
     addB.setHorizontalAlignment(JTextField.CENTER);
     addB.addActionListener(new ActionListener(){
     public void actionPerformed(ActionEvent e){
      id.selectAll();
      fname.selectAll();
       lname.selectAll();
       course.selectAll();
       year.selectAll();       
       String textID = id.getSelectedText();
       String textFName = fname.getSelectedText();
       String textLName = lname.getSelectedText();
       String textCourse = course.getSelectedText();
       String textYear = year.getSelectedText();


            summary.setCaretPosition(summary.getDocument().getLength());

        } 
       });

/////pALOS TEXTAREA

    summary = new JTextArea(11,31);
      summary.setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 5));
      summary.setText("ID" + newtab + "FirstName " + newtab +  "LastName" + newtab + "Course" + newtab + "Year" + newline);
      summary.setEditable(false);
+1  A: 

I'll take a stab at it but I have to make some assumptions here.

// Obviously no public fields, but I cant be bothered to make constructor
// or get/set methods
public class Form
{
    public String id;
    public String lastName;
    public String firstName;
    public String course;
    public String year;
}

So you want to add a new instance of Form to your list of forms everytime that button is pressed:

public class MyGui
{
    private List<Form> forms = new ArrayList<Form>();
    private JTextField fname;
    private JTextField id;
    private JTextField lname;
    private JTextField course;
    private JTextField year;
    // build gui ....
}

This is the action listener for your 'save/add' button

public void actionPerformed(ActionEvent e)
{
    Form form = new Form();
    form.id = id.getText();
    form.lastName = lname.getText();
    form.firstName = fname.getText();
    form.course = course.getText();
    form.year = year.getText();
    forms.add(form);
}
willcodejavaforfood
A: 

The pertinent part in your posted code is in your ActionListener, where you handle the button click. First off, You can maintain the ArrayList model you have requested as a List of Lists of Strings (List<List<String>>) - or of types defined in @willcodejavaforfood's Form structure. This way you will have an easy time of retaining previous rows. Each time the button is clicked, you can grab the data from the text fields as you have already coded and now simply add it to the model as a new row. Then you can iterate over the model and replace the data in the JTextArea.

The declaration of your model would look like this:

List<List<String>> model = new ArrayList<List<String>>();

and your updated action listener would look something like this:

addB.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        id.selectAll();
        fname.selectAll();
        lname.selectAll();
        course.selectAll();
        year.selectAll();
        String textID = id.getSelectedText();
        String textFName = fname.getSelectedText();
        String textLName = lname.getSelectedText();
        String textCourse = course.getSelectedText();
        String textYear = year.getSelectedText();

        List<String> line = Arrays.asList(new String[]{textID,textFName,textLName,textCourse,textYear});
        model.add(line);

        StringBuilder sb = new StringBuilder();
        sb.append("ID\tFirst\tLast\tCourse\tYear\n");
        for(List<String> input : model) {
            for (String item : input) {
                sb.append(item);
                if (input.indexOf(item) == input.size()-1) {
                    sb.append("\n");
                } else {
                    sb.append("\t");
                }
            }
        }
        summary.setText(sb.toString());
    }
});

It is a little brute-force, but it gets the job done. There is more that can be done to ensure proper column alignment and a prettier way to ensure a newline at the end of a line, etc, but that, as they say, is left as an exercise for the reader.

akf