-1

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);
    }
}
Daniel Speed
  • 90
  • 1
  • 3
  • 11
  • 1
    In your `openBtn` don't use `MouseListener` but [ActionListener](http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html) to handle the event when the button is pressed. Take a look to [How to Use Buttons, Check Boxes, and Radio Buttons](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html#abstractbutton) – dic19 Oct 22 '13 at 20:24

1 Answers1

3

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.

That involves a lot more than just using a file chooser. I suggest you start by reading the section from the Swing tutorial on How to Use File Choosers for a working example.

The file chooser is just used to get a file name not read a file. So next I would suggest you use a JTextArea (not a TextArea) to display the text from the file that you read. You can use the read(...) method of the JTextArea to do this.

All text components have a getText() method you can use the get the text. You can then search the string for whatever you want and replace the text by using the replace() method of a JTextArea.

Finally you should NOT be using the setBounds() method to set the size/location of a component. You should be using Layout Managers and let them do their job. The Swing tutorial also has a section on using layout managers.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks, I'll have a read. I didn't know, we haven't been taught seen as how our "tutor" is a lazy dick and I was just trying to guess at how to do it. Thanks for the response anyway :) – Daniel Speed Oct 22 '13 at 20:28
  • Ahh I see now, the JFileChooser is just a way of getting names from the directory like you said, but by creating a file and setting it to filechooser.getSelectedFile() and then using the BufferedReader/FileReader to make it into a string and display it in the text area. Thanks for passing on the info, I guess I should have gone to the oracle site as a start. Appriciate your help :) – Daniel Speed Oct 22 '13 at 21:18