I want to output all the values in a variable in the same sentence in a showMessageDialog method, I know I could use a JList to put them all in one screen, but I'd rather have something like: 3,4,2,62,12,41,5
I could do something like
for(int x = 0;x < array.length;x++){
JOptionPane.showMessageDialog(null,array[x] + ",");}
But that would take more than one screen and it's not what I want.
Also, perhaps it could be done with a JLabel instead, I'll suit myself with that if it's easier.
As shown here , use StringBuilder
to construct a line-oriented representation of your array and display it in a JScrollPane
in a JOptionPane
. The scroll pane's preferred size can be arbitrary.
Do the concatenation first:
String s = Arrays.toString(array);
s = s.substring(1,s.length-1);
JOptionPane.showMessageDialog(null,s);
The problem with this is, if the array is sufficiently large, then it will not fit on the screen anyway. Presenting it in a messagebox may not be usefull to the user.
If you need to show this in a manageable way for arbitrary large arrays, then I would rather write a small dialog class, which is not much work, and then use a scrollable textarea instead.
This way you can prepare it in any way you want to. However, from your short sample it's not entirely clear if you always have a small number of items, in which case creating the string should be sufficient.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.