To be blunt about it, I'm looking for help as to how to actually use it. We have just been set work using this on my course, but our new teacher does not teach, and I'm really struggling with this one. So I have a basic JFrame set up using windows builder, and the object is to be able to open a text file as a string and put it into the text space, and then be able to find strings in the text and change them. I'll paste the code I have below, if anyone can help explain how to do this, I'll really appriciate it, thanks. :)
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.TextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.filechooser.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class task1 extends JFrame {
private JPanel contentPane;
private JTextField findTxtBox;
private JButton findBtn;
private JTextField replaceTxtBox;
private JTextField fileTxtBox;
private JButton openBtn;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
task1 frame = new task1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public task1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 312);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
TextArea textArea = new TextArea();
textArea.setBounds(10, 45, 380, 160);
contentPane.add(textArea);
findTxtBox = new JTextField();
findTxtBox.setBounds(80, 211, 236, 20);
contentPane.add(findTxtBox);
findTxtBox.setColumns(10);
findBtn = new JButton("Find");
findBtn.setBounds(326, 210, 89, 23);
contentPane.add(findBtn);
JButton btnReplace = new JButton(" Replace");
btnReplace.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnReplace.setBounds(326, 240, 89, 23);
contentPane.add(btnReplace);
replaceTxtBox = new JTextField();
replaceTxtBox.setColumns(10);
replaceTxtBox.setBounds(80, 242, 236, 20);
contentPane.add(replaceTxtBox);
fileTxtBox = new JTextField();
fileTxtBox.setColumns(10);
fileTxtBox.setBounds(80, 11, 236, 20);
contentPane.add(fileTxtBox);
final JFileChooser fc = new JFileChooser();
fc.setFileFilter(new FileNameExtensionFilter("Text Files", "txt"));
fc.removeChoosableFileFilter(fc.getAcceptAllFileFilter());
openBtn = new JButton("Open File");
openBtn.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
fc.showOpenDialog(null);
}
});
openBtn.setBounds(326, 10, 89, 23);
contentPane.add(openBtn);
}
}