Reputation: 129
i'm developing an application in java to "monitor" trains interruptions.
In fact, i'm reading log.txt files and showing them into a JTabbedPane with differents panes which correspond to differents cities.
There is a main GUI with a JPane and i'm inserting a JTabbedPane in this using a Thread, and in this JTabbedPane, i'm inserting panes using threads. I'm doing this way because i want to update panes (with log files infos) every minutes without blocking all the GUI. Is this the right way to do a monitoring app?
My problem is when i have created all tabs i wanted, i can't update them, when i try to update one, it's always the last one that is updated.
Here is my code : Creating new JTabbedPane
Controller.Controller_ListeOnglets n = new Controller.Controller_ListeOnglets(View.View_Application.getJPanel());
n.start();
Here is the way i add tab in JTabbedPane
public class Controller_ListeOnglets extends Thread {
JPanel jo = new JPanel();
public Controller_ListeOnglets(JPanel j) {
jo = j;
}
@Override
public void run() {
View.View_ListeOnglets t = new View.View_ListeOnglets();
jo.add(t);
ArrayList<String> list = new ArrayList<>();
list = Controller_Modification.ChercherSystemes();
for (String i : list) {
try {
Controller_Onglet nouveau = new Controller_Onglet(View.View_ListeOnglets.getJTabbedPane(), Controller.Controller_BDD.GetSystemeID(i));
nouveau.start();
this.sleep(100);
} catch (InterruptedException | SQLException | ClassNotFoundException ex) {
Logger.getLogger(Controller_ListeOnglets.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public class Controller_Onglet extends Thread {
Model.Model_Systeme systemeB;
static JTabbedPane jo = new JTabbedPane();
public Controller_Onglet(JTabbedPane j, Model.Model_Systeme systemeA) {
jo = j;
systemeB = systemeA;
}
@Override
public void run() {
View.View_Onglet onglet = new View.View_Onglet(systemeB);
if (jo.getTabCount() == 0) {
jo.add(onglet);
jo.setTitleAt(jo.getTabCount() - 1, systemeB.getSysteme_Nom());
jo.setBackgroundAt(0, Color.CYAN);
} else {
jo.add(onglet, jo.getTabCount());
jo.setTitleAt(jo.getTabCount() - 1, systemeB.getSysteme_Nom());
jo.setBackgroundAt(jo.getTabCount() - 1, Color.CYAN);
}
/* Timer t = new Timer(5000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {*/
if (EventQueue.isDispatchThread()) {
Controller_TraitementFichier traitement = new Controller_TraitementFichier(jo, systemeB.getSysteme_Nom());
traitement.start();
} else {
Controller_TraitementFichier traitement = new Controller_TraitementFichier(jo, systemeB.getSysteme_Nom());
traitement.start();
}
/*}
});
t.start();*/
}
//A Compléter
public static void removeOnglet(Model.Model_Systeme systeme) {
for (int i = 0; i < jo.getTabCount(); i++) {
if (jo.getTitleAt(i).equals(systeme.getSysteme_Nom())) {
jo.remove(i);
}
}
}
And this is an example of how i'm trying to update a Tab
public class Controller_TraitementFichier extends Thread {
String title = null;
JTabbedPane jo = null;
public Controller_TraitementFichier(JTabbedPane j, String titleOnglet) {
title = titleOnglet;
jo = j;
}
@Override
public void run() {
InputStream flux = null;
try {
flux = new FileInputStream("./src/Ressources/Archivage_LISATEL/12203005.TXT");
InputStreamReader lecture = new InputStreamReader(flux);
BufferedReader buff = new BufferedReader(lecture);
String ligne = buff.readLine();
try {
while (ligne != null) {
View_Onglet onglet = View_ListeOnglets.getJTabbedPane(title);
if (title.equals(title)) {
View.View_ListeOnglets.getJTabbedPane().setSelectedIndex(0);
onglet.changeTexteArea1(ligne + "\n");
jo.setBackgroundAt(View.View_ListeOnglets.getJTabbedPane().getTabPlacement(), Color.red);
}
ligne = buff.readLine();
}
System.out.println("Traitement " + title);
buff.close();
lecture.close();
flux.close();
} catch (IOException ex) {
Logger.getLogger(Controller_TraitementFichier.class.getName()).log(Level.SEVERE, null, ex);
}
//System.out.println(View.View_ListeOnglets.getJTabbedPane().getTitleAt(View_ListeOnglets.getJTabbedPane().getSelectedIndex()));
} catch (IOException ex) {
Logger.getLogger(Controller_TraitementFichier.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
flux.close();
} catch (IOException ex) {
Logger.getLogger(Controller_TraitementFichier.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
it's not really clear so if you want to help me more, i'll send you my code.
Thanks
Upvotes: 1
Views: 664
Reputation: 11327
All Swing changes must be performed in AWT Event Thread. To make your code correct you should implement all your Swing updates in a separate Runnable object and call these updates using SwingUtilities.invokeLater(Runnable);
Something like this:
public class DataCollector implements Runnable {
public void run() {
// collect data
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// do swing update
}
});
}
}
Other possibility is using of SwingWorker class.
Upvotes: 1
Reputation: 205785
Use SwingWorker
, seen here, to query data in the background and update the GUI on the event dispatch thread. Each tab's content can have it's own worker thread.
Upvotes: 1