Backgorund info:
I have a buddy of mine in the Navy, and he wanted to know if I could whip him up a small app that would calcualte when he has his guard duty, because apparently counting on a calendar is hard. I used JOptionPane.showMessageDialog
to give him the output of the dates. Here's how I'm doing that.
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 you can't select the text that is displayed. He'd like to select it for IM and email, because what's the point in only being half lazy, right? (Only problem is in quotes because I have a feeling he'll scope creep this to death... haha)
My question:
Is there a "one-line solution" to making a selectable showMessageDialog
?
The JOptionPane displays the dialog boxes with one of the four standard icons (question, information, warning, and error) or the custom icons specified by the user.
It is used to create an information-message dialog titled "Message". static void showMessageDialog(Component parentComponent, Object message, String title, int messageType) It is used to create a message dialog with given title and messageType.
What package must be imported to use JOptionPane methods? The JOptionPane Class displays a dialog for user information and interaction it is part of the javax. swing package and must be imported before you can use it.
I was able to build on trashgod's answer. While he suggested using a JList
, I'm instead using a JTextArea (which gives the kind of selection I need.)
Here's what I'm doing:
JTextArea text = new JTextArea(sb.toString());
JOptionPane.showMessageDialog(null,text);
And it's working like a charm!
================================================
After a little experimentation 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 box displayed what I had selected on the first one. I was really impressed with that. Now granted, this isn't the functionality I was going for (the top section is) but that doesn't make it any less awesome! :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With