如何在启动程序时将包含JPanels的JScrollPane滚动到顶部?

时间:2016-01-28 13:35:42

标签: java swing jpanel

我遇到了JScrollPane的问题,在程序启动时我无法从顶部开始。我希望这里的一些巫师可能有答案:P

这是一个有效的代码示例:

public class JScrollPaneExample extends JPanel{

public JScrollPaneExample(int rowCount){

    setLayout(new BorderLayout());

    JScrollPane scrollPane = makeScrollPane(rowCount);
    add(scrollPane, BorderLayout.PAGE_START);
    setPreferredSize(new Dimension(300,300));
}

private JScrollPane makeScrollPane(int rowCount) {
    JScrollPane scrollPane = new JScrollPane();

    JPanel pane = new JPanel();
    pane.setLayout(new GridLayout(rowCount, 0));

    for(int i = 0; i < rowCount; i++){
        if(i != 4)
            pane.add(makeTestField("Test " + (i+1), "Test description: " + (i+1), i));
        else {
            pane.add(makeTestField("This is a test", 
                                       "This is also a test to see if this field will do a line break. "
                                       + "This is also a test to see if this field will do a line break. "
                                       + "Something else could be entirely different...", 
                                       i));
        }
    }

    scrollPane.getViewport().add(pane);
    scrollPane.setPreferredSize(new Dimension(200, 400));   
    return scrollPane;
}

protected static JPanel makeTestField(String header, String description, int rowCount){ 
    JPanel testField = new JPanel();

    testField.setLayout(new BorderLayout());
    testField.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

    JLabel label = new JLabel(header);
    Font font = new Font("Serif", Font.BOLD, 18);
    label.setFont(font);

    JButton btn = new JButton("Start");

    JTextArea textArea = new JTextArea(description);
    textArea.setEditable(false);
    textArea.setBackground(label.getBackground());
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    textArea.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));

    testField.add(label, BorderLayout.PAGE_START);
    testField.add(textArea, BorderLayout.CENTER);
    testField.add(btn, BorderLayout.LINE_END);

    return testField;
}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            // Turn off metal's use of bold fonts
            UIManager.put("swing.boldMetal", Boolean.FALSE);

            JFrame frame = new JFrame();
            JPanel scrollPane = new JScrollPaneExample(8);
            frame.add(scrollPane);
            frame.setVisible(true);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setPreferredSize(new Dimension(500,500));
            frame.pack();
        }
    });
}
}

我尝试在浏览interwebz时尝试退出许多不同的方法,例如设置scrollPanel的视口和许多其他内容,但是没有任何成功。

2 个答案:

答案 0 :(得分:4)

要执行此操作,您需要设置垂直滚动条的值。这是代码。

public class JScrollPaneExample extends JPanel{

    public JScrollPaneExample(int rowCount){

        setLayout(new BorderLayout());

        final JScrollPane scrollPane = makeScrollPane(rowCount);
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                scrollPane.getVerticalScrollBar().setValue(0);

            }
        });
        add(scrollPane, BorderLayout.PAGE_START);
        setPreferredSize(new Dimension(300,300));
    }

    private JScrollPane makeScrollPane(int rowCount) {
        final JScrollPane scrollPane = new JScrollPane();

        final JPanel pane = new JPanel();
        pane.setLayout(new GridLayout(rowCount, 0));

        for(int i = 0; i < rowCount; i++){
            if(i != 4)
                pane.add(makeTestField("Test " + (i+1), "Test description: " + (i+1), i));
            else {
                pane.add(makeTestField("This is a test", 
                        "This is also a test to see if this field will do a line break. "
                                + "This is also a test to see if this field will do a line break. "
                                + "Something else could be entirely different...", 
                                i));
            }
        }

        scrollPane.getViewport().add(pane);
        scrollPane.setPreferredSize(new Dimension(200, 400));   
        return scrollPane;
    }

    protected static JPanel makeTestField(String header, String description, int rowCount){ 
        final JPanel testField = new JPanel();

        testField.setLayout(new BorderLayout());
        testField.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

        final JLabel label = new JLabel(header);
        final Font font = new Font("Serif", Font.BOLD, 18);
        label.setFont(font);

        final JButton btn = new JButton("Start");

        final JTextArea textArea = new JTextArea(description);
        textArea.setEditable(false);
        textArea.setBackground(label.getBackground());
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));

        testField.add(label, BorderLayout.PAGE_START);
        testField.add(textArea, BorderLayout.CENTER);
        testField.add(btn, BorderLayout.LINE_END);

        return testField;
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                // Turn off metal's use of bold fonts
                UIManager.put("swing.boldMetal", Boolean.FALSE);

                final JFrame frame = new JFrame();
                final JPanel scrollPane = new JScrollPaneExample(8);
                frame.add(scrollPane);
                frame.setVisible(true);
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setPreferredSize(new Dimension(500,500));
                frame.pack();
            }
        });
    }
}

答案 1 :(得分:1)

另一个解决方案可能是使用JComponent#scrollRectToVisible并将其传递给0, 0, 1, 1(或类似的东西)

您需要在添加到JScrollPane

的基本组件上调用此方法