Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update/refresh JPanel on JButton click?

I am trying to plot a graph and graph should display when JButton is clicked. To create data set, I am taking some value through JTextField and then created a chart and plotted it. I've got 2 problems:

  1. The chart becomes visible after resizing the window and
  2. the chart doesn't refresh when I change the text field value.

Here is my program:

public class Test extends JFrame {

private JPanel contentPane;
private JTextField textField;
private ChartPanel chartPanel;

public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.WEST);
panel.setLayout(new MigLayout("", "[][grow]", "[][][][][]"));

JLabel lblA = new JLabel("a");
panel.add(lblA, "cell 0 2,alignx trailing");

textField = new JTextField();
panel.add(textField, "cell 1 2,growx");
textField.setColumns(10);

JButton btn = new JButton("Plot");
btn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        double a= Double.parseDouble(textField.getText());
        chartPanel = createChart(a);
        contentPane.add(chartPanel, BorderLayout.CENTER);  
    }
});
panel.add(btn, "cell 1 4");
}

// Create ChartPanel
private ChartPanel createChart(double a) {

XYDataset dataset = createDataset(a);
JFreeChart chart = ChartFactory.createXYLineChart("title", "X", "Y",
    dataset, PlotOrientation.VERTICAL, true, true, false);
return new ChartPanel(chart);
}

// Create Dataset
private XYDataset createDataset(double a) {
int x[] = { 1, 2, 3, 4, 5 };
int y[] = { 1, 4, 9, 16, 25 };

XYSeries s1 = new XYSeries("test");
for (int i = 0; i < y.length; i++) {
    s1.add(a*x[i], y[i]);
}

XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(s1);
return dataset;
}

//Main Mathed: skipped.

}
like image 559
Ranjeet Avatar asked Apr 13 '15 18:04

Ranjeet


People also ask

Can you add ActionListener to JPanel?

First off as @Sage mention in his comment a JPanel is rather a container than a component which do action. So you can't attach an ActionListener to a JPanel .

Can you add JButton to JFrame?

In a Java JFrame, we can add an instance of a JButton class, which creates a button on the frame as follows in the code below: //add a button. JButton b = new JButton("Submit");

How do I add a new line to a JPanel?

Make a separate JPanel for each line, and set the dimensions to fit each word: JLabel wordlabel = new JLabel("Word"); JPanel word1 = new JPanel(); word1. setPreferredSize(new Dimension(#,#); This should work for each word.


1 Answers

When you add a new Component to a Container that is currently showing, you must revalidate and repaint the Container

btn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        double a= Double.parseDouble(textField.getText());
        chartPanel = createChart(a);
        contentPane.add(chartPanel, BorderLayout.CENTER);
        //You have added a new component. The contentPane will be invalid, and needs repainting
        contentPane.validate();
        contentPane.repaint();
    }
});
like image 128
ControlAltDel Avatar answered Sep 27 '22 18:09

ControlAltDel