1

I have a code ready that generates a CPU/Time chart (I changed another code according to my need). So far so good, but now I can't integrate it in a JFrame that is really mine and already stylized!

Here is the code of the chart:

/** @see https://stackoverflow.com/a/5048863/230513 */
public class Atol extends ApplicationFrame {

private static final String TITLE = "Dynamic Series";
private static final String START = "Start";
private static final String STOP = "Stop";
private static final float MINMAX = 100;
private static final int COUNT = 2 * 60;
private static final int FAST = 100;
private static final int SLOW = FAST * 5;
private static final Random random = new Random();
private Timer timer;

public Atol(final String title) {
    super(title);
    final DynamicTimeSeriesCollection dataset
            = new DynamicTimeSeriesCollection(1, COUNT, new Second());
    dataset.setTimeBase(new Second(0, 0, 0, 1, 1, 2011));
    dataset.addSeries(gaussianData(), 0, "Gaussian data");
    JFreeChart chart = createChart(dataset);

    final JButton run = new JButton(STOP);
    run.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            String cmd = e.getActionCommand();
            if (STOP.equals(cmd)) {
                timer.stop();
                run.setText(START);
            } else {
                timer.start();
                run.setText(STOP);
            }
        }
    });

    final JComboBox combo = new JComboBox();
    combo.addItem("Fast");
    combo.addItem("Slow");
    combo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if ("Fast".equals(combo.getSelectedItem())) {
                timer.setDelay(FAST);
            } else {
                timer.setDelay(SLOW);
            }
        }
    });
   this.add(new ChartPanel(chart), BorderLayout.CENTER);
   JPanel btnPanel = new JPanel(new FlowLayout());
   btnPanel.add(run);
   btnPanel.add(combo);
   this.add(btnPanel, BorderLayout.SOUTH);
    SystemInfo si = new SystemInfo();             //Criando uma nova classe de infos do Sistem
    HardwareAbstractionLayer hal = si.getHardware(); //Infos de Hardware do sistema
    CentralProcessor cpu = hal.getProcessor();      //E as informações da cpu
    long[] oldTricks = cpu.getSystemCpuLoadTicks();

    
    timer = new Timer(FAST, new ActionListener() {
        float cpu() {

            Double stats = cpu.getSystemCpuLoadBetweenTicks(oldTricks);
            //Convertendo o valor de uso da CPU
            stats = stats * 100d;
            double teste = Math.round(stats * 100.0) / 100.0;
            double d = teste;
            float f = (float) d;
            System.out.println(f);
            return f;
        }
        float[] newData = new float[1];

        @Override
        public void actionPerformed(ActionEvent e) {

            newData[0] = cpu();
            dataset.advanceTime();
            dataset.appendData(newData);
        }
    });
}


private float[] gaussianData() {

    float[] a = new float[COUNT];
    for (int i = 0; i < a.length; i++) {
        a[i] = 2;
    }
    return a;
}

private JFreeChart createChart(final XYDataset dataset) {
    final JFreeChart result = ChartFactory.createTimeSeriesChart(
            TITLE, "hh:mm:ss", "milliVolts", dataset, true, true, false);
    final XYPlot plot = result.getXYPlot();
    ValueAxis domain = plot.getDomainAxis();
    domain.setAutoRange(true);
    ValueAxis range = plot.getRangeAxis();
    range.setRange(-MINMAX, MINMAX);
    return result;
}

public void start() {
    timer.start();
}

public static void main(final String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            Atol demo = new Atol(TITLE);
            demo.pack();

            demo.setVisible(true);
            demo.start();
        }
    });
}}

And this is my code for the button that should bring up a jpanel with the chart:

private void kButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here: GRAFICO DE CPU
cardLayout.show(pnlCards, "cpu");
chartcpu.add(Atol);}  

It changes the JPanel Card just right but I can't insert it in this JPanel called chart (this "chart" is a other jpanel and is inside the Card panel), it opens a new window! And he needs to stay inside this "chart" JPanel

enter image description here

How can i invoke the Atol class chart in my JPanel chartcpu which is in another class?

[EDIT] It worked but now how can i take all these decorations and leave only the chart? Chart appeared

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
BaaDe
  • 77
  • 1
  • 9
  • `public class Atol extends ApplicationFrame {` This already seems like 'code going wrong'. What is an `ApplicationFrame`? Is this your custom class? What is it about an application frame that could not be achieved using a factory method? Same question for `Atol` class. **BTW:** for better help sooner, post a [mre]. I had to trawl through that code to confirm it was using JFreeChart - quite relevant to the question (and tags). – Andrew Thompson Dec 08 '20 at 14:10
  • Sorry, I just reused that [code](https://stackoverflow.com/questions/5048852/using-jfreechart-to-display-recent-changes-in-a-time-series) and I dont really know how it works(?) – BaaDe Dec 08 '20 at 14:17

1 Answers1

1

If I understand it correctly, you want to show a frame inside another frame right? If this is the case, you could use JInternalFrame

// Define your main frame
JFrame mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLayout(null);
mainFrame.setSize(800, 800);
mainFrame.setVisible(true);


// Define your subFrame
JInternalFrame subFrame = new JInternalFrame();
subFrame.setLocation(200, 200);
subFrame.setSize(250, 250);        
subFrame.setClosable(false);
subFrame.setResizable(false); 
subFrame.setVisible(true);


// Add your subFrame to mainFrame and repaint
mainFrame.add(subFrame);
mainFrame.repaint();

Removing the borders of InternalFrame

subFrame.setBorder(null);        
BasicInternalFrameUI bFrame = (BasicInternalFrameUI)subFrame.getUI();
bFrame.setNorthPane(null);
tataelm
  • 679
  • 8
  • 17
  • Yes it is! I did it like that: Atol demo = new Atol (""); chartcpu.add(demo); chartcpu.repaint(); But it return me an error: Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding a window to a container – BaaDe Dec 08 '20 at 14:07
  • The error is self explanatory. **adding a window to a container**. A container cannot have any types of objects in it. Type must be 'containable'. You try to add 'Atol' to your chartcpu(is this pure JFrame?). Try making 'Atol' "extends JInternalFrame" instead "extends ApplicationFrame" – tataelm Dec 08 '20 at 14:27
  • The error stopped but nothing appears on my jpanel :( – BaaDe Dec 08 '20 at 14:35
  • Did you set Atol.setVisible(true); ?? – tataelm Dec 08 '20 at 14:40
  • It worked! Thank you so much! Can you help me with my new problem? How can I take off these panel behind and leave only the chart? Check my edit please – BaaDe Dec 08 '20 at 14:51
  • I've edited my answer also. Check that please. – tataelm Dec 08 '20 at 15:01
  • It work but when i click in "Start" the border comebacks – BaaDe Dec 08 '20 at 17:04
  • it Worked now i took off all buttons! Thank you so much!! – BaaDe Dec 08 '20 at 18:42
  • I like this approach; more [here](https://stackoverflow.com/a/65206207/230513). – trashgod Dec 08 '20 at 20:16