0

I have this GUI program where I'm trying to basically copy windows CMD. Since I have lots of features in this program, I decided to put parts of the code in different classes. But it doesn't respond.

       if(command.size()<2 && command.size()>0) {
            switch(command.get(0)) {
                case "dt":
                    getDateTime a = new getDateTime();
                    a.Start();
                    break;
                // other case(s) down below
            }
        }

Here is the geDateTime class

public class getDateTime {
    public static void Start() {
        Terminal t = new Terminal();
        try {
            DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            Date date = new Date();
            String s = dateFormat.format(date).toString();
            t.print(s);
        }catch(Exception e){ e.printStackTrace(); }
    }
}

Here is the print(); void in the main class...

public static void print(String s) {
        Color c = Color.WHITE; // prints white text to JFrame
        Style style = output.addStyle("Style", null);
        StyleConstants.setForeground(style, c);
        try{
            document.insertString(document.getLength(), s, style);
        }catch(Exception e){e.printStackTrace();}
    }

Now when I enter the command for accessing the getDateTime class, the program freezes and I can't input anything. HOWEVER, if I just put the getDateTime class into a void inside the main class it works fine; but this would be a problem to just put everything into the main class since some function(s) could have hundreds of line of code.

No errors are produced when the program freezes.

Arc
  • 441
  • 1
  • 9
  • 26

1 Answers1

1

In the code snippet that you have earlier, the code was trying to create a new Terminal rather than using the existing one.

Try this:

private static void print() {
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    Date date = new Date();
    String s = dateFormat.format(date).toString();
    print(s);
}

In the access method:

case "dt":
    print();
    break;

Update: On a side note, try to avoid static if at all possible. Generally speaking, it's bad practice. See https://stackoverflow.com/a/7026563/1216965

Community
  • 1
  • 1
neowulf33
  • 635
  • 2
  • 7
  • 19
  • thanks for answering, just one quick question. Is this on a seperate class, or is this within the main class terminal.java? – Arc Feb 27 '14 at 01:41
  • The print method will be in the Terminal class. – neowulf33 Feb 27 '14 at 01:43
  • `On a side note, try to avoid static if at all possible. Generally speaking, it's bad practice.` How? Statics are a crucial part of programming... – Qix - MONICA WAS MISTREATED Feb 27 '14 at 01:44
  • @neowulf33 is there anything I can do to if a void would be hundreds of lines long? – Arc Feb 27 '14 at 01:45
  • I keep the size of my methods to around 30 lines which doesn't force the user to scroll when reading the method. You could break up the big void method into smaller void methods. – neowulf33 Feb 27 '14 at 01:50
  • @neowulf33 alright thanks, I guess i'll have to control my OCD – Arc Feb 27 '14 at 01:51
  • Be aware that both the question and this question ignore the issue of time zone. This code is correct only if that string represents [UTC](https://en.wikipedia.org/wiki/Coordinated_Universal_Time). – Basil Bourque Feb 27 '14 at 10:00