How to display a text file using Java Swing

I stored my files using .txt format. At first read the files using a BufferedReader from the text file. Read each line one by one and then store them in an object array since this what you will need to pass it to the JTable constructor.

My Jtable would have just one column.
Each row of my text file would be places in a row of the jtable.
appointmentTable would be the name of my Jtable. I would pass the object array to my jtable as below:
appointmentTable.setModel(new javax.swing.table.DefaultTableModel(
map, new String [] { Appointment } ));
where appointment would be the name of my column.

Always remember to close the reader, otherwise you would have problems getting out the data to your object array. Its that easy.

try {

Object[][] map = new Object[1000][1000];
BufferedReader in = new BufferedReader(new FileReader(C:bankOfDarshanappointment.txt));
String subStr;

int i=0;
while ((subStr = in.readLine()) != null) {
map[i][0] = subStr;
i++;
}
in.close();

appointmentTable.setModel(new javax.swing.table.DefaultTableModel(
map, new String [] { Appointment } ));
} catch (IOException ex) {
Logger.getLogger(PostItView.class.getName()).log(Level.SEVERE, null, ex);
}

In

Leave a Reply

Your email address will not be published. Required fields are marked *