1

I need to take values that I find from inside a for loop to use in a function that isn't inside the loop, and I can't figure out how to do this. What I am hoping to accomplish is to extract values from a key in a hashmap to then plot in a JTable only if that row is selected (using a ListSelectionListener). This way, I can avoid graphing a hundred tables which would save a lot of time. Also I am using DefaultTableModel.

This is my for loop:

tableMaker(model);
for(Map.Entry<String,NumberHolder> entry : entries)
{   
  //add rows for each entry of the hashmap to the table             

  double[] v = new double[entry.getValue().singleValues.size()];
  int i = 0;
  for(Long j : entry.getValue().singleValues)
  {
    v[i++] = j.doubleValue();
  }                  
  //right here I need to take "v" 
  //and use it in my tableMaker function for that specific row
}                  

In my tableMaker function, I create my JTable and add a ListSelectionListener, where I hope to create a histogram when a row is selected and add it to my lowerPanel. This is some of the code. I need v so that I can create the dataSet.

public static void tableMaker(DefaultTableModel m)
{
   JPanel lowerPanel = new JPanel();

   //create table here

   frame.getContentPane().add(lowerPanel, BorderLayout.SOUTH);
   table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    table.getSelectionModel().addListSelectionListener(
            new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    JFreeChart chart = ChartFactory.createHistogram(
                        plotTitle, xaxis, yaxis, dataset, orientation,
                        show, toolTips, urls);                      
                //  lowerPanel.add(chart);
                //  lowerPanel.revalidate();
                }
            }
        });
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
user2007843
  • 609
  • 1
  • 12
  • 30
  • 1
    I don't understand the problem. If you need some `double[] v`, with your method `tableMaker(DefaultTableModel);`, change your method to also accept a `double[]` argument: `tableMaker(DefaultTableModel m, double[] v)` – Sotirios Delimanolis Apr 23 '13 at 15:21
  • I need to take `v` for any specific entry and put it into `tableMaker` – user2007843 Apr 23 '13 at 15:23
  • But you have control over the arguments that `tableMaker` accepts. Change the arguments for that method to accept a double[] and pass `v` to it. – Mike Apr 23 '13 at 15:26
  • okay I changed the arguments, but my question is still how to pass `v` to `tableMaker`? Should I just put `tableMaker(model, v)` at the place where I need it in the for loop? I think I need another function that will only create the graph – user2007843 Apr 23 '13 at 15:28

1 Answers1

1

I sense that you are trying to implement the approach outlined here. Instead of trying to create a new chart each time, create a single chart in a ChartPanel and retain a reference to its XYPlot. In your ListSelectionListener, you can then use plot.setDataset() to update the chart. In this example, a ChangeListener fetches the desired dataset from an existing List<XYDataset>. In this example, a button's ActionListener generates the dataset each time it is called.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • allright I have come a long way with this, now how do I add the chart to the lowerPanel? `lowerPanel.add(chart)` doesn't work. – user2007843 Apr 23 '13 at 20:07
  • I also cannot add it to a JScrollPane – user2007843 Apr 23 '13 at 20:19
  • Try `ChartPanel lowerPanel = new ChartPanel(chart)`. – trashgod Apr 23 '13 at 21:02
  • I decided to use ChartFactory to create the histogram, but I am having trouble updating the chart. There's no `dataset.deleteSeries` which is what I need to balance out `dataset.addSeries`. If I try adding a series with null values I get an error – user2007843 Apr 24 '13 at 18:27
  • You can't remove a series from a `HistogramDataset`; look at `addSeries` to see why. Replace the entire dataset in the plot. – trashgod Apr 24 '13 at 19:38
  • I am really struggling with this, I am trying to add a series by subtracting the previous one from the current one and I don't even think this is going to work. – user2007843 Apr 24 '13 at 20:28
  • That's a reasonable alternative. – trashgod Apr 25 '13 at 16:50