views:

590

answers:

7

G'day all,

I have an application which needs to display an ASCII file of 15 lines in a Swing component that cannot be edited by the user.

Below is my code which reads the ASCII file byte by byte. My thanks to the helpful commenters who explained that a JTextArea.setEditable(false) would be appropriate in Swing.

However, my code merely displays a string of numbers, when I personally made the ASCII file to be something quite different. Does anyone know what I am doing wrong and how to get the ASCII characters themselves to display?

import java.io.*;
import javax.swing.*;

public class LoadMap extends JFrame {
    public LoadMap() throws IOException {
        FileInputStream fIn = new FileInputStream("map.srn");
        BufferedReader rd = new BufferedReader(new InputStreamReader(fIn, "US-ASCII"));
        String map = "";
        JTextArea mapArea = new JTextArea(15, 50);

        try {
            int c;
            while ((c = rd.read()) != -1) {
                map= map + c;
            }
        } finally {
            if (rd != null) {
                rd.close();
            }
        }
        mapArea.setText(map);
        mapArea.setEditable(false);
        add(mapArea);
        pack();
        setVisible(true);
    }
}
+1  A: 

Use a JTextArea and call

setEditable(false);

to stop the user being able to edit the data

I've just read your code through and realise that's not a comprehensive enough answer. You can't do "+" on a label. What you need to do is read the text in first and store it somewhere, then call

setText(yourTextAsString);

on your text component on screen (for which I'd still use the JTextArea), and you need to add the text area to the frame, so your code would look something like:

public LoadMap() {
     String data = // read your data
     JTextArea textArea = new JTextArea();
     textArea.setText(data);
     textArea.setEditable(false);
     setLayout(new GridLayout());
     add(textArea);
     pack();
     setVisible(true);
}

I would suggest reading the Swing tutorial to get some more info on using Swing components

MrWiggles
You beat me by 10 seconds with practically the same wording!
JeeBee
Thank you very much.
elwynn
+1  A: 

Use a JTextArea and call setEditable(false).

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/text/JTextComponent.html#setEditable(boolean)

JeeBee
Thank you very much. I appreciate MrWiggles was first, but you were a close second!
elwynn
+1  A: 

You could construct a String inside the loop, and then put the string into JLabel when you've finished.

Joonas Pulakka
+1  A: 

This makes me feel like Captain Obvious, but still: just load the entire text first, and then build the label using the desired text. Or, as other posters rightly suggest, use a JTextArea since its more well-suited for multiline content.

unwind
A: 

You could use a JTextArea and set it to read only:

jtextarea.setEditable(false);
Torsten Uhlmann
+1  A: 

A JLabel will not display line breaks (unless you use HTML). So as the others wrote, use a text area.

However, there's another hidden problem with your code: you don't specify the file's encoding, which means the file contents may be garbled if it contains non-ASCII characters and its encoding does not match Java's platform default. To fix this, do not use FileReader. Instead, use a FileInputStream and wrap it in an InputStreamReader, specifying the encoding in its constructor.

Michael Borgwardt
Thanks Michael, that's useful to know.
elwynn
A: 

Here:

String map = "";
int c;
while ((c = rd.read()) != -1) {
    map= map + c;
}

What you're doing it appending int's to the string.

You should cast them to char instead.

int c;
while ((c = rd.read()) != -1) {
    map= map + ( char ) c;
}

You can see much better patterns in these questions.

http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file

http://stackoverflow.com/questions/181634/simplest-efficient-ways-to-read-binary-and-ascii-files-to-string-or-similar-in-v/324792#324792 ( see java part )

OscarRyz