java.lang.IllegalStateException kills GUI

20 views
Skip to first unread message

Victor Mozdzan

unread,
Nov 13, 2020, 12:10:40 PM11/13/20
to lanterna-discuss
Hello,

I am having an issue with a TextBox. I get a user input and try to set the text of the TextBox to that input. That TextBox has validation pattern set, and the setText() throws :

java.lang.IllegalStateException: TextBox validation pattern ^[1-9]{0,3}$ does not match the supplied text
        at com.googlecode.lanterna.gui2.TextBox.addLine(TextBox.java:227)
        at com.googlecode.lanterna.gui2.TextBox.setText(TextBox.java:183)

The problem is that when this occurs it kills my GUI.
My terminal is set up like this:

Terminal terminal = new UnixTerminal(System.in,
                                     System.out,
                                     Charset.defaultCharset(),
                                     UnixLikeTerminal.CtrlCBehaviour.TRAP);
  
Screen screen = new TerminalScreen(terminal);
MultiWindowTextGUI textGUI = createTextGUI(screen);

textGUI.setBlockingIO(false);
textGUI.setEOFWhenNoWindows(true);
textGUI.isEOFWhenNoWindows();
AsynchronousTextGUIThread guiThread = (AsynchronousTextGUIThread)textGUI.getGUIThread();
guiThread.start();
afterGUIThreadStarted(textGUI);
guiThread.waitForStop();

Is there a way to handle the IllegalStateException so it doesn't bring down the GUI?
I tied to wrap the setText() in try ... catch but it doesn't help

Thank you.

Victor Mozdzan

unread,
Dec 8, 2020, 3:49:34 PM12/8/20
to lanterna-discuss
I was able to to figure out a "solution". I noticed that if in the try block I set the value of the textbox to "" then the GUI doesn't quit on me. Below is my test case code:


import com.googlecode.lanterna.*;
import com.googlecode.lanterna.screen.*;
import com.googlecode.lanterna.bundle.*;
import com.googlecode.lanterna.TerminalSize;
import com.googlecode.lanterna.gui2.*;
import com.googlecode.lanterna.gui2.dialogs.*;
import com.googlecode.lanterna.gui2.Window.Hint;
import com.googlecode.lanterna.gui2.dialogs.MessageDialog;
import com.googlecode.lanterna.graphics.*;
import com.googlecode.lanterna.terminal.*;
import com.googlecode.lanterna.terminal.ansi.*;


import java.io.*;
import java.util.*;
import java.nio.charset.*;
import java.util.regex.*;

public class testCase {
   
      public static void main(String[] args) throws IOException {
          new testCase().run(args);
                                              
      }
                                          
      public void run(String[] args) throws IOException {       
       
        Terminal terminal = new UnixTerminal(System.in,
                                             System.out,
                                             Charset.defaultCharset(),
                                             UnixLikeTerminal.CtrlCBehaviour.CTRL_C_KILLS_APPLICATION);

    
       
        Screen screen = new TerminalScreen(terminal);         
        screen.startScreen();
        MultiWindowTextGUI textGUI = new MultiWindowTextGUI(new SeparateTextGUIThread.Factory(), screen);

        textGUI.setBlockingIO(false);
        textGUI.setEOFWhenNoWindows(true);
        textGUI.isEOFWhenNoWindows();
       
        Panel contentPanel = new Panel(new GridLayout(2));
       
        try {

            BasicWindow bw = new BasicWindow("testcase");
           
            TextBox textBox = new TextBox();
            textBox.setValidationPattern(Pattern.compile("^[a-z]{0,5}$"));
           
            try {
               
                textBox.setText("9");
               
            } catch(IllegalStateException e) {
                StringWriter sw = new StringWriter();
                e.printStackTrace(new PrintWriter(sw));
                String exceptionAsString = sw.toString();
                 // write exception to a file  
                FileWriter wr = new FileWriter("err");
                wr.write( exceptionAsString);
                wr.close();

                // this worked for me
                textBox.setText("");
            }
           
           
            textBox.setLayoutData(GridLayout.createLayoutData(
            GridLayout.Alignment.BEGINNING, // Horizontal alignment in the grid cell if the cell is larger than the component's preferred size
            GridLayout.Alignment.BEGINNING, // Vertical alignment in the grid cell if the cell is larger than the component's preferred size
            true,            // Give the component extra horizontal space if available
            false,           // Give the component extra vertical space if available
            1,  // Horizontal span
            1));  // Vertical span
           
            contentPanel.addComponent(textBox);
           
           
           
            bw.setHints(Arrays.asList(Window.Hint.FULL_SCREEN));
            bw.setComponent(contentPanel);
            textGUI.addWindow(bw);


            AsynchronousTextGUIThread guiThread = (AsynchronousTextGUIThread)textGUI.getGUIThread();
            guiThread.start();
            guiThread.waitForStop();

        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            screen.stopScreen();
            System.exit(0);
        }
    }  // end of run() 
Reply all
Reply to author
Forward
0 new messages