2

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);
    }
}
elwynn
  • 237
  • 2
  • 5
  • 14

7 Answers7

1

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

tddmonkey
  • 20,798
  • 10
  • 58
  • 67
1

Use a JTextArea and call setEditable(false).

https://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/text/JTextComponent.html#setEditable(boolean)

fospathi
  • 537
  • 1
  • 6
  • 7
JeeBee
  • 17,476
  • 5
  • 50
  • 60
1

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

Joonas Pulakka
  • 36,252
  • 29
  • 106
  • 169
1

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
  • 391,730
  • 64
  • 469
  • 606
1

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
  • 342,105
  • 78
  • 482
  • 720
0

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

jtextarea.setEditable(false);
Torsten Uhlmann
  • 645
  • 5
  • 13
0

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.

How do I create a Java string from the contents of a file?

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

Community
  • 1
  • 1
OscarRyz
  • 196,001
  • 113
  • 385
  • 569