Just a heads up, this question extends from my previous one Java no response from other classes . The answers I got on there we're great, but I'm encountering new problems; so this is a completely different question.
Like I said before, I'm making a CMD copy where user enters a command in a JTextField, and output comes onto a JScrollPane/JTextPane.
Here's the code for the JTextField for whenever the user presses the ENTER key on the keyboard.
The problem is, once you read the code below, is that the code doesn't even reach the first debug line in the access(command); void. No errors produced, again, no idea what's going on. Once again, I WOULD LOVE if I could have the the code organized in seperate classes. But since this code only works if the actual code, like DateTime.class's code, is in the main class; I don't know what to do.
public void inputFieldActionPerformed(java.awt.event.ActionEvent evt) {
print(inputField.getText()); // prints whatever the user entered in textfield to pane
inputField.setText(""); // sets textfield blank
inputField.requestFocus(); // requests textfield's focus
String[] temp = inputField.getText().split(" "); // splits whatever user entered
LinkedList<String> command = new LinkedList<>(Arrays.asList(temp)); // adds the array above into a linkedlist because I prefer them
access(command); // handles the command
}
Code for the "access(command);" void.
public void access(LinkedList<String> command) {
if(command.size()<2 && command.size()>0) { // goes here if user enters only a single word command
if(command.get(0).equals("dt")) {
System.out.println("thread read"); // debug to see if code even get's this far
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("thread started"); // another debug
DateTime dt = new DateTime();
String s = dt.Start();
print(s);
System.out.println("thread finished"); // another debug
}
}).start();
}
}
else if(command.size()>2) { // goes here if user enters a multi-word command
}
}
DateTime class.
public class DateTime {
public String Start() {
String s="";
try {
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
s = dateFormat.format(date).toString();
}catch(Exception e){ e.printStackTrace(); }
return s;
}
}
Print function.
public static void print(String s) {
Color c = Color.WHITE;
Style style = output.addStyle("Style", null);
StyleConstants.setForeground(style, c);
try{
if(!s.startsWith(" ")) {
s = " "+s;
}
if(!s.endsWith("\n")) {
s = s + "\n";
}
document.insertString(document.getLength(), s, style);
}catch(Exception e){e.printStackTrace();}
}
Anything else just let me know.