我需要将从 for 循环内部找到的值用于不在循环内部的函数中,但我不知道如何执行此操作。我希望完成的是从 HashMap 中的键中提取值,然后仅在选择该行时在 JTable
中绘制(使用 ListSelectionListener
)。这样,我可以避免绘制一百个表格,从而节省大量时间。我还使用 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
以便我可以创建数据集。
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();
}
}
});
}
最佳答案
我感觉到您正在尝试实现概述的方法 here 。不必每次都尝试创建新图表,而是在 ChartPanel
中创建单个图表。并保留对其 XYPlot
的引用。在你的ListSelectionListener
,然后您可以使用 plot.setDataset()
更新图表。在此example ,一个ChangeListener
从现有 List<XYDataset>
中获取所需的数据集。在此example ,一个按钮的ActionListener
每次调用时都会生成数据集。
关于java - 在 for 循环外部的函数中使用来自 for 循环的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16173181/