Selectable Alternative JOptionPane.showMessageDialog

Backgorund Information :

I have a friend in the navy, and he wanted to know if I could pick it up with a small application that would calculate when he had the guard, because, apparently, it was difficult to count on a calendar. I used JOptionPane.showMessageDialog to give it the output. This is how I do it.

 GregorianCalendar knownDate = new GregorianCalendar(year,month,day); GregorianCalendar[] futureDates = new GregorianCalendar[10]; for(int i = 0; i < 10; i++) { futureDates[i] = new GregorianCalendar(year,month,day); futureDates[i].add(Calendar.DAY_OF_MONTH,10*(i+1)); // duty every 10 days } String newline = System.getProperty("line.separator"); StringBuilder sb = new StringBuilder("Jakes duty dates:").append(newline); for(GregorianCalendar d : futureDates) { sb.append(months[d.get(Calendar.MONTH)]).append(" "); sb.append(d.get(Calendar.DAY_OF_MONTH)).append(newline); } JOptionPane.showMessageDialog(null,sb.toString()); 

"The only problem" is that you cannot select the text that is displayed. He would like to choose him for instant messaging and e-mail, because the point is that he was only half lazy, right? (Only the problem is in quotation marks, because I have a feeling that he will cover it to death ... haha)

My question is :

Is there a one-line solution for choosing showMessageDialog ?

+6
java swing
source share
2 answers

I was able to build an answer to trashgod. Although he suggested using JList , I use JTextArea instead (which gives me the choice I need).

That's what I'm doing:

 JTextArea text = new JTextArea(sb.toString()); JOptionPane.showMessageDialog(null,text); 

And it works like a charm!

================================================= =

After a little experiment, I did this:

 DefaultListModel model = new DefaultListModel(); for(GregorianCalendar g : futureDates) { String m = months[g.get(Calendar.MONTH)]; String d = String.valueOf(g.get(Calendar.DAY_OF_MONTH)); model.addElement(m + " " + d); } JList jlist = new JList(model); JOptionPane.showMessageDialog(null,jlist); JOptionPane.showMessageDialog(null,jlist.getSelectedValue()); 

And the second field showed what I chose on the first. I was very impressed with this. Now provided, this is not the functionality I was going to (the upper part), but that does not make it less attractive !:-)

+5
source share

Add dates to DefaultListModel , create a JList and pass the list to showMessageDialog() . This is more than one line, but the selection is copied to the clipboard by pressing the copy platform key.

 private static final DateFormat df = new SimpleDateFormat("dd-MMM"); private static void createAndShowGUI() { DefaultListModel dlm = new DefaultListModel(); for (int i = 0; i < 10; i++) { GregorianCalendar knownDate = new GregorianCalendar(); knownDate.add(Calendar.DAY_OF_MONTH, 10 * i); dlm.add(i, df.format(knownDate.getTime())); } JList list = new JList(dlm); JOptionPane.showMessageDialog(null, list); } 
+1
source share

All Articles