包含JPanel的JScrollPane不会滚动

时间:2013-05-20 20:30:59

标签: java swing jpanel jscrollpane preferredsize

我有一个JScrollPane,其中包含一个JPanel,其中包含一组JPanel。

可以更改集合大小,这就是我在JScrollPane中使用它的原因。

出于某种原因,无论我做什么,旋钮都不会出现在滚动条上。

正如您所看到的,主要的JPanel(预览)确实有一个布局。我读到如果布局为null,则可能会发生这种情况。

有谁知道如何解决这个问题?

记住

  • ReactionPreviewPanel扩展了JPanel。
  • show(ArrayList)方法将创建一堆ReactionPreviewPanel并使用SpringLayout在主(预览)JPanel中组织它们。

这是我的代码:

import java.awt.Color;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;

public class ResultsPanel extends JPanel
{
    private static final long serialVersionUID = -1713825505191028954L;
    private JTextPane name, url, tags;
    private JButton clear;
    private JLabel results;
    private JScrollPane scroller;
    private JPanel previews;

    public ResultsPanel()
    {
        super();
        initElements();
        addElements();
        setLayout(getSpringLayout());
    }

    private void initElements()
    {
        this.previews = new JPanel();
        this.previews.setLayout(new SpringLayout());
        this.previews.setBackground(Color.white);
        this.scroller = new JScrollPane(this.previews, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        this.scroller.getVerticalScrollBar().setUnitIncrement(1);
        this.results = new JLabel("[Displaying nothing]");
        this.results.setHorizontalAlignment(SwingConstants.CENTER);

        this.name = new JTextPane();
        this.name.setEditable(false);
        this.url = new JTextPane();
        this.url.setEditable(false);
        this.tags = new JTextPane();
        this.tags.setEditable(false);
        this.clear = new JButton("Clear");
        this.clear.setEnabled(false);
    }

    private void addElements()
    {
        add(clear);
        add(this.results);
        add(this.scroller);
    }

    private SpringLayout getSpringLayout()
    {
        SpringLayout layout = new SpringLayout();
        layout.putConstraint(SpringLayout.SOUTH, this.clear, -5, SpringLayout.SOUTH, this);
        layout.putConstraint(SpringLayout.EAST, this.clear, -5, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.WEST, this.clear, 5, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, this.results, 5, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.EAST, this.results, -5, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.WEST, this.results, 5, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, this.scroller, 2, SpringLayout.SOUTH, this.results);
        layout.putConstraint(SpringLayout.EAST, this.scroller, -5, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.WEST, this.scroller, 5, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.SOUTH, this.scroller, -2, SpringLayout.NORTH, this.clear);
        return layout;
    }

    public void show(ArrayList<Reaction> reactions)
    {
        this.results.setText(reactions.size() + " results");
        this.previews.removeAll();
        this.previews.repaint();
        if (reactions == null || reactions.size() == 0)
            return;
        ArrayList<ReactionPreviewPanel> panels = new ArrayList<ReactionPreviewPanel>();
        for (Reaction r : reactions)
        {
            ReactionPreviewPanel temp = new ReactionPreviewPanel(r);
            panels.add(temp);
        }
        for (ReactionPreviewPanel rpp : panels)
            this.previews.add(rpp);
        SpringLayout layout = new SpringLayout();
        for (int i = 0; i < panels.size(); i++)
        {
            layout.putConstraint(SpringLayout.WEST, panels.get(i), 2, SpringLayout.WEST, this.previews);
            layout.putConstraint(SpringLayout.EAST, panels.get(i), -3, SpringLayout.EAST, this.previews);
            if (i == 0)
                layout.putConstraint(SpringLayout.NORTH, panels.get(i), 2, SpringLayout.NORTH, this.previews);
            else
                layout.putConstraint(SpringLayout.NORTH, panels.get(i), 2, SpringLayout.SOUTH, panels.get(i - 1));
        }
        this.previews.setLayout(layout);
    }
}

1 个答案:

答案 0 :(得分:0)

如果没有更多细节,我猜想ResultsPanel的容器正在扩展以包含整个ResultsPanel,因此滚动条永远不必要。强制限制结果面板的最大大小和/或结果面板的容器。