2

So I have been starting to work on an encyclopedia as a project for college and I'm coming over an issue. I am trying to import a .txt file to print my text to my text panel but the problem is, I can't print paragraphs and it only prints one line. My textpanel code is the following:

import java.io.*;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTextArea;


public class GTAIntro extends JPanel {
private static final long serialVersionUID = 1L;

public GTAIntro() {
    setSize(800,800);
    setLayout(null);

    JButton button_1 = new JButton("");
    button_1.setIcon(new ImageIcon("D:\\Facultate\\anul 2\\Java Workspace\\Encyclopedia\\src\\icons\\back.png"));
    button_1.setEnabled(false);
    button_1.setBounds(10, 700, 50, 50);
    add(button_1);

    JButton button = new JButton("");
    button.setIcon(new ImageIcon("D:\\Facultate\\anul 2\\Java Workspace\\Encyclopedia\\src\\icons\\next.png"));
    button.setBounds(740, 700, 50, 50);
    add(button);

    JLabel rockstarlogo = new JLabel("");
    rockstarlogo.setIcon(new ImageIcon("D:\\Facultate\\anul 2\\Java Workspace\\Encyclopedia\\src\\icons\\gta\\rockstar-logo.png"));
    rockstarlogo.setBounds(10, 11, 150, 150);
    add(rockstarlogo);

    JTextArea textpanel = new JTextArea();
    textpanel.setLineWrap(true);
    textpanel.setWrapStyleWord(true);
    textpanel.setEditable(false);
    try{
        FileInputStream fstream = new FileInputStream("D:\\Facultate\\anul 2\\Java Workspace\\Encyclopedia\\src\\text\\gta\\gtaintro.txt");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        while((strLine = br.readLine()) != null) {
            textpanel.setText(strLine);
        }
    in.close();
    }catch(Exception e){System.err.println("Error: " + e.getMessage());}
    textpanel.setBounds(100, 222, 593, 528);
    add(textpanel);

}
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
MrSilent
  • 564
  • 10
  • 28
  • Neither Eclipse nor WindowBuilder have *anything* To do with this problem! BTW - Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Dec 13 '13 at 16:28

2 Answers2

3

Use JEditorPane.read(InputStream,Object) - much easier. Perhaps something like this:

import java.io.*;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTextArea;


public class GTAIntro extends JPanel {
private static final long serialVersionUID = 1L;

public GTAIntro() {
    setSize(800,800);
    setLayout(null);

    JTextArea textpanel = new JTextArea();
    textpanel.setLineWrap(true);
    textpanel.setWrapStyleWord(true);
    textpanel.setEditable(false);
    try{
        FileInputStream fstream = new FileInputStream("D:\\Facultate\\anul 2\\Java Workspace\\Encyclopedia\\src\\text\\gta\\gtaintro.txt");
        // do not use DataInputStream to read text
        // DataInputStream in = new DataInputStream(fstream);
        Reader reader = new InputStreamReader(fstream);
        textpanel.read(reader, fstream);
    }catch(Exception e){System.err.println("Error: " + e.getMessage());}
    textpanel.setBounds(100, 222, 593, 528);
    add(textpanel);

    JButton button_1 = new JButton("");
    button_1.setIcon(new ImageIcon("D:\\Facultate\\anul 2\\Java Workspace\\Encyclopedia\\src\\icons\\back.png"));
    button_1.setEnabled(false);
    button_1.setBounds(10, 700, 50, 50);
    add(button_1);

    JButton button = new JButton("");
    button.setIcon(new ImageIcon("D:\\Facultate\\anul 2\\Java Workspace\\Encyclopedia\\src\\icons\\next.png"));
    button.setBounds(740, 700, 50, 50);
    add(button);

    JLabel rockstarlogo = new JLabel("");
    rockstarlogo.setIcon(new ImageIcon("D:\\Facultate\\anul 2\\Java Workspace\\Encyclopedia\\src\\icons\\gta\\rockstar-logo.png"));
    rockstarlogo.setBounds(10, 11, 150, 150);
    add(rockstarlogo);

}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • The method read(Reader, Object) in the type JTextComponent is not applicable for the arguments (DataInputStream, FileInputStream) – MrSilent Dec 13 '13 at 16:38
  • My bad. I forgot to mention earlier. For better help sooner, post an [SSCCE](http://sscce.org/). That code I posted was not tested locally here. Post an SSCCE of your best attempt, and I'll correct it. – Andrew Thompson Dec 13 '13 at 16:42
  • I did post my code, this is only a part of my program as the other part is too big to post here. – MrSilent Dec 13 '13 at 16:52
  • Try the code in the edit. Note that it compiles cleanly, but since it has no `main(..)` and I have no text files here, I could not actually confirm it 'works as advertised'. – Andrew Thompson Dec 13 '13 at 16:59
  • Note the code in the edit still has many problems, starting with the layouts. I chose to alter *only* the code relevant to the question. – Andrew Thompson Dec 13 '13 at 17:01
  • It's working, thank you. What problems are you talking about? – MrSilent Dec 13 '13 at 17:03
  • Glad you got it sorted. :) – Andrew Thompson Dec 13 '13 at 17:04
  • Can you elaborate please? What problems are you talking about? – MrSilent Dec 13 '13 at 17:15
  • 1) The first was mentioned in a comment to the question. 2) Using hard coded paths to files, for what will likely be embedded-resources by the time of deployment. 3) Ignoring the stack trace (more useful than the message) in the `catch`.. – Andrew Thompson Dec 13 '13 at 17:20
0
String newLine = System.getProperty("line.separator");    
textpanel.setText(strLine + newLine);
Anirban Nag 'tintinmj'
  • 5,572
  • 6
  • 39
  • 59
  • 3
    This is simply a statement that doesn't actually serve to answer the original poster's question. I suggest you read over [**how to answer**](http://stackoverflow.com/questions/how-to-answer) and expand it. – brandonscript Dec 13 '13 at 16:46