简体   繁体   中英

Enable text highlighting in swing message-box

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.

try this

 JTextArea textarea= new JTextArea("add your message here");
 textarea.setEditable(true);
 JOptionPane.showMessageDialog(null, textarea, "Error", JOptionPane.ERROR_MESSAGE);

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM