954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Print my switch to a dialog box

I am trying to print this to a dialog box. The code prints to console and prints, 1,2,3,4....12 to the dialog box. Can someone give me some advice on printing to a dialog box the verses to the dialog?

import javax.swing.*; // program uses class JOptionPane

public class TwelveDays
{
// main method begins execution of Java application
public static void main( String args[] )
{

int day = 1;
String verse = "";

for (day = 1; day <= 12; day++) {

JOptionPane.showMessageDialog(null, verse);


switch(day)
{
case 1:
System.out.println ("On the first day of Christmas my true love sent to me:" );

case 2:
System.out.println ("On the second day of Christmas my true love sent to me:");

case 3:
System.out.println ("On the thrid day of Christmas my true love sent to me:");
break;
case 4:
System.out.println ("On the fourth day of Christmas my true love sent to me:");
break;
case 5:
System.out.println ("On the fifth day of Christmas my true love sent to me:");
break;
case 6:
System.out.println ("On the sixth day of Christmas my true love sent to me:");
break;
case 7:
System.out.println ("On the seventh day of Christmas my true love sent to me:");
break;
case 8:
System.out.println ("On the eigth day of Christmas my true love sent to me:");
break;
case 9:
System.out.println ("On the ninth day of Christmas my true love sent to me:");
break;
case 10:
System.out.println ("On the tenth day of Christmas my true love sent to me:");
break;
case 11:
System.out.println ("On the eleventh day of Christmas my true love sent to me:");
break;
case 12:
System.out.println ("On the twelveth day of Christmas my true love sent to me:");
break;

}


switch(day)
{
case 12:
verse += "12 Drummers Drumming";
case 11:
verse += "Eleven Pipers Piping";
case 10:
verse += "Ten Lords a Leaping";
case 9:
verse += "Nine Ladies Dancing";
case 8:
verse += "Eight Maids a Milking";
case 7:
verse += "Seven Swans a Swimming";
case 6:
verse += "Six Geese a Laying";
case 5:
verse += "Five Golden Rings";
case 4:
verse += "Four Calling Birds";
case 3:
verse += "Three French Hens";
case 2:
verse += "Two Turtle Doves";
case 1:
verse += "a Partridge in a Pear Tree";
break;

}

}

} // end method main

} // end class twelveDays

atrix415
Newbie Poster
5 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

to print to a dialog have a class extending the JDialog Class e.g

public MyClass extends JDialog{
private JTextArea display = new JTextArea(70,70);
public MyClass(){
..............................................
add(display);
...................................
}
}

A JTextArea object would be hand for it allows one to append text as follows

JTextArea display = new JTextArea(40,40);
display.append( sometext );

pjade
Newbie Poster
6 posts since Jun 2007
Reputation Points: 10
Solved Threads: 2
 

I am using Verse+= part to help me append to a dialog box with Joption.pane. But I cannot get the verse in.

atrix415
Newbie Poster
5 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

you missing break after most cases in your switch statement

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

He wants that, because the song goes

On the twelveth Day of Christmas
My true love gave to me
12 Drmmers Drumming
11 Pipers Piping
10 .....
masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

@OP

Make a Dialog with a Label (or JTextPane would probably be better), then in the first switch you want to use component.setText("string") . Then, everyplace else you should add text, you want to use component.setText(component.getText() + "string") .

Give that a try, then, if you have problems, come back and post that code, and we will see if we can help you further.

Also, don't forget to add a "\n" to the end of each of those Strings (although I don't believe a Label honors those, but a "
" would work in a Label). Then, maybe, add a short sleep after each text block (if you want to introduce a little pause between each line for effect).

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You