8

Say i'm using the following code to prompt an error message in my simple swing application:

JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);

Is there any way I could make it possible for the user to highlight text sections (for copy/paste purpose)?

Many thanks.

sgrossman
  • 105
  • 2
  • 7
  • What "sections" would you like to allow them to be able to highlight? The message? And what is held by the message variable? – Hovercraft Full Of Eels Jan 06 '13 at 14:56
  • I meant sections of the message string that is held by the message variable – sgrossman Jan 06 '13 at 15:01
  • 1
    *"an error message in my simple swing application:"* If the app. were simple (for the *user*) it would probably not show any part of the error message as produced by the J2SE, and instead have a `Submit Bug Report` button which will either copy/paste the lot of it into an email, or do something more intelligent (e.g. send it directly back to your server). – Andrew Thompson Jan 06 '13 at 15:13

3 Answers3

7

try this

 JTextArea textarea= new JTextArea("add your message here");
 textarea.setEditable(true);
 JOptionPane.showMessageDialog(null, textarea, "Error", JOptionPane.ERROR_MESSAGE);
sgrossman
  • 105
  • 2
  • 7
Alya'a Gamal
  • 5,624
  • 19
  • 34
2

JOptionPane can be constructed with any object, not just a string message. So you could construct a JTextArea and pass that to the JOptionPane as your message. That should allow copy paste.

ditkin
  • 6,774
  • 1
  • 35
  • 37
2

If you object to the white background shown by the default JTextArea, you can set the JTextArea background color equal to the JOptionPane's background color.

String title = "foo";
String message = "Select me";

JTextArea msg = new JTextArea(message);
JOptionPane pane = new JOptionPane(msg, JOptionPane.INFORMATION_MESSAGE);
msg.setBackground(pane.getBackground());
JDialog dialog = pane.createDialog(null, title);
dialog.setVisible(true);