Home > front end >  How to use actionlistener to find identical characters in text area 1,2 and show it in 3
How to use actionlistener to find identical characters in text area 1,2 and show it in 3

Time:02-22

I created a window with a button and 3 fields, 1st, 2nd field is the text, and in the 3rd field, it is necessary to write down words that are repeated in 1st & 2nd fields. How to make it?

 JFrame frame = new JFrame("new Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        Label lbTextLabel = new Label("1:");
        lbTextLabel.setBounds(50, 10, 120, 20);
 
        Label lbTextLabel2 = new Label("2:");
        lbTextLabel2.setBounds(50, 50, 120, 20);
 
        Label lbTextLabel3 = new Label("3:");
        lbTextLabel3.setBounds(50, 90, 120, 20);
 
        JTextArea TextArea = new JTextArea("Welcome to javatpoint");
        TextArea.setBounds(50,30, 120,20);
 
        JTextArea TextArea2 = new JTextArea("Welcome to javatpoint 2");
        TextArea2.setBounds(50,70, 127,20);
 
        JTextArea TextArea3 = new JTextArea("");
        TextArea3.setBounds(50,110, 127,20);
 
        JButton button =new JButton("Click Here");
        button.setBounds(140,130,95,30);
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                
            }
        });

CodePudding user response:

Tell me that the below code does what you want and I will add explanations.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Identify {
    private JTextArea  oneTextArea;
    private JTextArea  twoTextArea;
    private JTextArea  threeTextArea;

    private void createAndDisplayGui() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createMainPanel(), BorderLayout.CENTER);
        frame.add(createButtonsPanel(), BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createButtonsPanel() {
        JPanel buttonsPanel = new JPanel();
        JButton button = new JButton("Click Me");
        button.setMnemonic(KeyEvent.VK_C);
        button.addActionListener(this::findCommonWords);
        buttonsPanel.add(button);
        return buttonsPanel;
    }

    private JPanel createMainPanel() {
        JPanel mainPanel = new JPanel(new GridLayout(0, 2, 10, 10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        JLabel oneLabel = new JLabel("                                                  1:");
        oneLabel.setDisplayedMnemonic(KeyEvent.VK_1);
        mainPanel.add(oneLabel);
        oneTextArea = new JTextArea("Apple Mango Orange");
        mainPanel.add(oneTextArea);
        oneLabel.setLabelFor(oneTextArea);
        JLabel twoLabel = new JLabel("                                                  2:");
        twoLabel.setDisplayedMnemonic(KeyEvent.VK_2);
        mainPanel.add(twoLabel);
        twoTextArea = new JTextArea("Mango Orange Banana");
        mainPanel.add(twoTextArea);
        twoLabel.setLabelFor(twoTextArea);
        JLabel threeLabel = new JLabel("                                                  3:");
        threeLabel.setDisplayedMnemonic(KeyEvent.VK_3);
        mainPanel.add(threeLabel);
        threeTextArea = new JTextArea(1, 10);
        mainPanel.add(threeTextArea);
        threeLabel.setLabelFor(threeTextArea);
        return mainPanel;
    }

    private void findCommonWords(ActionEvent event) {
        String text1 = oneTextArea.getText();
        String text2 = twoTextArea.getText();
        String[] words1 = text1.split(" ");
        String[] words2 = text2.split(" ");
        List<String> list1;
        List<String> list2;
        if (words1.length > words2.length) {
            list1 = new ArrayList<>(Arrays.asList(words1));
            list2 = new ArrayList<>(Arrays.asList(words2));
        }
        else {
            list1 = new ArrayList<>(Arrays.asList(words2));
            list2 = new ArrayList<>(Arrays.asList(words1));
        }
        list1.retainAll(list2);
        threeTextArea.setText(list1.stream().collect(Collectors.joining(" ")));
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new Identify().createAndDisplayGui());
    }
}

This is what it looks like when I run it (before clicking on the button).

screen capture

CodePudding user response:

You have to split both textarea word and check if first textarea word is matched with second textarea word, If match print it in third textarea.

Here down is example:

import java.awt.event.*;  
import javax.swing.*;    
import java.util.*;
class CompareWord{
    public static void main(String[] args) {
        JFrame f;
        JLabel lbTextLabelt1, lbTextLabel2, lbTextLabel3;
        JTextArea TextArea, TextArea2, TextArea3;
        JButton button;

        f = new JFrame("new Frame");
        lbTextLabelt1 = new JLabel("1:");
        lbTextLabelt1.setBounds(50, 10, 120, 20);

        lbTextLabel2 = new JLabel("2:");
        lbTextLabel2.setBounds(50, 70, 120, 20);

        lbTextLabel3 = new JLabel("3:");
        lbTextLabel3.setBounds(50, 140, 120, 20);

        TextArea = new JTextArea("Welcome to javatpoint");
        TextArea.setBounds(50,30, 120,30);

        TextArea2 = new JTextArea("Welcome to javatpoint");
        TextArea2.setBounds(50,90, 127,30);

        TextArea3 = new JTextArea("");
        TextArea3.setBounds(50,160, 127,30);

        button = new JButton("Click Here");
        button.setBounds(120,200,95,30);
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){

                String str1 = TextArea.getText();//Apple Mango Orange;
                String str2 = TextArea2.getText();//Mango Orange Banana;
                String str3 = "";

                String arrStr1[] = str1.split("//s ");
                String arrStr2[] = str2.split("//s ");

                List<String> wordsOfFirstStr = Arrays.asList(str1.split(" "));

                for (String wordsOfSecondStr : str2.split(" ")) {
                    if(wordsOfFirstStr.contains(wordsOfSecondStr))
                    {
                        str3  = wordsOfSecondStr   " ";
                    }
                }
                TextArea3.setText(str3);

            }
        });        

        f.add(lbTextLabelt1);
        f.add(lbTextLabel2);
        f.add(lbTextLabel3);
        f.add(TextArea);
        f.add(TextArea2);
        f.add(TextArea3);
        f.add(button);
        f.setSize(400, 400);
        f.setLayout(null);
        f.setVisible(true);
    }
}

Output:

enter image description here

  • Related