I have no experience with JOptionpane
but I need a simple program to simplify my life.
The code I need help with is below:
public static void main (String[] args) {
String input = "";
input = JOptionPane.showInputDialog("Enter code");
JOptionPane.showMessageDialog(null, toStringU(toArray(input)), "RESULT",
JOptionPane.INFORMATION_MESSAGE);
}
toStringU
method gives me a long long text
I want to run it without any compiler (a standalone application, double click, put info and take results).
And I cant copy the result from output panel, which I need to copy. So either I need to copy it and/or I want to write it to a txt file (second one would be great).
JOptionPane
allows you to specify an Object
as the message parameter, if this value is a Component
of some kind, it will be added to the JOptionPane
(String
s are rendered using JLabel
automatically)
By setting up something like a JTextArea
that is not editable, you can take advantage of its copying capabilities, with out much more work on your part...
import java.awt.EventQueue;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestOptionPane11 {
public static void main(String[] args) {
new TestOptionPane11();
}
public TestOptionPane11() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JTextArea ta = new JTextArea(10, 10);
ta.setText("This is some really long text that is likely to run over "
+ "the viewable area and require some additional space to render "
+ "properly, but you should be able to select and copy parts or "
+ "whole sections of the text as you, but it should remain "
+ "no editable...");
ta.setWrapStyleWord(true);
ta.setLineWrap(true);
ta.setCaretPosition(0);
ta.setEditable(false);
JOptionPane.showMessageDialog(null, new JScrollPane(ta), "RESULT", JOptionPane.INFORMATION_MESSAGE);
}
});
}
}
Additional side effects
Another side effect is the fact that the JTextArea
can actually write it's contents to a Writer
for you...
FileWriter writer = null;
try {
writer = new FileWriter("SomeoutputFile.txt", false);
ta.write(writer);
} catch (IOException exp) {
exp.printStackTrace();
} finally {
try {
writer.close();
} catch (Exception e) {
}
}
Which allows you to write a file as well...