0

I am making a program that lets user enter file path in a JTextField and displays the contents of the file in a JTextArea when the "View" JButton is clicked or the Enter Key is pressed.

Problem: Clicking the "View" button works (the file contents get displayed). But, the enter key ain't working. I think southPanel.setFocusable(true) is messing.

import java.awt.BorderLayout;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.*;

public class CFrame extends JFrame{
JTextArea outputTextArea = new JTextArea(); // Create a TextArea on which the contents of the file will be displayed
JPanel southPanel = new JPanel(); // Create a panel to be placed at the bottom of the frame
JScrollPane output = new JScrollPane(outputTextArea); // Create a ScrollPane for outputTextArea
JButton view = new JButton("View"); // This button, when clicked, displays the file's contents in outputTextArea
JTextField fileNameField = new JTextField(); // File Path/Name is entered in this field
CFrame() {
    southPanel.setFocusable(true);
    southPanel.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            if(e.getKeyCode() == e.VK_ENTER) {
                view.setEnabled(true);
            }
        }
    });
    southPanel.setLayout(new BorderLayout());
    southPanel.add(fileNameField,BorderLayout.CENTER);
    southPanel.add(new JLabel("  File Name:  "), BorderLayout.WEST);
    southPanel.add(view, BorderLayout.EAST);
    outputTextArea.setEditable(false);
    setLayout(new BorderLayout(0,1));
    add(output,BorderLayout.CENTER);
    add(southPanel,BorderLayout.SOUTH);
    view.addActionListener(new ButtonListener());
}

public class ButtonListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        String fileName = fileNameField.getText();
        String data = "";
        File file = new File(fileName);
        try {
            Scanner input = new Scanner(file);
            while(input.hasNextLine()) {
                data = data + input.nextLine() + "\n\r";
            }
            outputTextArea.setText(data);
        }
        catch(FileNotFoundException ex) {
            System.out.println("No such file");
        }
        southPanel.requestFocusInWindow();
    }
}
}
Wololo
  • 841
  • 8
  • 20

1 Answers1

4

enter file path in a JTextField and displays the contents of the file in a JTextArea when the "View" JButton is clicked or the Enter Key is pressed

Your design is wrong. You should not be attempting to listener for the Enter key on the panel.

Add the ActionListener to the text field. If the text field has focus and you press Enter the ActionListener is invoked.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • What if I want the JPanel to respond to the KeyEvent? Because after this program, I will be making a game in which a circle (displayed in a JPanel) will be moved left and right by pressing the LEFT and RIGHT arrow keys. – Wololo Jul 15 '15 at 02:52
  • @Saud then you should use key-binding .example http://stackoverflow.com/questions/17387037/java-swing-keybinding and http://stackoverflow.com/questions/23486827/java-keylistener-vs-keybinding – Madhawa Priyashantha Jul 15 '15 at 03:00
  • Ok... Thanks... will give a try to KeyBinding – Wololo Jul 15 '15 at 03:06
  • @Saud, `What if I want the JPanel to respond to the KeyEvent?` - solve one problem at a time. Moving a circle is a different question then you asked in this posting. Keep your questions simple and don't ask multiple question in the same posting because different questions will have different solutions. If the question asked in this posting has been answered, then "accept" the answer so people know the problem has been solved. – camickr Jul 15 '15 at 03:43