我在我的项目中使用JSlider。在某些地方我需要获取滑块的值,但getValue()方法不会返回当前滑块值。它始终返回默认值。
以下是我的代码的相关部分:
this.slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
// This two lines below are about Cytoscape framework. I am taking a column of a table and convert it to List<String>. There are not doing anything about slider.
CyColumn timeColumn = table.getColumn("startTime"); // Getting start time column
List<String> timeList = filter.getTimeFromColumn(timeColumn); // Gets value of start time column without null values
// I printed out slider.getValue() value and it returns default slider value all the time.
sliderLabel.setText(timeList.get(slider.getValue()));
}
});
在我的代码的另一部分中,我需要更改滑块的最小值和最大值。为此,我使用下面的代码:
public void reCreateSlider(){
// Activity size returns correct.
ArrayList<CyNode> activities = filter.FilterRowByNodeType("activity", "nodeType");
this.slider = new JSlider(0, activities.size());
}
为什么slider.getValue()一直返回默认值?非常感谢,
答案 0 :(得分:0)
实际上我只是改变了下面的reCreateSlider()方法并且它有效。
public void reCreateSlider(){
// Activity size returns correct.
ArrayList<CyNode> activities = filter.FilterRowByNodeType("activity", "nodeType");
slider.setMinimum(0);
slider.setMaximum(activities.size());
}
如果有人遇到同样的问题,可以这样做。