Calling class in Java after editing file used in as source for table
- by user2892290
I'm currently working on a project, I'll try to subrscibe first. I save data into text file, that I use as a source for browser of that data. The browser is based on table that contains the data. I have to rewrite the source file everytime I delete or edit data. That's where the problem comes in. After deleting or editing data I call a method to create the table again, but the table never creates. Is it possibly made by editing the file and calling the method right after that? If I restart my app the table is successfully created with right data. Take in note that I don't get any error message.
This is the method I use for loading data from source file:
try (BufferedReader input1 = new BufferedReader(new FileReader("./src/data.src"))) {
int lines = 0;
while (input1.read() != -1) {
if (!(input1.readLine()).equals("")) {
lines++;
}
}
input1.close();
if (lines == 0) {
JOptionPane.showMessageDialog(null, "No data to load, create a note first!");
new Writer().build(frame);
} else {
try (BufferedReader input = new BufferedReader(new FileReader("./src/data.src"))) {
Game[] g = new Game[lines];
String currentLine;
String[] help;
int counter = 0;
while (lines > 0) {
currentLine = input.readLine();
help = currentLine.split("#");
g[counter] = new Game(help[0],help[1], help[2], help[3], help[4], help[5], help[6], help[7], help[8], help[9]);
counter++;
lines--;
}
input.close();
final JButton bButton = new backButton().create(frame, mPanel);
build(g, frame, bButton);
mPanel.add(panel);
mPanel.add(panel2);
mPanel.add(searchPanel);
mPanel.add(bButton);
bButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
panel.removeAll();
frame.setCursor(Cursor.getDefaultCursor());
}
});
mPanel.setPreferredSize(new Dimension(1000, 750));
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
frame.setLayout(new FlowLayout());
frame.add(mPanel);
frame.pack();
JMenuBar menuBar = new Menu().create(frame, mPanel);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
Rectangle rec = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
int width = (int) rec.getWidth();
int height = (int) rec.getHeight();
frame.setBounds(1, 3, width, height);
frame.addComponentListener(new ComponentAdapter() {
@Override
public void componentMoved(ComponentEvent e) {
frame.setLocation(1, 3);
}
});
And this is the method I use for creating the table:
String[][] tableData = new String[g.length][9];
for (int i = 0; i < tableData.length; i++) {
tableData[i][0] = g[i].getChampion();
tableData[i][1] = g[i].getRole();
tableData[i][2] = g[i].getEnemy();
tableData[i][3] = g[i].getDifficulty();
tableData[i][4] = g[i].getResult();
tableData[i][5] = g[i].getScore();
tableData[i][6] = g[i].getGameType();
tableData[i][7] = g[i].getPoints();
tableData[i][8] = g[i].getLeague();
}
final JLabel searchLabel = new JLabel("Search for champion played.");
final JButton searchButton = new JButton("Search");
final JTextField searchText = new JTextField(20);
frame.setTitle("LoL Notepad - reading your notes");
JTable table = new JTable(tableData, columnNames);
final JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(980, 500));
panel2.setPreferredSize(new Dimension(1000, 550));
panel2.setVisible(false);
panel2.setBorder(new EmptyBorder(10, 10, 10, 10));
panel3.setVisible(false);
panel.setLayout(new FlowLayout());
panel.add(scrollPane);
searchPanel.add(searchLabel);
searchPanel.add(searchText);
searchPanel.add(searchButton);
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
search(g, searchText.getText(), frame, bButton);
frame.setCursor(Cursor.getDefaultCursor());
} catch (IOException ex) {
Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
table.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getClickCount() == 1) {
JTable target = (JTable) e.getSource();
panel.setVisible(false);
searchPanel.setVisible(false);
bButton.setVisible(false);
int row = target.getSelectedRow();
specific(row, g, frame, bButton);
}
}
});