0

I am making a code editor in Java and I have used RSyntaxTextArea jar file to take help. The problem is - There is a ComboBox in application with which we can switch languages. There are two methods in RSyntaxTextArea: setSyntaxEditingStyle() and getSyntaxEditingStyle() which are not working inside ComboBox action listener. Please help me, I am badly stuck. Here is the code. Both the methods:

textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
and
String a = textArea.getSyntaxEditingStyle();

are not working inside Action Event function. They work perfectly outside but there is no way to use it outside.

public void actionPerformed(ActionEvent e) {
                
    if ( language == "Python") {
        try {
            textArea.setSyntaxEditingStyle(null);
            textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_PYTHON);
            textArea.getSyntaxEditingStyle();
            String command = "python " + opened_file; 
            Runtime.getRuntime().exec("cmd /c start cmd.exe /K " + command);
        } catch (IOException e5) {
            e5.printStackTrace();
        }
    }
    
    else if (language == "Java") {
        try {
            // Look Here
            // Look Here
            // Look Here
            // Look Here
            // Look Here
            // Look Here
            // Look Here
            // Look Here
            // Look Here {
         textArea
          .setSyntaxEditingStyle(
              SyntaxConstants.SYNTAX_STYLE_JAVA
          );

            String a = textArea.getSyntaxEditingStyle();
            System.out.println(a);
                    
            // Look Here }
            // Look Here
            // Look Here
            // Look Here
            // Look Here
            // Look Here
            // Look Here
            // Look Here
            
            String classpath = opened_file.replace(".java", "");
            String [] last_name_list = opened_file.split("\\\\");
            String lastname = last_name_list[last_name_list.length - 1];
            String folderpath = opened_file.replace(lastname, "");
            String []classpath_1 = classpath.split("\\\\");
            String class_file = classpath_1[classpath_1.length-1];
            
            Runtime rt = Runtime.getRuntime();
            rt.exec("cmd /c start cmd.exe /K \"cd " + folderpath + " && javac " + opened_file + " && java " + class_file +"\"");
            
            
        }catch (Exception e5) {
            e5.printStackTrace();
        }
    }
Amal K
  • 4,359
  • 2
  • 22
  • 44
  • 4
    Related: [How do I compare Strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – maloomeister Jun 22 '21 at 06:14
  • 1
    Furthermore, what do you mean with the methods "_are not working inside combobox action listner_"? What are you seeing? Are you getting any exceptions? If you do, share the exceptions in your question. Ideally, provide a [mre] so that people can help you more easily. – maloomeister Jun 22 '21 at 06:18
  • 1
    Did you check those methods are called at all, i.e. did you step through your code with a debugger already? And did you check what those methods are doing? I remember cases where some setters on ui components actually trigger events to make the changes and the getter would return the new state only after this event has been consumed. If that's the case here it's obvious why it doesn't work: you're already running _on_ the event dispatch thread and even if not, processing would probably not have finished right after calling the setter. – Thomas Jun 22 '21 at 06:19
  • If you are not getting any exceptions and your only issue is that you are not seeing the Swing component being updated on your UI as you expect it to, you might have to `revalidate()` and `repaint()` to tell Swing that the UI should be updated. – maloomeister Jun 22 '21 at 06:24
  • @maloomeister Hello Sir. No error is being shown and the setter is not setting the theme. And the getter prints the theme (String) outside the actionlistner but prints nothing when used inside it. As you can see I have used printstacktrace to find errors but sadly it shows nothing – Code With Raiju Jun 22 '21 at 06:24
  • @maloomeister Yes Sir I have tried using revalidate and repaint but it still does not work – Code With Raiju Jun 22 '21 at 06:28
  • @Thomas So is there any solution if processing is not finished? – Code With Raiju Jun 22 '21 at 06:30
  • The first thing you should do is fixing your `String` comparison as noted in the first comment. Then, you should update your question to provide a [mre] of your problem. Did you actually debug through your code as already noted by @Thomas to see if the methods are even called? Because with your current way to compare the `String`s, this all looks fishy to me. – maloomeister Jun 22 '21 at 06:32
  • 1
    You might not have read the link @maloomeister posted (if not: you should) so let me put it into a few words: `language == "Java"` might return false even if `language` has the string value `Java` (for details see the link). Thus use `equals()` and to prevent potential nullpointers try it this way: `"Java".equals(language)`. – Thomas Jun 22 '21 at 06:38
  • @maloomeister Sorry Sir as my data provided was not enough and I will update it. And Sir I am feeling ashamed to tell that I don't know how to debug. I have created decent applications in java swing like calculator, tic tac toe game and now this code editor which can run code of 4 languages. Sir I dont think there is a problem with string comparision because after getters and setters, the code executes file in cmd and it is running perfectly. The getters and setters methods just changes editor style like in python it will colorise def() and all keywords and in java it will color all keywords – Code With Raiju Jun 22 '21 at 06:43
  • @Thomas Ok sir I will see right now. Thanks :) – Code With Raiju Jun 22 '21 at 06:44
  • Or, as an alternative to what Thomas said about `"Java".equals(language)`, you could use `Objects.equals(language, "Java")`. – MC Emperor Jun 28 '21 at 12:03

1 Answers1

-1

I solved the question. There are sometimes where getters and setters do not work in action functions.. To fix this we can use threading. First Declare your variable to volatile.

Then in thread function use if else and use getters and setters there