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);