user983246
user983246

Reputation: 1449

JOptionPane MessageDialog to display data of an array in java

I'm working on a program that displays a MessageDialog which shows data of an array I created. Each line for example:

11327|933393|2 is inside element 0 of an array.
11833|938393|1 is inside element 1 of an array.

For example pretend the numbers below are inside the MessageDialog:

11327|933393|2
11833|938393|1
11934|483393|7

My only problem is that I can only display each element of the array one by one per MessageDialog. but I want to display all 3 elements inside one single MessageDialog.

Any hints or tips of how I can display my entire array inside one MessageDialog? :)

Upvotes: 1

Views: 6210

Answers (1)

trashgod
trashgod

Reputation: 205785

You can place arbitrary components in your dialog, as shown in this example. A JList or JTable would seem to be a good choice.

Addendum: Here's a simple example using JList.

enter image description here

import java.awt.EventQueue;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/** @see https://stackoverflow.com/questions/7781781 */
public class OptionList {

    private void display() {
        String[] items = {
            "11327|933393|2", "11833|938393|1", "11934|483393|7"
        };
        JList list = new JList(items);
        JPanel panel = new JPanel();
        panel.add(list);
        JOptionPane.showMessageDialog(null, panel);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new OptionList().display();
            }
        });
    }
}

Upvotes: 6

Related Questions