0

I have a text file with a list of names in. I am trying to create a GUI and then read in the text from the file into the GUI and display it in a textfield/label/anything. I can create the GUI and read in the code, but do not know how to display the read in text in the GUI. Below is my code. When I run it displays the GUI but does not display the read in text.

public class ASSIGNMENT {

    private JLabel lbl1;
    private JTextField txt1;
    private JPanel panel;
    private JFrame frame;

    public ASSIGNMENT(){
        createGUI();
        addLabels();

        frame.add(panel); 
        frame.setVisible(true);
    }

    public void createGUI(){ 
        frame = new JFrame();
        frame.setTitle("Books");
        frame.setSize(730, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel();
        panel.setLayout(null);
        panel.setBounds(10, 10, 10, 10);
        panel.setBorder(BorderFactory.createLineBorder (Color.decode("#1854A2"), 2));
        frame.add(panel);
    }

    public void addLabels(){
        lbl1 = new JLabel(" ");
        lbl1.setBounds(700, 450, 120, 25);
        lbl1.setForeground(Color.white);
        panel.add(lbl1);
    }

    public void books() throws IOException{
        String result = "books2.txt";
        String line;
        LineNumberReader lnr = new LineNumberReader(new FileReader(new File("books2.txt")));
        while((line = lnr.readLine()) != null){
            result += line;
        }
        JLabel label1 = new JLabel(result);
        panel.add(label1);
    }


    public static void main(String[] args) throws Exception{
        new ASSIGNMENT();

    }
}
admix
  • 1,752
  • 3
  • 22
  • 27
Sophie
  • 29
  • 1
  • 7
  • I see you have written a `books()` method to read the file and place its contents in a JLabel. But that method won’t execute unless you actually call it from somewhere in your code. – VGR Jul 20 '16 at 18:46

1 Answers1

1

Hello here is your code working. You basically need to set a Layout Manager properly. You have two options. Option one is to have NULL and layout manager. In this case you need to position all your components with setBounds().

Option number two is to use a more user friendly layout manager that does not require that like GridBagLayout. Bellow you can see your code corrected for GridBagLayout. I repeat having null as manager is possible, but you need to position your elements with coordinates with the help of setBounds

public class ASSIGNMENT3 {

    private JLabel lbl1;
    private JTextField txt1;
    private JPanel panel;
    private JFrame frame;

    public ASSIGNMENT3() throws IOException{
        createGUI();
        addLabels();
        books();

        frame.add(panel); 
        frame.setVisible(true);
    }

    public void createGUI(){ 
        frame = new JFrame();
        frame.setTitle("Books");
        frame.setSize(730, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        panel.setBounds(10, 10, 10, 10);
        panel.setBorder(BorderFactory.createLineBorder (Color.decode("#1854A2"), 2));
        frame.add(panel);
    }

    public void addLabels(){
        lbl1 = new JLabel("Labe 1 ");
        lbl1.setBounds(700, 450, 120, 25);
        lbl1.setForeground(Color.white);
        panel.add(lbl1);
    }

    public void books() throws IOException{
        String result = "books2.txt";
        String line;
//        LineNumberReader lnr = new LineNumberReader(new FileReader(new File("books2.txt")));
//        while((line = lnr.readLine()) != null){
//            result += line;
//        }
//        
        txt1 = new JTextField(20);
        txt1.setText(result);
        JLabel label1 = new JLabel(result);
        panel.add(label1);
        panel.add(txt1);
    }


    public static void main(String[] args) throws Exception{
        new ASSIGNMENT3();

    }
}
Alexander Petrov
  • 9,204
  • 31
  • 70
  • Thank you it worked. Do you know how I can make it so each new line from the file is on a new line in the GUI? – Sophie Jul 21 '16 at 16:23
  • As @NathanProgrammer mentioned already you need a JTextArea if you want to have text that spans over many lines. JTextField is juts a JTextField. – Alexander Petrov Jul 21 '16 at 16:26
  • I have used a JTextArea instead of a JTextField, but am unsure of how to make it display the next line from the file on a new line in the Text Area – Sophie Jul 21 '16 at 17:46
  • There you go http://stackoverflow.com/questions/2088016/add-a-new-line-to-the-end-of-a-jtextarea – Alexander Petrov Jul 21 '16 at 17:49