开发者

Selectable alternative to JOptionPane.showMessageDialog

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);
}开发者_StackOverflow
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?


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! :-)


Add the dates to a DefaultListModel, create a JList, and pass the list to showMessageDialog(). It's more than one line, but the selection copies to the clipboard using the platform's copy keystroke.

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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜