And I want to show the different words in two text files and make them bold, show on the screen.

my code compiles when entering a word show the common words in two txt files and underlined them. instead of entering the word, I want to read a txt file, the txt file have different words between two txt files, contents of this text file matches contents of other two text files, show them on the screen on these txt files as bold.

I'm doing simple programme like win merge program. for your help thank you for now.

this is my code d1.txt have the words : hey selam d1.txt : what up yo

the d3.txt file different words between d1.txt and d2.txt : hey selam what up yo

I want to read d3.txt file and these words should be bold on the other text files, on the screen as bold

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collection;
import java.util.ArrayList;
import java.util.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Document;
import javax.swing.text.Highlighter;
import javax.swing.text.JTextComponent;
import javax.swing.text.LayeredHighlighter;
import javax.swing.text.Position;
import javax.swing.text.View;

public class a {
    public static void main(String[] args) throws IOException {
        try {
            UIManager
                    .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception evt) {
        }

        JFrame f = new JFrame("Highlight example");

        final JTextPane textPane = new JTextPane();
        final JTextPane textPane2 = new JTextPane();
        // (textPane.setHighlighter(highlighter);
        // (textPane2.setHighlighter(highlighter);
        JPanel pane = new JPanel();
        pane.setLayout(new BorderLayout());
        pane.add(new JLabel("Enter word: "), "West");
        final JTextField tf = new JTextField();

        pane.add(tf, "Center");
        f.getContentPane().add(pane, "South");
        f.getContentPane().add(new JScrollPane(textPane), "West");
        f.getContentPane().add(new JScrollPane(textPane2), "East");

        try {
        textPane.read(new FileReader("D:\\Denemeler\\deneme1.txt"), null);
        } catch (Exception e) {
            System.out.println("Failed to load file " + args[0]);
            System.out.println(e);
        }

        try {
            textPane2.read(new FileReader("D:\\Denemeler\\deneme2.txt"), null);
        } catch (Exception e) {
            System.out.println("Failed to load file " + args[0]);
            System.out.println(e);

        }
        // compare(readFileAsList("D:\\Denemeler\\deneme1.txt"),
        // readFileAsList("D:\\Denemeler\\deneme2.txt"));
        f.setSize(400, 400);

        f.setVisible(true);

        final WordSearcher searcher = new WordSearcher(textPane);
        final WordSearcher searcher1 = new WordSearcher(textPane2);

        tf.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {

                word = tf.getText().trim();
                int offset = searcher.search(word);
                int offset1 = searcher1.search(word);

                if (offset1 != -1 && offset != -1) {
                    try {
                        textPane.scrollRectToVisible(textPane
                                .modelToView(offset1));
                    } catch (BadLocationException e) {
                    }

                    try {
                        textPane2.scrollRectToVisible(textPane2
                                .modelToView(offset1));
                    } catch (BadLocationException e) {
                    }

                }
            }
        });

        textPane.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent evt) {
                searcher.search(word);
            }

            public void removeUpdate(DocumentEvent evt) {
                searcher.search(word);
            }

            public void changedUpdate(DocumentEvent evt) {
            }
        });

        textPane2.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent evt) {
                searcher1.search(word);
            }

            public void removeUpdate(DocumentEvent evt) {
                searcher1.search(word);
            }

            public void changedUpdate(DocumentEvent evt) {
            }
        });

        f.setSize(400, 400);
        f.setVisible(true);

Recommended Answers

All 15 Replies

you have written all that code, yet you don't know how to read a list of words from a .txt file and use them as input?

next to that, maybe it's just me, but it does look a lot like an assignment, of which you copied the original base code, but you don't ask any questions. saying what you want to do is nice, but what is it you're having trouble with, exactly?

What is supposed to happen when the code is compiled and executed? Please explain what is shown on the screen.

only I didn't do it the highlighter method. it is also very difficult to do that

Can you explain what the problem is you are having with the code you posted?

I think I'm little bit confused cause I want to read the txt from JTextfield. but it is not necessary sorry

my code compiles when entering a word show the common words in two txt files. instead of entering the word, I want to read a txt file, the txt file have different words between two txt files, contents of this text file matches contents of other two text files, show them on the screen on these txt files as bold.

I am sorry, I do not clearly understand what you are trying to say here. What currently is your program doing? You mean your program reads 2 text files and then wait for a user to enter a word. If the word appears in both text file contents, the word will be highlighted in their content display?

Then you want to change your program to read 2 text files, compare their contents, and highlighted the words appear in both content? When you said "word," does it mean one word from one content could appear in multiple locations in the other content? Or one by one?

String contentA = "Normal day life is to be happy. If one is not happy, the one should get help.";
String contentB = "Today is a tough day for me. I am not happy, so what should I do?";
// When compare these 2 strings, would the result be similar to below?
// Note that the word "happy" appears twice in contentA but once in contentB
// ->Normal <day> life is to be <happy>. If one <is> <not> <happy>, the one <should> get help.
// ->Today <is> a tought <day> for me. I am <not> <happy>, so what <should> I do?
//
// Or you would allow only the first appearance to be highlighted
// Note that the second "happy" word is not highlighted
// ->Normal <day> life is to be <happy>. If one <is> <not> happy, the one <should> get help.
// ->Today <is> a tought <day> for me. I am <not> <happy>, so what <should> I do?

It seems to me that the file is given as an example for your homework/assignment, and you need to modify the code. I am not going to tell you where to modify it, but I could give you how to compare it. You could simply iterate through one file content (as each word) and compare with other file content. If you want a word to appear only at its first occurance, return the result once you find the word; otherwise, keep going through the whole content of the other file content. Though, it is still at least O(nxm) where n is the length of content file A and m is the lenght of content file B (this does not include the length of a word).

Then you want to change your program to read 2 text files, compare their contents, and highlighted the words appear in both content?

no its not actually both content. opposite it

first Im gonna tell you what I have done until now.
1. I can show contents of two text files on the screen, (using JTextArea, JScrollPane like that etc.)
2.In another place, I have compared contents two text files, I found the different words and write them another d3.txt file.
3. Finally I want to show these different words on the d1.txt and d2.text and show on the screen.
like a winmerge programme. these different words between d1.txt and d2.txt should be underlined or highlighted it doesnt matter I can do this part.

can you have an idea 1 and 2. steps combine with each other. forget about above.

>

shortly I want to find different words in two txt files and make them bold or highlighted, I want to seem to make them explicit on the screen.

Ok. What are your ideas on how to do that? Do you have any questions?

If d3.txt files matches other contents of files, they should be bold. But how can I do that?

Another comparing for that?

I'm new on this web site. How can I edit my question. I want to use my another code for the solution. Can you tell me?

The menu items time out. If there isn't an Edit Post item, you'll have to post a new version.

But I don't want to seem my question's solution on the web site. How can I delete it?

OK, regardless you want the same or different, there is no different in solution. In other words, you could easily reverse what I suggested earlier. Also, forget the GUI of display for now. You need to focus on the solution behind the display first.

What you may need to do is to simply read both file contents into variables. Then go through each word from a file over the other whole string. If the word is found, marked it in both file content. After you got all marked duplicated words in both content, move on to the display. In the display, instead of highlight the marked word, highlight the words that are unmarked. See what I mean? You do not need to change anything but the display which still gives you the same result.

Remember, do not try to think about display GUI for now. What you need to complete is the solution behind it. What you have done is OK but it adds more step into your solution. Because you will need to read from your saved duplicated word file, and then compare those words with both file contents again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.