带有 JScrollPane 和原型单元格值的 JList 包裹元素名称(用点替换而不是显示水平滚动条),为什么?

发布于 2024-08-30 16:15:35 字数 1836 浏览 9 评论 0原文

我在 JScrollPane 中有一个 Jlist,并且设置了一个原型值,这样它就不必计算大列表的宽度,而只需使用此默认宽度。

现在的问题是,Jlist 由于某种原因用点 (...) 替换了元素的末尾,因此水平滚动条永远不会显示。

如何禁用“包装”?那么,如果长元素比 Jlist 的宽度宽,那么它们不会被点替换?

我在一个小示例应用程序中重现了该问题。如果您不明白我的意思,请运行它:

import javax.swing.*;
import java.awt.*;

public class Test
{
    //window
    private static final int windowWidth = 450;
    private static final int windowHeight = 500;

    //components
    private JFrame frame;
    private JList classesList;
    private DefaultListModel classesListModel;

    public Test()
    {
        load();
    }

    private void load()
    {   
        //create window
        frame = new JFrame("Test");
        frame.setSize(windowWidth, windowHeight);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setUndecorated(true);
        frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);

        //classes list
        classesListModel = new DefaultListModel();
        classesList = new JList(classesListModel);
        classesList.setPrototypeCellValue("prototype value");
        classesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        classesList.setVisibleRowCount(20);
        JScrollPane scrollClasses = new JScrollPane(classesList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);     

        for (int i = 0; i < 200; i++)
        {
            classesListModel.addElement("this is a long string, does not fit in width");
        }

        //panel
        JPanel drawingArea = new JPanel();
        drawingArea.setBackground(Color.white);

        drawingArea.add(scrollClasses);
        frame.add(drawingArea);

        //set visible
        frame.setVisible(true);
    }
}

即使您强制水平滚动条,您仍然无法滚动,因为由于点(...)换行,元素实际上不比宽度宽。

提前致谢。

I've got a Jlist inside a JScrollPane and I've set a prototype value so that it doesn't have to calculate the width for big lists, but just uses this default width.

Now, the problem is that the Jlist is for some reason replacing the end of an element with dots (...) so that a horizontal scrollbar will never be shown.

How do I disable with "wrapping"? So that long elements are not being replaced with dots if they are wider than the Jlist's width?

I've reproduced the issue in a small example application. Please run it if you don't understand what I mean:

import javax.swing.*;
import java.awt.*;

public class Test
{
    //window
    private static final int windowWidth = 450;
    private static final int windowHeight = 500;

    //components
    private JFrame frame;
    private JList classesList;
    private DefaultListModel classesListModel;

    public Test()
    {
        load();
    }

    private void load()
    {   
        //create window
        frame = new JFrame("Test");
        frame.setSize(windowWidth, windowHeight);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setUndecorated(true);
        frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);

        //classes list
        classesListModel = new DefaultListModel();
        classesList = new JList(classesListModel);
        classesList.setPrototypeCellValue("prototype value");
        classesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        classesList.setVisibleRowCount(20);
        JScrollPane scrollClasses = new JScrollPane(classesList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);     

        for (int i = 0; i < 200; i++)
        {
            classesListModel.addElement("this is a long string, does not fit in width");
        }

        //panel
        JPanel drawingArea = new JPanel();
        drawingArea.setBackground(Color.white);

        drawingArea.add(scrollClasses);
        frame.add(drawingArea);

        //set visible
        frame.setVisible(true);
    }
}

Even if you force horizontal scrollbar, you still won't be able to scroll because the element is actually not wider than the width because of the dot (...) wrapping.

Thanks in advance.

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

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

发布评论

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

评论(3

无戏配角 2024-09-06 16:15:35

当添加到滚动窗格的组件的首选大小大于滚动窗格的大小时,滚动条会自动出现。

通过使用 setPrototypeCellValue(...) 方法,您将影响列表计算其首选大小的方式,这意味着您有责任提供正确的值以确保字符串不会被截断。

因此,简单的解决方案不是不使用该方法,但此外您还需要将滚动窗格的首选大小设置为您想要的任何大小。如果需要的话,就会出现水平滚动条。

Scrollbars appear automatically when the preferred size of the component added to the scrollpane is greater than the size of the scrollpane.

By using the setPrototypeCellValue(...) method you are affecting the way the list calculates its preferred size, which means you are responsible for providing the proper value that ensures the strings will not be truncated.

So the simple solution is not not use that method, but in addition you will need to set the preferred size of the scrollpane to be whatever you want. Then the horizontal scrollbars will appear if required.

攀登最高峰 2024-09-06 16:15:35

我对这个问题的回答是,首先找到列表中最长的元素,然后使用
该元素上的 setPrototype 方法

My answer to that question is that first find the longest element in the list then use
setPrototype method on that elements

來不及說愛妳 2024-09-06 16:15:35

当您调用 classesList.setPrototypeCellValue("prototype value") 时,您是在告诉 JList classesList 将其最大宽度限制为字符串的长度“原型价值”。 (请参阅javadocs

然后,当您使用字符串“这是一个长字符串,不适合宽度”填充列表时,难怪它不适合宽度!因为您提供的原型的宽度小于您填充列表的字符串的宽度。

JScrollPane 将自动显示滚动条,您通常不需要调整它们的行为。 JList 还将自动调整其宽度以尝试显示列表中最大宽度的项目。当您通过调用 setPrototypeCellValue() 告诉 JList 修复其宽度时,就会出现问题。

如果注释掉

classesList.setPrototypeCellValue("prototype value");

或将其替换为

classesList.setPrototypeCellValue("this is a long string, does not fit in width");

然后它将按照您的预期运行。

When you call classesList.setPrototypeCellValue("prototype value") you are telling the JList classesList to limit its maximum width to the length of the string "prototype value". (See javadocs)

Then later on when you populate the list with the strings "this is a long string, does not fit in width", no wonder it does not fit in the width! Because the width of the prototype you gave it is smaller than the width of the string you are filling the list with.

The JScrollPane will automatically show the scrollbars and you usually don't need to adjust their behavior. The JList will also automatically adjust its width to try and show the maximum width item in the list. The problem occurs when you tell the JList to fix its width by calling the setPrototypeCellValue().

If you comment out

classesList.setPrototypeCellValue("prototype value");

or replace it with

classesList.setPrototypeCellValue("this is a long string, does not fit in width");

then it will function as you expected it to.

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