3

Made this little program to teste some GUI and Tess4j.

public static void main(String[] args) {

    JButton open = new JButton();
    JFileChooser fc = new JFileChooser();
    fc.setCurrentDirectory(new java.io.File("C:"));
    fc.setDialogTitle("Classificador de Documentos");
    if (fc.showOpenDialog(open) == JFileChooser.APPROVE_OPTION){

    }
    JFrame wait = new JFrame("Resultado");
    wait.setVisible(true);
    wait.setSize(300, 300);
    JLabel labelwait = new JLabel("Loading");
    JPanel panelwait = new JPanel();
    wait.add(panelwait);
    panelwait.add(labelwait);

    File imageFile = new File(fc.getSelectedFile().getAbsolutePath());
    ITesseract instance = new Tesseract();
    instance.setLanguage("por");// JNA Interface Mapping
    // ITesseract instance = new Tesseract1(); // JNA Direct Mapping

    try {
        String result = instance.doOCR(imageFile);

        if (result.toLowerCase().contains("ecocardiograma")){
            JFrame frame = new JFrame("Resultado");
            frame.setVisible(true);
            frame.setSize(300, 300);
            JLabel label = new JLabel("Este ficheiro é um ecocardiograma");
            JPanel panel = new JPanel();
            frame.add(panel);
            panel.add(label);
        }
        else {
            JFrame frame = new JFrame("Resultado");
            frame.setVisible(true);
            frame.setSize(300, 300);
            JLabel label = new JLabel("Este ficheiro não é um ecocardiograma");
            JPanel panel = new JPanel();
            frame.add(panel);
            panel.add(label);
        }
    } catch (TesseractException e) {
        JFrame frame = new JFrame("Resultado");
        frame.setVisible(true);
        frame.setSize(300, 300);
        JLabel label = new JLabel(e.getMessage());
        JPanel panel = new JPanel();
        frame.add(panel);
        panel.add(label);
    }
}

When running this code on Eclipse IDE it works as intended, but when running on a .jar file it gets stuck on the second frame labeled "Loading"

What can be the issue?

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
  • How are you making your jar? – Gatusko Mar 19 '18 at 19:59
  • File->Export->Running Jar File || Regarding the library handling i tried all options – Mário Ribeiro Mar 19 '18 at 20:02
  • 1
    `new JFrame()` to start an application is known as buggy, especially considering strange refresh behavior. There's no way to guarantee it, but using `EventQueue.invokeLater(Runnable)` might solve your problem. For more information see [here](https://stackoverflow.com/questions/22534356/java-awt-eventqueue-invokelater-explained). – Izruo Mar 19 '18 at 20:07

1 Answers1

0

If you Use Swing The solution is :

put your code in a thread safe

java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            // your main code ...
        }
    });

Example :

java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
           JFrame wait = new JFrame("Resultado");
           wait.setVisible(true);
           wait.setSize(300, 300);
           JLabel labelwait = new JLabel("Loading");
           JPanel panelwait = new JPanel();
           wait.add(panelwait);
           panelwait.add(labelwait);
        }
    });

If you Use JavaFx The solution is :

Platform.runLater(() -> {
        //your main code her ...
});

Example :

Platform.runLater(() -> {
        JFrame wait = new JFrame("Resultado");
        wait.setVisible(true);
        wait.setSize(300, 300);
        JLabel labelwait = new JLabel("Loading");
        JPanel panelwait = new JPanel();
        wait.add(panelwait);
        panelwait.add(labelwait);
});