0

Hi Friends I have a DefaultPieDataset that am Creating this way:

package business.intelligence.system;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JInternalFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.data.jdbc.JDBCPieDataset;


public class DepositBase extends JInternalFrame {

private JPanel jpAcc = new JPanel();
private JList checkBoxesJList;
private JLabel lScheme;
private String schm = "";

DepositBase() throws SQLException, ClassNotFoundException {
    super("Deposit base", false, true, false, true);
    setSize(1300, 600);
    jpAcc.setLayout(null);
    jpAcc.setBackground(Color.LIGHT_GRAY);
    JScrollPane scrollPane = new JScrollPane(checkBoxesJList);
    lScheme = new JLabel("<html><u>SCHEME CODE</u></html>");
    lScheme.setBounds(10, 5, 100, 25);
    lScheme.setForeground(Color.BLACK);
    Connection conn = null;
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.80:1521:simba9i", "SYSTEM", "system123");
        Statement st = conn.createStatement();
        String combo = "select distinct SCHM_CODE from DLY_DEP_VIEW";
        ResultSet res = st.executeQuery(combo);
        String ids = "";
        ArrayList<String> v = new ArrayList<>();
        v.add("All");
        while (res.next()) {
            ids = res.getString("SCHM_CODE");
            v.add(ids);
            checkBoxesJList = new JList(createData(v));

        }



        checkBoxesJList.setBounds(10, 30, 80, 600);
        checkBoxesJList.setBackground(Color.LIGHT_GRAY);
        checkBoxesJList.setCellRenderer(new CheckListRenderer());
        checkBoxesJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    } catch (Exception as) {
    }


    checkBoxesJList.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent evt) {
            if (evt.getValueIsAdjusting()) {
                return;
            }
            Object[] temp = checkBoxesJList.getSelectedValues();
            for (int i = 0; i < temp.length; i++) {
                // System.out.println(temp[i]);
                Connection conn = null;
                schm = temp[i].toString();
               // System.out.println(schm);
                try {

                    createDataset(schm);
                } catch (Exception ae) {
                }


            }
        }
    });

    checkBoxesJList.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            int index = checkBoxesJList.locationToIndex(e.getPoint());
            CheckableItem item = (CheckableItem) checkBoxesJList.getModel().getElementAt(index);
            item.setSelected(!item.isSelected());
            Rectangle rect = checkBoxesJList.getCellBounds(index, index);
            checkBoxesJList.repaint(rect);
        }
    });



    final PieDataset dataset = createDataset(schm);
    final JFreeChart chart = createChart(dataset);
    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new Dimension(900, 900));
    chartPanel.setBounds(550, 30, 700, 500);
    // setContentPane(chartPanel);
    jpAcc.add(chartPanel);
    jpAcc.add(checkBoxesJList);
    jpAcc.add(scrollPane);
    jpAcc.add(lScheme);


    getContentPane().add(jpAcc);
    setVisible(true);

}

private PieDataset createDataset(String schm) throws SQLException, ClassNotFoundException {


    // create the dataset...
    Connection conn = null;
    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.80:1521:simba9i", "SYSTEM", "system123");
    final DefaultPieDataset dataset = new JDBCPieDataset(conn,
            "select distinct SCHM_CODE, sum(DEP_AMT) as AMOUNT from DLY_DEP_VIEW  where schm_code = '" + schm + "' group by schm_code");
    System.out.println(schm);
    try {
    } catch (Exception ad) {
        JOptionPane.showMessageDialog(this, ad,
                "Error", JOptionPane.ERROR_MESSAGE);
    }

    return dataset;

}

private CheckableItem[] createData(ArrayList<String> strs) {
    int n = strs.size();
    CheckableItem[] items = new CheckableItem[n];
    for (int i = 0; i < n; i++) {
        items[i] = new CheckableItem(strs.get(i));
    }
    return items;
}

private JFreeChart createChart(final PieDataset dataset) {

    // create the chart...
    final JFreeChart chart = ChartFactory.createPieChart3D(
            "Deposit Base", dataset, true, true, false);



    // set the background color for the chart...
    chart.setBackgroundPaint(Color.white);

    // get a reference to the plot for further customisation...
    final PiePlot3D plot = (PiePlot3D) chart.getPlot();     
    plot.setLabelGenerator(null);
    //plot.setLabelGenerator(new StandardPieSectionLabelGenerator(" {2}", NumberFormat.getNumberInstance(), NumberFormat.getPercentInstance()));

    plot.setForegroundAlpha(1f);
    plot.setNoDataMessage("No data to display");
    return chart;


}
}

class CheckableItem {

private String str;
private boolean isSelected;

public CheckableItem(String str) {
    this.str = str;
    isSelected = false;
}

public void setSelected(boolean b) {
    isSelected = b;
}

public boolean isSelected() {
    return isSelected;
}

@Override
public String toString() {
    return str;
}
}

class CheckListRenderer extends JCheckBox implements ListCellRenderer {

public CheckListRenderer() {
    setBackground(UIManager.getColor("List.textBackground"));
    setForeground(UIManager.getColor("List.textForeground"));
}

@Override
public Component getListCellRendererComponent(JList list, Object value,
        int index, boolean isSelected, boolean hasFocus) {
    setEnabled(list.isEnabled());
    setSelected(((CheckableItem) value).isSelected());
    setFont(list.getFont());
    setText(value.toString());
    return this;
}
}

I need my chart to change depending on the JList component I CLick But I do not see any change. What Should I do. I am a beginner. I will appreciate links to example and examples.

Stanley Mungai
  • 4,044
  • 30
  • 100
  • 168
  • You are creating a `DataSet` but don't do anything with it in you example, do you do this elsewhere in the code? – GrahamA Nov 27 '12 at 11:15
  • @GrahamA I did not Post the Entire Code I am using the Dataset this way: ` final PieDataset dataset = createDataset(); final JFreeChart chart = createChart(dataset); final ChartPanel chartPanel = new ChartPanel(chart);` – Stanley Mungai Nov 27 '12 at 11:39
  • Doesn't anyone Know this >.please help – Stanley Mungai Nov 28 '12 at 09:30
  • I've answered your question, I think that the problem is more related to your lack of experience preventing you from understanding it. The code in your `ListSelectionListener` is not doing anything useful as you call `createDataset(schm)` but don't do anything with the result. Consider adding a [SSCCE](http://sscce.org) that doesn't require a database and you may get more help but without all you code its hard to help you. – GrahamA Nov 28 '12 at 10:28
  • I agree I have great lack of experience I am a beginner Both in Java An Jfreecharts.. I have edited My code there is the Full Code – Stanley Mungai Nov 28 '12 at 11:02
  • I'm afraid there are multiple problems with this code including setting the layout manager to null (`jpAcc.setLayout(null);`) but my original solution is still correct, each time your `ListSelectionListener` fires you do nothing with the dataset you create in `createDataset()`. Its time to take a step back and rework you code, start by getting the Internal Frame and ListBox working – GrahamA Nov 28 '12 at 12:03
  • @GrahamA.. System.out.println(schm); prints the values of the Checkbox clicked , So I think The listbox is working, and LLike I had state sir I did not understnad your first Solution. – Stanley Mungai Nov 28 '12 at 12:25
  • Cross-posted [here](http://www.jfree.org/forum/viewtopic.php?f=3&t=116055). – trashgod Nov 28 '12 at 14:53

1 Answers1

2

Your code is not working because although the code inside ListSelectionListener executes createDataset() it does nothing with it, the dateset you created when you first called createDataset() is unrealted/not updated.

Rather than creating a new DefaultPieDataset each time your list box changes create the dataset once (the first time the method is called say) and on subsequent chages just call JDBCPieDataset#executeQuery() with your new query, this will execute the new query and then callfireDatasetChanged().

Make you Dataset a property

private JDBCPieDataset dataset; private Connection conn;

Modify your Listener)

  scheamaList.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent evt) {
    try {
      dataset.executeQuery( (String) scheamaList.getSelectedItem());
    } catch (Exception ae) {
      ae.printStackTrace();
     }
  }
  });

So that the dataset is refreshed:

enter image description here

GrahamA
  • 5,875
  • 29
  • 39
  • @Stanley: This should work; don't forget to put you chart in a `ChartPanel`. – trashgod Nov 27 '12 at 20:01
  • @trashgod Problem I i don not Understand because Every time I click on the list The query value changes so tha.t the pie chat have different data – Stanley Mungai Nov 28 '12 at 07:47
  • Guys I think An example will help me or....Is there really a way to do what am trying to do or Not so I can get done with it and look for other ways? – Stanley Mungai Nov 28 '12 at 12:27
  • A Jlist will not Take AcctionListener Directly and getSelected Values – Stanley Mungai Nov 28 '12 at 14:08
  • @Stanley In my example I'm using a `JComboBox` which does. The important bit is how I'm using `dataset.executeQuery()` rather then creating a new Dataset (and then ignoring it) as you where. – GrahamA Nov 28 '12 at 14:44