The problem I have is when I press the "back" button, the program opens up the correct GUI but doesn't get rid of the "LiftAnalysis" GUI. I have almost no experience with JFreeChart or using TimeSeriesCharts. Please suggest a method to hide the current GUI when the back button is pressed. Code is below
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.Axis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class LiftAnalysis extends JFrame {
private static final long serialVersionUID = 1L;
public LiftAnalysis(){
//super(title);
// Create dataset
XYDataset dataset = createDataset();
// Create chart
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Lift Analysis", // Chart
"Date", // X-Axis Label
"Number", // Y-Axis Label
dataset);
//Changes background color
//XYPlot plot = (XYPlot)chart.getPlot();
Plot plot = chart.getPlot();
plot.setBackgroundPaint(new Color(255,228,196));
chart.setBackgroundPaint(Color.DARK_GRAY);
ChartPanel panel = new ChartPanel(chart);
setContentPane(panel);
panel.setLayout(null);
if (plot instanceof CategoryPlot) {
setAxisFontColor(((CategoryPlot) plot).getDomainAxis(), Color.white);
setAxisFontColor(((CategoryPlot) plot).getRangeAxis(), Color.white);
} else if (plot instanceof XYPlot) {
setAxisFontColor(((XYPlot) plot).getDomainAxis(), Color.white);
setAxisFontColor(((XYPlot) plot).getRangeAxis(), Color.white);
}
ImageIcon helfIcon = new ImageIcon("C:\\Users\\joshu\\Desktop\\School\\Computer Science\\logo_small_icon_only_inverted.png");
Image helfImage = helfIcon.getImage();
Image modifiedHelfImage = helfImage.getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH);
helfIcon = new ImageIcon(modifiedHelfImage);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setIcon(helfIcon);
lblNewLabel.setBounds(736, -14, 62, 69);
panel.add(lblNewLabel);
JButton btnBack = new JButton("Back");
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
AnalysisPage p10 = new AnalysisPage();
panel.setVisible(false);
p10.run(); //Opens a different GUI
}
});
btnBack.setBackground(Color.GRAY);
btnBack.setFont(new Font("Tahoma", Font.BOLD, 18));
btnBack.setBounds(43, 318, 120, 34);
panel.add(btnBack);
}
private XYDataset createDataset() {
TimeSeriesCollection dataset = new TimeSeriesCollection();
TimeSeries series1 = new TimeSeries("Bench");
series1.add(new Day(15, 12, 2021), 675);
series1.add(new Day(18, 12, 2021), 675);
series1.add(new Day(21, 12, 2021), 702);
series1.add(new Day(28, 12, 2021), 720);
series1.add(new Day(03, 01, 2022), 738);
dataset.addSeries(series1);
TimeSeries series2 = new TimeSeries("Squat");
series2.add(new Day(16, 12, 2021), 1750);
series2.add(new Day(19, 12, 2021), 1800);
series2.add(new Day(22, 12, 2021), 1850);
series2.add(new Day(29, 12, 2021), 1850);
series2.add(new Day(04, 01, 2022), 1900);
dataset.addSeries(series2);
TimeSeries series3 = new TimeSeries("Deadlift");
series3.add(new Day(17, 12, 2021), 1425);
series3.add(new Day(20, 12, 2021), 1455);
series3.add(new Day(23, 12, 2021), 1500);
series3.add(new Day(30, 12, 2021), 1575);
series3.add(new Day(05, 01, 2022), 1650);
dataset.addSeries(series3);
return dataset;
}
private void setAxisFontColor(Axis axis, Color fontColor) {
if (!fontColor.equals(axis.getLabelPaint()))
axis.setLabelPaint(fontColor);
if (!fontColor.equals(axis.getTickLabelPaint()))
axis.setTickLabelPaint(fontColor);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
LiftAnalysis example = new LiftAnalysis();
example.setSize(800, 400);
example.setLocationRelativeTo(null);
example.setVisible(true);
example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
});
}
public void run() {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(() -> {
LiftAnalysis example = new LiftAnalysis();
example.setSize(800, 400);
example.setLocationRelativeTo(null);
example.setVisible(true);
example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
});
}
}