Samit Paudel
Samit Paudel

Reputation: 181

How to print all the array elements in a JOptionPane?

I have an array which I want to print on a JOptionPane to get a result like this:

enter image description here

with the array being the book list. I don't know how to

Following is the code for the array:

String[] booksOfSubGenre = new String[bookInfo.size()];
        for (int i = 0; i < bookInfo.size(); i++) {
            int j = 0;
            if (bookInfo.get(i).getSubGenre().equals(selectedSubGenreInCombobox)) {
                subGenreCount++;
                System.out.println(subGenreCount);
                booksOfSubGenre[j] = bookInfo.get(i).getBookName();
            }
        }

Upvotes: 0

Views: 658

Answers (3)

Abra
Abra

Reputation: 20914

The type of the second parameter, named message, in all the showMessageDialog methods of class javax.swing.JOptionPane is Object which means it can be any class, including Swing components like javax.swing.JList.

Consider the following.

import java.awt.BorderLayout;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class OptsList {

    public static void main(String[] args) {
        String[] booksOfSubGenre = new String[]{"The Lost Hero",
                                                "The Hobbit",
                                                "A Game of Thrones"};
        int count = booksOfSubGenre.length;
        String subgenre = "Fantasy";
        JPanel panel = new JPanel(new BorderLayout(10, 10));
        String text = String.format("There are %d books of Sub-Genre %s", count, subgenre);
        JLabel label = new JLabel(text);
        panel.add(label, BorderLayout.PAGE_START);
        JList<String> list = new JList<>(booksOfSubGenre);
        panel.add(list, BorderLayout.CENTER);
        JOptionPane.showMessageDialog(null, panel, "Query Result", JOptionPane.INFORMATION_MESSAGE);
    }
}

Running the above code produces the following:

running app

Upvotes: 1

caslawter
caslawter

Reputation: 672

To display in that format, you can use String.format() to create a string template and pass in the data.

String text = String.format("There are %o books of Sub-Genre Fantasy: %s", subGenreCount , bookInfo.get(i).getBookName());
JOptionPane.showMessageDialog(null, text, "Query Result", JOptionPane.INFORMATION_MESSAGE);

To display multiple books, you can append the book names together and display it as one string.

    String appendedText = "";
    int subGenreCount = 0;
    String[] booksOfSubGenre = new String[bookInfo.size()];
    for (int i = 0; i < bookInfo.size(); i++) {
        int j = 0;
        if (bookInfo.get(i).getSubGenre().equals(selectedSubGenreInCombobox)) {
            subGenreCount++;
            System.out.println(subGenreCount);
            booksOfSubGenre[j] = bookInfo.get(i).getBookName();
            appendedText += bookInfo.get(i).getBookName() + "\n";
        }
    }
    String text = String.format("There are %o books of Sub-Genre Fantasy: %s", subGenreCount , appendedText);
    JOptionPane.showMessageDialog(null, text, "Query Result", JOptionPane.INFORMATION_MESSAGE);

Upvotes: 1

camickr
camickr

Reputation: 324128

Format your text output using HTML.

So your text string might look like:

String text = "<html>There are two books of Sub-Genre Fantasy:<br>The Lost Hero<br>Another Book>";

Upvotes: 1

Related Questions