How can I pass a Java textbox to a list of arrays?

Guys, kindly help me pass the values ​​of my inputs in my JTextField (ID, LastName, FirstName, Course, Year) to my ArrayList without replacing the existing elements. In the meantime, I will use the stored ArrayList values ​​to add to 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);

      

-3


a source to share


2 answers


I'll take a hit, 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 the form to your list of forms every time:



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 an action listener for the 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);
}

      

+1


a source


The relevant part of your hosted code is in yours ActionListener

, where you handle the button click. First, you can save the model ArrayList

you requested as a list of lists of strings (List <List <String>); or the types defined in the @willcodejavaforfood form structure. This way it will be easy for you to keep the previous lines. Each time the button is clicked, you can grab data from the text fields as you have already coded, and now just add it to the model as a new line. Then you can iterate over the model and replace the data in the JTextArea.

Your model declaration will look like this:

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

      



and your updated action listener will 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's a bit brute force, but it gets the job done. This can be done to provide proper column alignment and a nicer way to provide a new line at the end of a line, etc., but that being said, is left as an exercise for the reader.

0


a source







All Articles