0

I have a code where I'm taking input from user and after clicking on SUBMIT button it executes my logic and shows user a JTextArea on a new frame but the problem is it shows after program executes totally. I have used System.out.println and consoleText.append to see if it's happening on both eclipse and JTextArea but console on eclipse was updating with the code executes but JTextArea only shows when code executes totally.

Here's the code -

MainApp.java

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import com.skillnetinc.marker.utility.InputUtility;

public class MainApp {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                showGUI();
            }
        });
    }

    protected static void showGUI() {
        JFrame inputFrame = new JFrame("Marker Deletion Script");
        inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        inputFrame.setSize(800, 300);// 800 width and 500 height
        inputFrame.setLayout(null);// using no layout managers
        inputFrame.setVisible(true);// making the frame visible

        inputFrame.setLocationRelativeTo(null);

        InputUtility.showUserInputFields(inputFrame);
        InputUtility.buttonActivity(inputFrame);

    }

}

& InputUtility.java

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class InputUtility {

    public static JFrame tableFrame;
    public static JTextArea markerDesc, consoleText;
    public static JLabel labelMarkerDesc;

    public static void showUserInputFields(JFrame inputFrame) {
        labelMarkerDesc = new JLabel("<html>Marker<br>Description</html>");
        labelMarkerDesc.setBounds(50, 115, 100, 30);
        markerDesc = new JTextArea(10, 20);
        markerDesc.setBounds(150, 15, 300, 230);
        markerDesc.setBorder(BorderFactory.createLineBorder(Color.gray));

        inputFrame.add(markerDesc);
        inputFrame.add(labelMarkerDesc);
    }

    public static void buttonActivity(JFrame inputFrame) {
        JButton submit = new JButton("SUBMIT");
        submit.setBounds(520, 50, 150, 40);
        submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                consoleText = new JTextArea();
                consoleText.setEditable(false);
                consoleText.setVisible(true);
                JScrollPane sp = new JScrollPane(consoleText);

                tableFrame = new JFrame("Script Result");
                tableFrame.add(sp);
                tableFrame.setSize(800, 300);
                tableFrame.setVisible(true);
                tableFrame.setLocationRelativeTo(null);

                testConsole(consoleText);

            }

        });
        inputFrame.add(submit);
    }

    public static void testConsole(JTextArea consoleText) {
        String marker[] = InputUtility.markerDesc.getText().split("\n");
        for (int i = 0; i < marker.length; i++) {
            String posName = marker[i].split(" ")[0];

            File file = new File("\\\\" + posName + "\\C$\\environment\\marker");
            consoleText.append("\nChecking for marker inside " + file);
            System.out.println("\nChecking for marker inside " + file);

            File[] files = file.listFiles();
            if (file.canRead()) {
                consoleText.append("Found Total " + files.length + " markers inside " + file);
                System.out.println("Found Total " + files.length + " markers inside " + file);
            }

        }
    }

}

Input from user will be something like

192.168.75.18    startup.err
192.168.87.99    startup.err
192.168.66.38    startup.err
Akash
  • 1
  • 1
  • Does this answer your question? [Dynamically refresh JTextArea as processing occurs?](https://stackoverflow.com/questions/629315/dynamically-refresh-jtextarea-as-processing-occurs) – MDK Nov 13 '20 at 14:00
  • 1) an application should only have a single parent JFrame. For child windows you can use a JDialog 2) Your task is running on the `Event Dispatch Thread (EDT)` and the GUI can't be refreshed until all processing is finished. The solution is to use a separate Thread. The easiest way to do this is to use a `SwingWorker` and "publish()" the results as they become available. Read the section from the Swing tutorial on [Concurrency](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) for more information and an example of a SwingWorker. – camickr Nov 13 '20 at 15:59
  • Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Nov 13 '20 at 20:06

0 Answers0