I've been at this for ages now and I'm finally frustrated enough to ask about it. I'm at the limits of what I can do with my very basic understanding of applications. I'd really appreciate any help or advice in any aspect of the program.
My goal in this program is to create what is essentially an "autocorrect" feature on a simple text editor. In a folder called "commands" there are a series of text files titled like "ThisCommand(" that contain a string such as "ThisCommand(0000,1111,2222)". Whenever I type something that gets recognized as something with this format in the text editor, I want to fill in what's inside the file of the same name. For example, in the final application if I typed "ThisCommand(" in the editor the program would automatically change my entry to "ThisCommand(0000,1111,2222)".
Breaking this down into it's individual pieces, I already know how to...
- Load and save files from this folder.
- Build an application window with JFrame and it's associated containers.
- I have some understanding of action listeners with buttons.
What I'm struggling with is...
- Understanding where individual code goes in an UI application file. I'm not sure where in the code I can even access the text sitting in the text editor and then use it for this purpose.
- Understanding how to actively replace text in a window (or passively for that matter).
Here is my relevant code so far. Everything works, I'm just not sure where to go from here.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JSeparator;
public class UI {
private JFrame frame;
private JTextArea textArea;
private static ArrayList<String> commands;
private static ArrayList<String> commands_override;
public static ArrayList<String> getCommands() {return commands;}
public static ArrayList<String> getCommands_Override() {return commands_override;}
/**
* Launch the application.
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
importFolderContents();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UI window = new UI();
window.frame.setVisible(true);
} catch (Exception e) {e.printStackTrace();}
}
});
}
/**
* Create the application.
*/
public UI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setBounds(0,0,screenSize.width, screenSize.height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File");
menuBar.add(file);
JMenuItem NewEvent = new JMenuItem("New");
file.add(NewEvent);
JSeparator separator1 = new JSeparator();
file.add(separator1);
JMenuItem SaveEvent = new JMenuItem("Save"); // TODO JFileChooser
file.add(SaveEvent);
JMenuItem LoadEvent = new JMenuItem("Load");
file.add(LoadEvent);
JSeparator separator2 = new JSeparator();
file.add(separator2);
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
JMenu help = new JMenu("Help");
menuBar.add(help);
JMenuItem Shortcuts = new JMenuItem("Shortcuts");
help.add(Shortcuts);
frame.getContentPane().add(menuBar, BorderLayout.NORTH);
textArea = new JTextArea();
JScrollPane scroll = new JScrollPane(textArea);
frame.getContentPane().add(scroll, BorderLayout.CENTER);
JPanel buttons = new JPanel();
frame.getContentPane().add(buttons, BorderLayout.WEST);
JButton btnNewButton = new JButton("New button");
buttons.add(btnNewButton);
}
/** Imports the list of commands that can be replaced in the current session.
*
* Starts by combing the "commands" folder for the titles of each file. When it has
* made an array list of the names, an equivalent index array is filled with strings
* that will override the commands entered.
*
* @throws FileNotFoundException If this somehow gets thrown, god help you.
*
*/
private static void importFolderContents() throws FileNotFoundException {
commands = new ArrayList<String>();
commands_override = new ArrayList<String>();
// Imports the filenames of all of the contents of the commands folder
File folder1 = new File("./commands");
File[] fileList1 = folder1.listFiles();
for (int i = 0; i < fileList1.length; i++) {
String a = fileList1[i].getName();
if (a.contains(".txt")) {
String target = a.replaceFirst(".txt", "");
commands.add(target);
Scanner reader = new Scanner(fileList1[i]);
String replacement = reader.nextLine();
commands_override.add(replacement);
reader.close();
}
}
}
}