【问题标题】:Swing JScrollPane - how to set vertical scroll bar to left?Swing JScrollPane - 如何将垂直滚动条设置为左侧?
【发布时间】:2011-08-09 07:03:23
【问题描述】:

见标题。我尝试将组件方向更改为 RIGHT_TO_LEFT,但这产生了意想不到的副作用 - 对于具有指定首选大小的组件,它的行为很奇怪。

(JDK 1.6.0_23,Eclipse VE)

编辑

这是一个例子:

我们有JFramejMainScrollPane。在jMainScrollPane 中,我们放置一个jMainPanel。现在将jMainPanel 的首选大小设置为比jMainScrollPane 窄。 jMainPanel 仍将占用 jMainScrollPane 上的所有空间。现在将jMainScrollPane 的方向更改为RIGHT_TO_LEFT,看看会发生什么。

示例代码(更改jMainScrollPane 的方向以查看差异):

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class TestFrame extends JFrame {
    private JPanel      jContentPane    = null;
    private JScrollPane jMainScrollPane = null;
    private JPanel      jMainPanel      = null;

    public TestFrame(){
        super();
        initialize();
    }

    private void initialize(){
        this.setSize(480, 339);
        this.setContentPane(getJContentPane());
        this.setTitle("JFrame");
    }

    private JPanel getJContentPane(){
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(new BorderLayout());
            jContentPane.add(getJMainScrollPane(), BorderLayout.CENTER);
        }
        return jContentPane;
    }

    private JScrollPane getJMainScrollPane(){
        if (jMainScrollPane == null) {
            jMainScrollPane = new JScrollPane();
            jMainScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            jMainScrollPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
            jMainScrollPane.setViewportView(getJMainPanel());
        }
        return jMainScrollPane;
    }

    private JPanel getJMainPanel(){
        if (jMainPanel == null) {
            jMainPanel = new JPanel();
            jMainPanel.setLayout(new BorderLayout());
            jMainPanel.setPreferredSize(new Dimension(30, 30));
        }
        return jMainPanel;
    }
}

EDIT2

由于这种奇怪的性质,我已向 Oracle 提交了一个错误。他们给我发了一个错误链接

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7038455

目前还没有这样的错误 - 我想,它正在检查中。

但是,问题仍然存在 - 是否有解决方法或其他方法?

【问题讨论】:

  • @Manoj 似乎@Frozen_Spider 已经尝试过提到的问题的答案,但对结果不满意。
  • @Frozen_Spider 当你写“里面的布局被破坏”时,你能告诉我们你的意思吗?还是发个截图?
  • 我同意。给定的解决方案应该有效。我已经在JTableJScrollpane 上对其进行了测试。 JScrollpane 没有设置任何首选尺寸。
  • 我注意到你有两个JScrollPanes...主要的一个查看jMainPanel,第二个什么也没查看。你为什么要这样设计?如果您的意图是触发jMainPanel 的垂直滚动条,您只需要JMainPanel.setPreferredSize(new Dimension(500, 400)); 并删除我看不到目的的第二个JScrollPane

标签: java swing jscrollpane


【解决方案1】:

同意 OP:这是错误。 RToL 方向的设置会触发 viewPosition 的计算。这发生在布局完成之前。在这种情况下,JViewport 返回视图的preferredSize 而不是其实际大小(当时为零)。在内心深处,BasicScrollPaneUI.Handler 不知道这个伪造的大小,它的计算基于不正确的数据。

这里有一些代码可以玩(原来的 OP 被进一步剥离,添加了彩色边框以查看是什么):

public class COInScrollPane extends JFrame {

    public COInScrollPane(){
        super();
        initialize();
    }

    private void initialize(){
        this.setTitle("JFrame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JScrollPane jMainScrollPane = getJMainScrollPane();
        this.add(jMainScrollPane);
        this.setSize(480, 339);
        Action action = new AbstractAction("Toggle CO") {

            @Override
            public void actionPerformed(ActionEvent e) {
                ComponentOrientation co = jMainScrollPane.getComponentOrientation().isLeftToRight() ?
                        ComponentOrientation.RIGHT_TO_LEFT : ComponentOrientation.LEFT_TO_RIGHT;
                jMainScrollPane.applyComponentOrientation(co);
                jMainScrollPane.revalidate();
                jMainScrollPane.repaint();
            }
        };
        add(new JButton(action), BorderLayout.SOUTH);
    }

    private JScrollPane getJMainScrollPane() {
        JScrollPane jMainScrollPane = new JScrollPane(getJMainPanel());
        jMainScrollPane.setViewportBorder(BorderFactory
                .createLineBorder(Color.GREEN));
        jMainScrollPane
                .applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        return jMainScrollPane;
    }

    private JPanel getJMainPanel() {
        JPanel jMainPanel = new JPanel();
        jMainPanel.add(new JButton("just a button"));
        jMainPanel.setBorder(BorderFactory.createLineBorder(Color.RED));
        return jMainPanel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new COInScrollPane().setVisible(true);

            }
        });
    }
}

初始布局完全错误(由 OP 报告),用按钮切换 CO 两次 - 布局看起来不错。

一个快速而肮脏的黑客可能是在生产环境中这样做:在初始布局之后,强制 CO 进入 LToR 以允许正确的坐标,然后返回到 RToL。

【讨论】:

  • 谢谢你,肮脏的黑客作品 :) 我也发布了一个错误链接,但是 - 真遗憾 - 我描述了一个比你做的更糟糕的错误。
  • @Frozen Spider 很高兴它对你有用 :-) 评论 ID 是什么(自动错误响应上的数字)?
  • 这个bug到底有什么秘密...Some bugs are not included in the Bug Database for security reasons.
  • Bug ID 贴在问题的底部:7038455 @eee:看来,你是对的。好吧,我很高兴他们知道。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-23
  • 2016-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多