gpt4 book ai didi

java - 使用 scrollRectToVisible() 时,需要调用两次才能正确定位。怎么修?

转载 作者:行者123 更新时间:2023-11-30 06:21:11 25 4
gpt4 key购买 nike

我的情况是:我的项目中有2个tab View ,Tab A View 显示列表,Tab B在JScrollPane中显示图表。特点是:点击选项卡A中的一个列表项,它将转到选项卡B并滚动到指向的图表X(可能是第一个,也可能是第二个....)

问题是:如果单击选项卡 A 中的一个项目,选项卡 B View 将滚动到最后一个图表(在 JScrollPane 的底部)。然后转到选项卡 A,单击另一个项目,选项卡 B 应该滚动到第一个图表(在 JScrollPane 的顶部)。但是,JScrollPane 仍然固定在底部。但是,如果我转到选项卡 A 并单击相同的项目 agian,则选项卡 B 可以正确地滚动到第一个图表。

代码如下:

protected void buildUI() { setLayout( new BorderLayout() );

   myChartPanel = new JPanel( new GridBagLayout() );

// add different charts for different Item in Tab A
myChartPanel.add(..)

myGraphViewPane = new JScrollPane();
Border scrollPaneBorder = myGraphViewPane.getBorder();
myGraphViewPane.setBorder( null );

//Set single graph view visible by default
myGraphViewPane.setViewportView( myChartPanel );

add( myGraphViewPane, BorderLayout.CENTER );
setBorder( scrollPaneBorder );

}

我试过下面的滚动代码,没有一个效果很好:

myChartPanel.scrollRectToVisible( focusChart.getBounds() );

myGraphViewPane.getVerticalScrollBar().setValue( focusChart.getLocation().x );

myGraphViewPane.getViewport().setViewPosition(new Point( focusChart.getLocation().x, chartView.getLocation().y ) );

为什么只点击两次,就可以滚动到正确的位置?太奇怪了!

最佳答案

尝试将 myChartPanel.scrollRectToVisible( focusChart.getBounds() ); 调用包装在 SwingUtilities.invokeLater()

关于java - 使用 scrollRectToVisible() 时,需要调用两次才能正确定位。怎么修?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20836109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com