问题是垂直滚动条不会真正停止,而是先停止一个节点。如果您保持拆分视图打开,这可以正常工作。据我所知,这是因为Java本身存在以下问题。
如果垂直滚动条滚动到最大值,则JSplitPane
的单触式可展开按钮会将垂直滚动条值减少15,然后单击单触式可展开按钮以最小化左窗格。
任何人都可以通过(使用附加的样本测试)验证这一点,看到GUI更改了滚动条的位置。使用控制台输出也可以验证相同。
在我看来这是一个错误,我已将此报告为Oracle中的一个错误 - Java。 但尚未被接受。
任何解决方法?
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
/**
* Sample code.
*
* @author Saurav.Kumar
*
*/
public class OneTouchBug {
public void playUI() {
DefaultMutableTreeNode root;
root = new DefaultMutableTreeNode(0);
final JTree tree = new JTree(root);
JLabel bugLabel = new JLabel(
"<html>One touch button will reduce the vScroll <br>" +
"value by 15 if it is scrolled to maximum value and <br>" +
" then click one-touch Expandable ");
JPanel p = new JPanel();
p.add(bugLabel);
final JScrollPane jScrollPane = new JScrollPane(tree, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
final JSplitPane sptPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jScrollPane, p);
sptPane.setDividerLocation(90);
sptPane.setOneTouchExpandable(true);
jScrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
System.out.println("=======================" + e.getValue());
}
});
JFrame f = new JFrame();
f.getContentPane().add(sptPane);
f.setSize(400, 200);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
populateTree(root);
tree.scrollPathToVisible(new TreePath(root.getLastLeaf().getPath()));
}
private void populateTree(DefaultMutableTreeNode root) {
DefaultMutableTreeNode currentNode = root;
int counter = 0;
while (counter++ < 9) {
DefaultMutableTreeNode nextNode = new DefaultMutableTreeNode("" + counter);
currentNode.add(nextNode);
currentNode = nextNode;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new OneTouchBug().playUI();
}
});
}
}