在其外部函数中使用for循环中的值 [英] Using values from a for loop in a function outside of it
问题描述
我需要获取从for循环中找到的值,以在不在循环内的函数中使用,而且我不知道该怎么做.我希望完成的工作是从哈希图中的键中提取值,然后仅在选择了该行(使用ListSelectionListener
)后才在JTable
中进行绘制.这样,我可以避免绘制一百张表的图形,这样可以节省大量时间.我也在使用DefaultTableModel
.
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
.
这是我的for循环:
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
}
在我的tableMaker
函数中,我创建我的JTable
并添加一个ListSelectionListener
,希望在其中选择行时创建直方图并将其添加到我的lowerPanel中.这是一些代码.我需要v
以便创建数据集.
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();
}
}
});
}
推荐答案
我认为您正在尝试实施此处.不必每次尝试创建一个新图表,而是在ChartPanel
中创建一个图表并保留对其XYPlot
的引用.然后,您可以在ListSelectionListener
中使用plot.setDataset()
来更新图表.在此示例中,ChangeListener
从现有的List<XYDataset>
获取所需的数据集.在此示例中,按钮的ActionListener
每次调用时都会生成数据集.
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.
这篇关于在其外部函数中使用for循环中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!