miglayout 行格式:包含 1 行 JTextArea 的 JScrollPane

发布于 2024-10-11 05:02:07 字数 468 浏览 3 评论 0原文

如果需要,我想显示一个带有水平滚动条的单行文本标签。 (没有垂直滚动条,因为我知道它是一行。)

我通过包含 JTextArea 的 JScrollPane 实现。

对于布局管理器,我正在使用 MigLayout,但似乎无法弄清楚行格式规范的用途。如果我只使用 [] 那么它在没有水平滚动条的情况下工作得很好,但是当滚动条出现时,它看起来很糟糕,因为滚动条占据了所有空间。

我想做的是:

  • 显示具有恒定高度的 JScrollPane,当滚动条存在时看起来不错,并且当滚动条不存在时有额外的空间
  • 显示具有可变高度的 JScrollPane,以便窗格的高度为 1当滚动条不存在时的文本行,或者当滚动条存在时用于容纳滚动条的附加空间
  • 以仅需要 1 行文本的方式显示 JScrollPane(例如,滚动条被更改,以便它占用一些水平空间有关文本的右侧)

有什么建议吗?

I want to display a one-line text label that has a horizontal scrollbar if necessary. (No vertical scrollbar since I know it's one line.)

I am implementing via a JScrollPane containing a JTextArea.

For layout manager, I am using MigLayout and can't seem to figure out what to use for the row format specification. If I just use [] then it works fine for no horizontal scrollbar, but when the scrollbar appears, it looks bad since the scrollbar takes up all the space.

What I would like to do is either:

  • show the JScrollPane with a constant height that looks good when the scrollbar is present, and has extra space when the scrollbar is absent
  • show the JScrollPane with variable height so that the height of the pane is the 1 line of text when the scrollbar is absent, or an additional space to accomodate the scrollbar when it's present
  • show the JScrollPane in a way that only takes 1 row of text (e.g. the scrollbar is changed so that it eats up some of the horizontal space to the right of the text in question)

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御弟哥哥 2024-10-18 05:02:07

有趣的问题(我并没有像我应该的那样过度热衷于研究 jpa )

基本上,布局本身不能做太多事情:scrollPane 的首选大小根据水平滚动条的可见性而不同。在我看来,客户端代码可以动态地告诉管理器如何处理要覆盖的区域。下面是一个可以使用的代码片段。

  • 它使用一个不可见的虚拟组件,该组件具有可能可见的水平滚动条的固定大小(是的,姑娘们,我知道,固定大小应该动态调整到scrollPane水平滚动条的任何高度变化属性:-)
  • 它在scrollPane 的水平滚动条,根据可见性变化调整虚拟对象的隐藏模式并重新验证包含的面板。

对于 Win/Nimbus 工作得很好,但 Metal 中有一个小故障(也许还有其他 LAF),需要对差异进行神奇的数字调整以保持布局稳定

    JTextArea area = new JTextArea("starting ", 1, 10);
    JScrollPane areaScrollPane = new JScrollPane(area);
    areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
    areaScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    Dimension dim = areaScrollPane.getPreferredSize();
    areaScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    // get the height diff with/out horizontal scrollbar
    int diff = dim.height - areaScrollPane.getPreferredSize().height;
    areaScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    LC lc = new LC().wrapAfter(2).debug(500);
    final MigLayout layout = new MigLayout(lc);
    final JPanel panel = new JPanel(layout);
    panel.add(new JLabel("OneLineRow"));

    panel.add(areaScrollPane);
    // create and add an invisible filler
    // note: metal needs magic adjust, dont know why
    // diff -= 3;
    final JComponent dummy = (JComponent) Box.createVerticalStrut(diff);
    dummy.setVisible(false);
    final String dummyConstraint = "span, hidemode ";
    panel.add(dummy, dummyConstraint + "0");
    // component listener which adjusts hidemode of filler on 
    // scrollpane's horizontal scrollbar showing/hiding
    ComponentAdapter adapter = new ComponentAdapter() {

        /** 
         * @inherited <p>
         */
        @Override
        public void componentShown(ComponentEvent e) {
            layout.setComponentConstraints(dummy, dummyConstraint + "2");
            panel.revalidate();
        }

        /** 
         * @inherited <p>
         */
        @Override
        public void componentHidden(ComponentEvent e) {
            layout.setComponentConstraints(dummy, dummyConstraint + "0");
            panel.revalidate();
        }

    };
    areaScrollPane.getHorizontalScrollBar().addComponentListener(adapter);
    panel.add(new JScrollPane(new JTable(20, 5)), "span");
    showInFrame(panel, "one line textArea");

非常欢迎反馈,也许有一种我忽略的不那么人为的方法

Interesting problem (and me not overly enthused with looking into jpa as I should )

Basically, it's not much a layout could do on its own: the pref size of the scrollPane differs depending on the visibility of the horizontal scrollBar. It's up to client code to dynamically tell the manager what to do with the to-be covered area, IMO. Below is a code-snippet to play with.

  • it uses an invisible dummy component with the fixed size of the maybe visible horizontal scrollbar (yeah, gals, I know, that fixed size should be adjusted dynamically to any height changing properties of the scrollPane's horizontal scrollbar :-)
  • it installs a componentListener on the scrollPane's horizontal scrollBar, adjusts the dummy's hidemode on visibility changes and revalidates the containing panel.

Works fine for Win/Nimbus, there's a glitch in Metal, though (and maybe other LAFs) which needs a magic number adjust of the diff to keep the layout steady

    JTextArea area = new JTextArea("starting ", 1, 10);
    JScrollPane areaScrollPane = new JScrollPane(area);
    areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
    areaScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    Dimension dim = areaScrollPane.getPreferredSize();
    areaScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    // get the height diff with/out horizontal scrollbar
    int diff = dim.height - areaScrollPane.getPreferredSize().height;
    areaScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    LC lc = new LC().wrapAfter(2).debug(500);
    final MigLayout layout = new MigLayout(lc);
    final JPanel panel = new JPanel(layout);
    panel.add(new JLabel("OneLineRow"));

    panel.add(areaScrollPane);
    // create and add an invisible filler
    // note: metal needs magic adjust, dont know why
    // diff -= 3;
    final JComponent dummy = (JComponent) Box.createVerticalStrut(diff);
    dummy.setVisible(false);
    final String dummyConstraint = "span, hidemode ";
    panel.add(dummy, dummyConstraint + "0");
    // component listener which adjusts hidemode of filler on 
    // scrollpane's horizontal scrollbar showing/hiding
    ComponentAdapter adapter = new ComponentAdapter() {

        /** 
         * @inherited <p>
         */
        @Override
        public void componentShown(ComponentEvent e) {
            layout.setComponentConstraints(dummy, dummyConstraint + "2");
            panel.revalidate();
        }

        /** 
         * @inherited <p>
         */
        @Override
        public void componentHidden(ComponentEvent e) {
            layout.setComponentConstraints(dummy, dummyConstraint + "0");
            panel.revalidate();
        }

    };
    areaScrollPane.getHorizontalScrollBar().addComponentListener(adapter);
    panel.add(new JScrollPane(new JTable(20, 5)), "span");
    showInFrame(panel, "one line textArea");

Feedback highly welcome, maybe there is a less artificial approach that I overlooked

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文