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();
}
}