0

I'm writing a program which will read, record, and plot modbus values on a time series. I have everything figured out aside from the plotting part. My issue lies with the GUI. I am trying to set up the GUI such that I have several tabbed panes, with a plotted chart on the inside of a tabbed pane with a "Plot" button to trigger the actual plotting method. I am using JFreePlot.

My current code for the program is shown below, but it's not really complete, so to be precise I am just wondering as to how I can place a JFreeChart in a "space" in a tabbed pane in a JFrame, activated by a button within the same tabbed pane as the chart.

private class SwingAction_5 extends AbstractAction {
    public SwingAction_5() {
        putValue(NAME, "SwingAction_5");
        putValue(SHORT_DESCRIPTION, "Some short description");
    }
    public void actionPerformed(ActionEvent e) {
        try{
        JFrame frame = new JFrame();
        FileDialog fd = new FileDialog(frame,"Choose a file",FileDialog.LOAD);
        fd.setDirectory("C:\\");
        fd.setFile("*.txt");
        fd.setVisible(true);
        String f_name = fd.getFile();
        BufferedReader reader = new BufferedReader(new FileReader(f_name));
        int lines = 0;
        while (reader.readLine() != null) {lines++;}
        reader.close();
        FileInputStream fis = new FileInputStream(f_name);
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
        String line=null;
        Float[] f_arr = new Float[lines];
        for(int i=0;i<lines;i++){
            f_arr[i] = Float.parseFloat(br.readLine());
        }

        br.close();
        final XYSeries series = new XYSeries("Data");
        for (int i = 0; i < f_arr[0]*f_arr[1]; i++) {
            series.add(i, f_arr[i+2]);
        }
        XYSeriesCollection dataset = new XYSeriesCollection(series);
        JFreeChart chart = ChartFactory.createXYLineChart("Amperage Deviation", "Seconds","Amps", dataset, PlotOrientation.VERTICAL, false, false, false);
        JPanel p = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        final JTabbedPane jtp = new JTabbedPane();
        jtp.add(jtp, BorderLayout.CENTER);

        frame.add(p,BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        }
        catch(Exception mbi){ 
            mbi.printStackTrace();
            System.exit(1);
        }

    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
zzzzzzzch
  • 59
  • 2
  • 9
  • 1
    Put the button in one of the non-center constraints of a `BorderLayout` in a `JPanel`. Set the `JPanel` as the view of the tab. In the center of the `BorderLayout` panel, put another `JPanel` that uses a `CardLayout`. On button click, add the chart to the `CardLayout` & show the card. For better/more help, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). For the example, replace the chart with a `JPanel` with a red background. If you can achieve it with a colored panel, it should be the same basic code as for the chart. – Andrew Thompson Jun 14 '16 at 14:01
  • 1
    For [example](http://stackoverflow.com/a/15715096/230513). – trashgod Jun 15 '16 at 02:18

0 Answers0