Graph and data storage in the same singleton, it is an error ??
I need to create a GUI to show data coming from a satellite receiver
through the usb ( used as a virtual com port with library rxtx)
I already created an architecture like this :
package it.imt.edusat.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class JTabbedTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
(new JTabbedTest()).create();
}
});
}
private void create() {
JTabbedPane telemetryGroupsTabs = new JTabbedPane();
telemetryGroupsTabs.addTab("One", new TelemetryGroup());
telemetryGroupsTabs.addTab("Two", new TelemetryGroup());
JFrame f = new JFrame("JTabbedPaneTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(telemetryGroupsTabs);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class TelemetryGroup extends JPanel {
public TelemetryGroup() {
this.setLayout(new BorderLayout());
View view = new View();
this.add(new View(), BorderLayout.CENTER);
this.add(new Control(view), BorderLayout.WEST);
}
}
class Control extends JPanel {
private static final Random random = new Random();
public Control(ActionListener listener) {
JButton change = new JButton("Change");
add(change);
change.addActionListener(listener);
}
}
class View extends JPanel implements ActionListener {
private static final Random random = new Random();
public View() {
setPreferredSize(new Dimension(100, 100));
setBackground(new Color(random.nextInt()));
}
public void actionPerformed(ActionEvent e) {
setBackground(new Color(random.nextInt()));
repaint();
}
}
where View ia a Singleton Class, it stores data coming from the serial
and also shows them using JfreeChart, I choose the singleton because I
think it is not necessary to create a graph for any tab of the
JTabbedPane, I could reuse the same graph, it seems to be not so, the
result is that I can see just the graph for the latest created View
and I cannot see it in the other tabs.
DO you think this is an architecture error or what ??
Thanks
Antonio
ww.etantonio.it/en