# Ping in JTextArea anzeigen



## MaxG. (26. Apr 2017)

Hi,
hab ein Tool geschrieben in dem man eine IP Adresse angeben kann und diese wird dann angepingt. Dabei lass ich auch das Ergebnis in einer JTextArea ausgeben, allerdings wird nur die letzte Zeile ausgegeben. Zum Vergleich habe ich das ganze auch mit System.out.println() auf der Konsole ausgegeben, da gehts. Mit einem JLabel hab ich es auch schon probiert, ging auch nicht. Was muss ich machen das der ganze Text ausgegeben wird?
Hier der Code:

```
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class Ping {

    static JFrame mainFrame;
    static JFrame result;

    static JPanel panelButton;
    static JPanel panelTextField;
    static JPanel panelLabel;
   
    static JLabel infoLabel;
    static JTextArea textAreaResults;
    static JTextField input;
    static JButton buttonStart;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        mainFrame = new JFrame();
        mainFrame.setSize(250, 175);
        mainFrame.setTitle("Ping Tool");
        mainFrame.setLayout(new BorderLayout());
       
        result = new JFrame();
        result.setSize(500, 350);
       
        textAreaResults = new JTextArea();
        textAreaResults.setEditable(false);
       
        result.add(textAreaResults);
        result.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       
        panelButton = new JPanel();
        panelTextField = new JPanel();
        panelLabel = new JPanel();
       
        infoLabel = new JLabel("Bitte IP Adresse eingaben");
        input = new JTextField(13);
        buttonStart = new JButton("Ping Starten");
       
        panelButton.add(buttonStart);
        panelTextField.add(input);
        panelLabel.add(infoLabel);
       
        listener();
       
        mainFrame.add(panelLabel, BorderLayout.NORTH);
        mainFrame.add(panelTextField, BorderLayout.CENTER);
        mainFrame.add(panelButton, BorderLayout.SOUTH);
        mainFrame.setVisible(true);
        mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
   
   
    static private void showResults() throws IOException{
        mainFrame.dispose();
        result.setVisible(true);
        ProcessBuilder processBuilder = new ProcessBuilder(new String[] { "ping", input.getText().trim() });
        Process process = processBuilder.start();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
            textAreaResults.setText(line);
           
        }
        bufferedReader.close();
       
       
       
    }
   
    static private void listener(){
        buttonStart.addActionListener(new ActionListener() {
           
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                try {
                    showResults();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }

}
```


----------



## thet1983 (26. Apr 2017)

MaxG. hat gesagt.:


> textAreaResults.setText(line);


weil du immer den text überschreibst....


----------



## MaxG. (26. Apr 2017)

Vielen Dank für die Antwort.
Hier der Verbesserte Code:

```
package Sonstige;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class Ping {

    static JFrame mainFrame;
    static JFrame result;

    static JPanel panelButton;
    static JPanel panelTextField;
    static JPanel panelLabel;
   
    static JLabel infoLabel;
    static JTextArea textAreaResults;
    static JTextField input;
    static JButton buttonStart;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        mainFrame = new JFrame();
        mainFrame.setSize(250, 175);
        mainFrame.setTitle("Ping Tool");
        mainFrame.setLayout(new BorderLayout());
       
        result = new JFrame();
        result.setSize(500, 350);
       
        textAreaResults = new JTextArea();
        textAreaResults.setEditable(false);
       
        result.add(textAreaResults);
        result.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       
        panelButton = new JPanel();
        panelTextField = new JPanel();
        panelLabel = new JPanel();
       
        infoLabel = new JLabel("Bitte IP Adresse eingaben");
        input = new JTextField(13);
        buttonStart = new JButton("Ping Starten");
       
        panelButton.add(buttonStart);
        panelTextField.add(input);
        panelLabel.add(infoLabel);
       
        listener();
       
        mainFrame.add(panelLabel, BorderLayout.NORTH);
        mainFrame.add(panelTextField, BorderLayout.CENTER);
        mainFrame.add(panelButton, BorderLayout.SOUTH);
        mainFrame.setVisible(true);
        mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
   
   
    static private void showResults() throws IOException{
        mainFrame.dispose();
        result.setVisible(true);
        ProcessBuilder processBuilder = new ProcessBuilder(new String[] { "ping", input.getText().trim() });
        Process process = processBuilder.start();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
            textAreaResults.setText(textAreaResults.getText() + "\n" +  line);
        }

        bufferedReader.close();
       
       
       
    }
   
    static private void listener(){
        buttonStart.addActionListener(new ActionListener() {
           
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                try {
                    showResults();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }

}
```

Grüße Max


----------



## thet1983 (26. Apr 2017)

schau dir mal die Methode append() an, vl ist die besser geeigent.


----------



## MaxG. (28. Apr 2017)

@thet1983 Danke für die Antwort hab es gleich ausgetauscht hier der neue Code:

```
package Sonstige;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class Ping {

    static JFrame mainFrame;
    static JFrame result;

    static JPanel panelButton;
    static JPanel panelTextField;
    static JPanel panelLabel;
   
    static JLabel infoLabel;
    static JTextArea textAreaResults;
    static JTextField input;
    static JButton buttonStart;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        mainFrame = new JFrame();
        mainFrame.setSize(250, 175);
        mainFrame.setTitle("Ping Tool");
        mainFrame.setLayout(new BorderLayout());
       
        result = new JFrame();
        result.setSize(500, 350);
       
        textAreaResults = new JTextArea();
        textAreaResults.setEditable(false);
       
        result.add(textAreaResults);
        result.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       
        panelButton = new JPanel();
        panelTextField = new JPanel();
        panelLabel = new JPanel();
       
        infoLabel = new JLabel("Bitte IP Adresse eingaben");
        input = new JTextField(13);
        buttonStart = new JButton("Ping Starten");
       
        panelButton.add(buttonStart);
        panelTextField.add(input);
        panelLabel.add(infoLabel);
       
        listener();
       
        mainFrame.add(panelLabel, BorderLayout.NORTH);
        mainFrame.add(panelTextField, BorderLayout.CENTER);
        mainFrame.add(panelButton, BorderLayout.SOUTH);
        mainFrame.setVisible(true);
        mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
   
   
    static private void showResults() throws IOException{
        mainFrame.dispose();
        result.setVisible(true);
        ProcessBuilder processBuilder = new ProcessBuilder(new String[] { "ping", input.getText().trim() });
        Process process = processBuilder.start();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
            textAreaResults.append(line + "\n");
        }

        bufferedReader.close();
       
       
       
    }
   
    static private void listener(){
        buttonStart.addActionListener(new ActionListener() {
           
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                try {
                    showResults();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }

}
```


----------



## MaxG. (28. Apr 2017)

Ich hab nur noch das Problem das das "ü" immer als Fragezeichen dargestellt wird. Wie kann man das ändern, sodass es auch richtig dargestellt wird?


----------



## thet1983 (28. Apr 2017)

dem inputstreamreader könnte man ein charset mitgeben, schon mal versucht?
InputStreamReader


----------



## MaxG. (28. Apr 2017)

Ja ich ich arbeite mit einem Buffered Reader dem ich einen Input Stream gebe.

```
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
            textAreaResults.append(line + "\n");
        }
```


----------



## thet1983 (28. Apr 2017)

ja und dem kann man ein charset mit geben....

```
new InputStreamReader(process.getInputStream(), "UTF-8")
```


----------



## MaxG. (28. Apr 2017)

@thet1983 Sorry hab den Falschen Code gepostet.

```
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(),"UTF-8"));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
            textAreaResults.append(line + "\n");
        }
```

Funktioniert aber leider nicht.
Hab auch schon das Probiert:

```
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(),StandardCharsets.UTF_8));
```
Funktioniert auch nicht


----------



## Times (16. Mai 2017)

Hey, hatte vor einer Weile das selbe Problem.
Das Charset der Kommandozeile ist auch nicht UTF-8 sondern "850" (Also einfach nur "850"). So war es zumindest bei mir. Um dein richtiges herauszufinden öffne die Kommandozeile und gib ein:
chcp
Dann kommt so etwas wie:
Aktive Codepage: 850.

BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream(),Charset.forName("850").newDecoder()));


----------



## MaxG. (17. Mai 2017)

@Times Danke für die Antwort, habs gleich ausprobiert und es Funktioniert, hätte nicht mehr daran gedacht das das noch was wird. 
Vielen Dank!

hier noch der Verbesserte Code:

```
package Sonstige;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class Ping {

    static JFrame mainFrame;
    static JFrame result;

    static JPanel panelButton;
    static JPanel panelTextField;
    static JPanel panelLabel;
   
    static JLabel infoLabel;
    static JTextArea textAreaResults;
    static JTextField input;
    static JButton buttonStart;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        mainFrame = new JFrame();
        mainFrame.setSize(250, 175);
        mainFrame.setTitle("Ping Tool");
        mainFrame.setLayout(new BorderLayout());
       
        result = new JFrame();
        result.setSize(500, 350);
       
        textAreaResults = new JTextArea();
        textAreaResults.setEditable(false);
       
        result.add(textAreaResults);
        result.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       
        panelButton = new JPanel();
        panelTextField = new JPanel();
        panelLabel = new JPanel();
       
        infoLabel = new JLabel("Bitte IP Adresse eingaben");
        input = new JTextField(13);
        buttonStart = new JButton("Ping Starten");
       
        panelButton.add(buttonStart);
        panelTextField.add(input);
        panelLabel.add(infoLabel);
       
        listener();
       
        mainFrame.add(panelLabel, BorderLayout.NORTH);
        mainFrame.add(panelTextField, BorderLayout.CENTER);
        mainFrame.add(panelButton, BorderLayout.SOUTH);
        mainFrame.setVisible(true);
        mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
   
   
    static private void showResults() throws IOException{
        mainFrame.dispose();
        result.setVisible(true);
        ProcessBuilder processBuilder = new ProcessBuilder(new String[] { "ping", input.getText().trim() });
        Process process = processBuilder.start();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(),Charset.forName("850").newDecoder()));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
            textAreaResults.append(line + "\n");
        }

        bufferedReader.close();
       
       
       
    }
   
    static private void listener(){
        buttonStart.addActionListener(new ActionListener() {
           
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                try {
                    showResults();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }

}
```


----------

