Question

I have a char[xSize][ySize] being sent to my GUI. The GUI prints the long string as one long line without recognizing the new lines in it. What method or code could I use? The code I currently use is:

            JFrame frame1 = new JFrame("Start Screen");
            JLabel label = new JLabel();
            frame1.setVisible(true);
            frame1.setSize(500, 500);
            frame1.setLayout(new BorderLayout());
            frame1.add(label, BorderLayout.NORTH);

            //Code to recieve from other program:

            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            String Sentence = new String(receivePacket.getData());  

            //Print received text to GUI:
            label.setText(Sentence);

Thanks in advance

Was it helpful?

Solution

For something JLabel, you would need to first replace the new lines (\n) with <br> and wrap the text in <html>...</html>

String Sentence = new String(receivePacket.getData());  

//Print received text to GUI:
label.setText("<html>" + Sentence.replaceAll("\n", "<br>") + "</html>");

Or you might be able to use a non-editable JTextArea instead...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top