Create a responsive Java Swing application that highlights the first occurrence of a search term in a JTextArea using inner classes and event listeners. This case-sensitive text finder GUI features Find and Clear buttons, scrollable text area, and a graceful window close handler that displays a thank-you message. Ideal for students learning Java GUI programming, event handling, and string manipulation.
Overview
Read all of Problem 1 before starting, hints are given throughout the
problem
statement.
See the Execution Examples, below, to see what the GUI should look like.
Fields and Behaviors:
GUI Component
Behavior
TextArea
Will hold the text being
searched.
It needs to be updated
when
a
word is found.
This area is horizonta
l
l
y
and vertical
ly
scrollable.
TextField
Holds the word to be found
.
Find Button
Every time this bu
t
t
on is pressed, the
first
occurrence of the
“find” word in the TextArea will be located and highlighted.
A
named inner
class.
Clear Button
Clears the TextField that holds the word to match.
Sets the input focus to the ‘find’ text field.
This does NOT clear the text area.
An An
onymous inner class.
Windows close
Close using window listener/adapter inner class
, and print a
“Thank you for using finder” message.
An Anonymous inner class.
Add a WindowListener using WindowAdapter class
formatted similar to an
ActionListener.
Requirements:
1.
You need to develop your own inner classes to handle the events.
You must
handle the window clo
sing and button press events
.
2.
The search is case sensitive.
If the case of
the letters in the word in the
TextArea
does not match exactly
the word in the
TextField, skip over it.
3.
You only need to locate the
first
occurrence of a string of text.
Use ONE of the
string methods for this ‘find’ (look up the indexOf method of the Stri
ng class).
4.
The Find
operation is able to find text
such as in
this sentence
,
“this sentence”
should be found.
NOTE:
To get text
into
the text area, simply copy text from another window and paste it
Answer
import java.util.*;
import javax.swing.*;
import java.awt.Color;
import
java.util.logging.Level;
import
java.util.logging.Logger;
import
javax.swing.text.BadLocationException;
import
javax.swing.text.DefaultHighlighter;
import
javax.swing.text.Highlighter;
import
javax.swing.text.Highlighter.HighlightPainter;
import
java.util.logging.LogRecord;
public class Find
extends javax.swing.JFrame {
public
Find() {
initComponents();
}
@SuppressWarnings("unchecked")
//
<editor-fold defaultstate="collapsed" desc="Generated
Code">
private
void initComponents() {
jLabel1
= new javax.swing.JLabel();
jTextField1
= new javax.swing.JTextField();
jButton1
= new javax.swing.JButton();
jButton2
= new javax.swing.JButton();
jScrollPane1
= new javax.swing.JScrollPane();
jTextArea1
= new javax.swing.JTextArea();
setTitle("Find");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new
java.awt.Font("Tahoma", 1, 12)); // NOI18N
jLabel1.setText("Find:");
jButton1.setText("Find");
jButton1.addActionListener(new
java.awt.event.ActionListener() {
public
void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Clear");
jButton2.addActionListener(new
java.awt.event.ActionListener() {
public
void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
//
print message when close window
setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
addWindowListener(new
java.awt.event.WindowAdapter() {
@Override
public void
windowClosing(java.awt.event.WindowEvent windowEvent) {
System.out.println("Thank
you for using finder");
JOptionPane.showMessageDialog(null,"Thank
you for using finder");
//THEN you can exit the
program
System.exit(0);
}
});
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
javax.swing.GroupLayout
layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(132,
132, 132)
.addComponent(jButton1,
javax.swing.GroupLayout.PREFERRED_SIZE, 70,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18,
18, 18)
.addComponent(jButton2,
javax.swing.GroupLayout.PREFERRED_SIZE, 73,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(43,
43, 43)
.addComponent(jLabel1,
javax.swing.GroupLayout.PREFERRED_SIZE, 60,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31,
31, 31)
.addComponent(jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE, 178,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 409,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(41,
Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addGap(19,
19, 19)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
.addGap(24,
24, 24)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField1,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(18,
18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap())
);
pack();
}//
</editor-fold>
private
void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//
TODO add your handling code here:
jTextField1.setText("");
}
private
void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//
TODO add your handling code here:
String
text = jTextArea1.getText();
String
searchTerm = jTextField1.getText();
if(text.contains(searchTerm)){
Highlighter
highlighter = jTextArea1.getHighlighter();
HighlightPainter
painter = new DefaultHighlighter.DefaultHighlightPainter(Color.pink);
int
p0 = text.indexOf(searchTerm);
int
p1 = p0 + searchTerm.length();
try
{
highlighter.addHighlight(p0,
p1, painter );
}
catch (BadLocationException ex) {
Logger.getLogger(Find.class.getName()).log(Level.SEVERE,
null, ex);
}
}
else{
JOptionPane.showMessageDialog
(null, "Not Found", "Error", JOptionPane.ERROR_MESSAGE);
}
}
public
static void main(String args[]) {
try
{
for
(javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if
("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}
catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Find.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Find.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Find.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Find.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
//</editor-fold>
/*
Create and display the form */
java.awt.EventQueue.invokeLater(new
Runnable() {
public
void run() {
new
Find().setVisible(true);
}
});
}
//
Variables declaration - do not modify
private
javax.swing.JButton jButton1;
private
javax.swing.JButton jButton2;
private
javax.swing.JLabel jLabel1;
private
javax.swing.JScrollPane jScrollPane1;
private
javax.swing.JTextArea jTextArea1;
private
javax.swing.JTextField jTextField1;
//
End of variables declaration
}