-1

The problem I am having is when I ping an IP, the JTextArea does not update until the pinging is finished. The goal here is for the JTextArea to update every time the IP is pinged. This is my first time using the swing library and I couldn't find any answers online. Any help would be greatly appreciated, thanks!

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;

public class pingGUI 
{
    private JFrame mainFrame;       //initializing objects
    private JLabel headerLabel;
    private JLabel statusLabel;
    private JPanel controlPanel;
    private JTextField inputIP;
    private JButton pingButton;
    private JTextArea textArea;
    private JScrollPane scroll;

    public pingGUI(){
        prepareGUI();
    }
    public static void main(String[] args){
        pingGUI window = new pingGUI();  
        window.showEvent();       
    }
    private void prepareGUI(){
        mainFrame = new JFrame("Ping Test");
        mainFrame.setSize(400,400);
        mainFrame.setLayout(new GridLayout(4, 0));

        headerLabel = new JLabel("",JLabel.CENTER);
        statusLabel = new JLabel("",JLabel.CENTER);
        statusLabel.setVerticalAlignment(0);
        statusLabel.setSize(350,100);

        mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent){
                System.exit(0);
            }        
        });    
        controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout());

        mainFrame.add(headerLabel);
        mainFrame.add(controlPanel);
        mainFrame.add(statusLabel);
        mainFrame.setVisible(true);
    }

    private void showEvent(){

        headerLabel.setText("Enter a IP address to ping."); 
        inputIP = new JTextField(15);
        pingButton = new JButton("Ping");
        textArea = new JTextArea();
        textArea.setColumns(20);
        textArea.setRows(20);
        textArea.setLineWrap(true);
        textArea.setEditable(false);
        textArea.setVisible(true);
        scroll = new JScrollPane(textArea);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        pingButton.setActionCommand("Ping");
        pingButton.addActionListener(new ButtonClickListener());

        controlPanel.add(pingButton);
        controlPanel.add(inputIP);
        mainFrame.add(scroll);
        mainFrame.setVisible(true); 
    }   
    public void printIP(String s) {
           textArea.append(s + "\n");
       }
    private class ButtonClickListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand(); 
            if (command.equals( "Ping" ))  {
                if (!inputIP.getText().isEmpty()) {
                    statusLabel.setText("Pinging IP Address...");
                    runPing("ping " + inputIP.getText());
                }
                else
                    statusLabel.setText("No IP address entered.");
            }
        }
    }
    public void runPing(String command) {   //Ping user specified IP address
        try {
            Process p = Runtime.getRuntime().exec(command); //accepts a command and returns according to command received.
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

            String str = "";
            while ((str = input.readLine()) != null) {
                printIP(str);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

1 Answers1

1

Your implementation of runPing() is blocking the event dispatch thread, so no GUI updates can occur until after it completes. Instead, use a SwingWorker to run the command in the background and publish interim updates. Starting form this complete example using ProcessBuilder, the following change produces the result shown.

@Override
protected Integer doInBackground() {
    try {
        ProcessBuilder pb = new ProcessBuilder("ping", "-c 3", "example.com");
    …
}

image

trashgod
  • 203,806
  • 29
  • 246
  • 1,045