Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8857061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:30:07+00:00 2026-06-14T14:30:07+00:00

I have the bellow code which call data from MySQL and return in a

  • 0

I have the bellow code which call data from MySQL and return in a XYPlot (JFreeChart) on a JPanel. There are different datasets, depending of the row index of my JTable. The problem is when I select a row, the chart seems to be good, but when I select another row, the XYplot appear but when I make a click (drag, second mouse-click, etc.) the chart returns to the first one. Practically every time when I select another plot, and I make something with the mouse (zooming, etc.), it returns to the primary/first xyplot selection.
I tried JPanel.validate(); but still not working, also repaint(); method. Furthermore, with JFrame it is working very good, no problem (yes! right, because opens every time one frame with the specific XYPlot). I suppose that the problem is from JPanel. Any help/advice? Please!

UPDATE: The JPanel with the XYPlot, is encapsulated in an JSplitPanel. When I move the divider (enlarge JPanel with the XYPlot), there are two XYPlot, the first one (obtained on the first click, and the second one (if was clicked second time to another row/dataset -> XYPlot). I suppose that the problem is from query -> and repainting over the previous XYPlot.

private void PrimaryTableMouseClicked(java.awt.event.MouseEvent evt) {                                          
        int row = PrimaryTable.getSelectedRow();
        int realIndex = PrimaryTable.convertRowIndexToModel(row);
        String Table_click = (PrimaryTable.getModel().getValueAt(realIndex, 0).toString());

        try {

            String query = "select wavenumber,spectrum FROM test where id_test ='" + Table_click + "'";
            pst = conn.prepareStatement(query);
            rs = pst.executeQuery();
            if (rs.next()) {

                JDBCXYDataset dataset = new JDBCXYDataset(ConnecrDb(), query);

                JFreeChart chart = ChartFactory.createXYLineChart(
                        "title",
                        "cm",
                        "in",
                        dataset,
                        PlotOrientation.VERTICAL,
                        false, //legend
                        true, //tooltip
                        false); //urls

                XYPlot plot = (XYPlot) chart.getPlot();

                ChartPanel CP = new ChartPanel(chart);
                PanelWithChart.setLayout(new BorderLayout());
                PanelWithChart.add(CP, BorderLayout.CENTER);
                PanelWithChart.repaint();
            }
             rs.close();
            pst.close();


        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:30:08+00:00Added an answer on June 14, 2026 at 2:30 pm

    I think that this may be your problem

    PanelWithChart.setLayout(new BorderLayout());
    PanelWithChart.add(CP, BorderLayout.CENTER);
    

    By calling JPanel#add() you are adding a new component each time PrimaryTableMouseClicked is called. If your ChartPanel is the only component on PanelWithChart try removing the previous chart:

    PanelWithChart.setLayout(new BorderLayout());
    PanelWithChart.removeAll();
    PanelWithChart.add(CP, BorderLayout.CENTER);
    

    This will not work if you have other components on the panel, if this is the case then you will need to use PanelWithChart.remove(Component comp) but this is harder as you do not have a referance to the previous chart.

    Please not that this is probably not the best way the use a chart, have you considered creating the chart once and making the dataset a propery? You could then use this code in PrimaryTableMouseClicked to achieve much the effect.

    dataset.removeAllSeries();
    XYSeries newSeries = new XYSeries("New Data");
    dataset.addSeries(newSeries);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page which is fetching data from a webservice using async call.
Below I have some code which should return an array, the response from the
I have below snippet of code in which TestClass is extending jPanel which is
I have below code which overrides equals() and hashcode() methods. public boolean equals(Object obj)
I want to know is below code correct ? I have following code which
I have the code below which I use clone() and live() . The code
I have the code below which I use clone() and delegate() . The code
I have the below code, which iterates over a list based on a custom
I have the below code for my a Dialog box for a which contains
I have the following code below which works, but I want to insert values

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.