首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JComboBox in a JScrollPane

JComboBox in a JScrollPane
EN

Stack Overflow用户
提问于 2013-12-01 12:52:45
回答 2查看 968关注 0票数 1

我在一个带有JComboBoxes的面板中添加了许多BoxLayout。所有工作良好的大文本将被包装成两行或三行,这就是我使用html标记的原因。

我的问题是,当有很多复选框时,我会添加一个ScrollPane。ScrollPane应该只使用"VERTICAL_SCROLLBAR_AS_NEEDED“,但这会将文本中的格式分解为一行。

编辑:

问题是,如何将面板添加到JScrollPane中,并在组合框文本中添加换行符。使用滚动窗格,它不能工作。

下面是一个简单的例子:

代码语言:javascript
复制
public static void main(String[] args) {
    new MySwingTest();
}

private MySwingTest(){
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(createCenterPanel(), BorderLayout.CENTER);
    frame.setSize(400, 400);
    frame.setVisible(true);
}

private Component createCenterPanel() {

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
    for(int i = 0 ; i < 10;i++){
        panel.add(new JCheckBox("<html>das das das das das da d da fdfsdf dfsd fsdfsdf sdfsd fsdfsd fsdf fsd fss fs </html>"));
    }
    JScrollPane pane = new JScrollPane(panel,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    return pane;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-01 15:19:48

如果我正确理解,您真正想要的是一个比包含它的JScrollPane更宽的面板。Swing有一个接口可滚动,专门用于此目的:

代码语言:javascript
复制
class CheckboxPanel
extends JPanel
implements Scrollable {
    private final int checkBoxHeight;

    public CheckboxPanel() {
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        checkBoxHeight = new JCheckBox("Example").getPreferredSize().height;
    }

    @Override
    public boolean getScrollableTracksViewportWidth() {
        // This prevents a horizontal scrollbar from appearing.
        return true;
    }

    @Override
    public boolean getScrollableTracksViewportHeight() {
        return false;
    }

    @Override
    public Dimension getPreferredScrollableViewportSize() {
        return getPreferredSize();
    }

    @Override
    public int getScrollableUnitIncrement(Rectangle visibleRect,
                                          int orientation,
                                          int direction) {
        if (orientation == SwingConstants.HORIZONTAL) {
            return 1;
        }

        return Math.min(checkBoxHeight, direction < 0 ?
            visibleRect.y : getHeight() - (visibleRect.y + visibleRect.height));
    }

    @Override
    public int getScrollableBlockIncrement(Rectangle visibleRect,
                                           int orientation,
                                           int direction) {
        if (orientation == SwingConstants.HORIZONTAL) {
            return 10;
        }

        return Math.min(visibleRect.height, direction < 0 ?
            visibleRect.y : getHeight() - (visibleRect.y + visibleRect.height));
    }
}
票数 2
EN

Stack Overflow用户

发布于 2013-12-01 13:51:27

我会为面板添加一些addComponentListener并设置setPreferredSize

代码语言:javascript
复制
public class MySwingTest {

    JScrollPane scrollPane;

    JPanel panel;

    public static void main(String[] args) {
        new MySwingTest();
    }

    private MySwingTest(){
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createCenterPanel(), BorderLayout.CENTER);
        frame.setSize(400, 400);
        frame.setVisible(true);
    }

    private Component createCenterPanel() {

        panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
        for(int i = 0 ; i < 10;i++){
            panel.add(new JCheckBox("<html>das das das das das da d da fdfsdf dfsd fsdfsdf sdfsd fsdfsd fsdf fsd fss fs </html>"));
        }   

        scrollPane = new JScrollPane(panel,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);   

        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

          scrollPane.addComponentListener(new ComponentAdapter() {   
             @Override
             public void componentResized(ComponentEvent e) {              
                Rectangle rect = scrollPane.getViewportBorderBounds();

              panel.setPreferredSize(new Dimension((int)rect.getWidth()-10, (int)panel.getPreferredSize().getHeight()+100)); // dummy offset
             }
          });

        return scrollPane;
    }
}

你想要的不是理想的,而是方向。

希望它能帮上忙

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20312301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档