0

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.

Community
  • 1
  • 1
Arc
  • 441
  • 1
  • 9
  • 26
  • I'm not a Java UI developer but I suspect the problem is that you are calling `print(...)` from outside of the display thread. You are trying to update `document` from in another thread and I suspect that you need some synchronization there or need to use some UI construct. – Gray Feb 27 '14 at 18:38
  • Ok so heres an update, the code gets to if(command.get(0).equals("dt")) line but doesn't enter the loop. Any ideas? – Arc Feb 27 '14 at 18:50
  • I'd use a debugger: http://www.vogella.com/tutorials/EclipseDebugging/article.html – Gray Feb 27 '14 at 18:57

2 Answers2

0

Most of the problem like "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)" you mentioned is because code flow can't reach at that point for some reason. Since access() is called in inputFieldActionPerformed(), you should add breakpoint there and check if the function is executed.

wonhee
  • 1,581
  • 1
  • 14
  • 24
  • Ok so heres an update, the code gets to if(command.get(0).equals("dt")) line but doesn't enter the loop. Any ideas? – Arc Feb 27 '14 at 18:49
  • So what was the problem? Guessing that command.get(0) didn't have "dt" string in it? – wonhee Feb 28 '14 at 09:02
0

Once again my own syntax messes me up... watch

        print(inputField.getText());
            inputField.setText("");
            inputField.requestFocus();
        String[] temp = inputField.getText().split(" ");
        LinkedList<String> command = new LinkedList<>(Arrays.asList(temp));
        System.out.println("reaching access");
        access(command); // handles the command
        System.out.println("passed access");

Notice that the

inputField.setText("");
                inputField.requestFocus();

lines reset to blank BEFORE I split whatever was inside the text field. So when I pass the command linkedlist through the loop it has nothing in it.

Fixed by moving two lines of code...

        print(inputField.getText());
        String[] temp = inputField.getText().split(" ");
        LinkedList<String> command = new LinkedList<>(Arrays.asList(temp));
        System.out.println("reaching access");
        access(command); // handles the command
        System.out.println("passed access");
            inputField.setText(""); // notice its at the end now
            inputField.requestFocus(); // notice its at the end now

sigh I feel dumb

Arc
  • 441
  • 1
  • 9
  • 26
  • This is why we format our code. In Eclipse Source menu -> Format. It does some stupid things on occasion but otherwise it is highly recommended -- especially when multiple developers are involved. If you need to you can edit the formats but the defaults are also recommended. – Gray Feb 27 '14 at 20:40