(Hopefully) Quick question, I've written out some code to display information from a text file in a JOptionPane, it works, but it's creating a new box for each line.
How would I make it display all read text in a single JOptionPane instead of box by box, line by line. Without having to rewrite the whole thing, if possible.
Scanner inFile = null;
try {
inFile = new Scanner(new FileReader( curDir + "/scripts/sysinfo.txt"));
} catch (FileNotFoundException ex) {
Logger.getLogger(DropDownTest.class.getName()).log(Level.SEVERE, null, ex);
}
while(inFile.hasNextLine()){
String line = inFile.nextLine();
JOptionPane.showMessageDialog(null, line, "System Information", JOptionPane.INFORMATION_MESSAGE);
}
I've looked around for similar questions but couldn't find anything to quite fit this and decided to just ask, but I'll apologize if I've somehow missed something and this ends up as a duplicate.
Thanks for any help.
You could try using a StringBuilder to create a single large string, then display this StringBuilder like so:
Scanner inFile = null;
StringBuilder builder = new StringBuilder();
try {
inFile = new Scanner(new FileReader(curDir + "/scripts/sysinfo.txt"));
} catch (FileNotFoundException ex) {
Logger.getLogger(DropDownTest.class.getName()).log(Level.SEVERE, null, ex);
}
while(inFile.hasNextLine()){
String line = inFile.nextLine();
builder.append(line);
builder.append("\n"); // add this for newlines
}
JOptionPane.showMessageDialog(null, builder, "System Information", JOptionPane.INFORMATION_MESSAGE);