I was writing an applet before and was displaying information in a JOptionPane. Useing \n, I was able to make the information display multilined like this:

Name1
Grade1
Name2
Grade2
etc.

I unfortunately had to change my code from an applet to a JFrame and subsiquently my Labels to JLabels. Most of my code remains the same. However, ow when I display the information in the JOptionPane, it appears like this:

Name1 Grade1 Name2 Grade2 etc.

How do I make a multiline JLabel to print out in a JOptionPane?

Recommended Answers

All 7 Replies

<html>Name1<br>
Grade1<br>
Name2<br>
Grade2</html>

Thanks but I am not dealing with HTML. I think I thought of a solution. I will use a textArea but make it un-editable.

Thanks but I am not dealing with HTML.

You may have missed the point here - JLabels automatically support most basic HTML items, so you use <BR> instead of \n in a JLabel

I don't think it will work because of the way I am doing it. I am reading from a text file into an arraylist and then adding it to a JLabel. My code looks like this:

stop=CompSciAP.size();
            index=0;
            while (index<stop)
            {
                APLabel.setText(APLabel.getText()+CompSciAP.get(index)+"\n");
                index++;
            }

The \n does not do anything. I am not familiar with html at all so would I make the line look like this?

APLabel.setText(<html>APLabel.getText()+CompSciAP.get(index)<br></html>);

I still think I will try the text area thing I said before.

Wait, I found a solution. Even though I am making a JFrame as opposed to an applet, I use a Label instead of a JLabel. I tested this and it prints out the way I want it to.

I am not familiar with html at all so would I make the line look like this?

APLabel.setText(<html>APLabel.getText()+CompSciAP.get(index)<br></html>);

Yup, almost. Just build up a String (or StringBuffer) starting with "<html>, then, in your read loop, append each line followed by a "<br>" then finally append a "</html>". This really is the "right" way to do this in Java.
See http://java.sun.com/docs/books/tutorial/uiswing/components/html.html

alright thanks! thats useful information!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.