简体   繁体   English

JScrollPane不会截断JLabel文本

[英]JScrollPane doesn't truncate JLabel text

So I'm trying to create a table with information, and the rows in the table exceeds the height of the JFrame, therefor I'm trying to use a JScrollPane as a solution. 因此,我试图创建一个包含信息的表,并且表中的行超过了JFrame的高度,因此,我试图使用JScrollPane作为解决方案。

When I add the JPanel to the Frame it truncates the JLabels text, if it exceeds the bounds of the frame, as seen in the picture below: 当我将JPanel添加到Frame时,如果它超出了框架的边界,它将截断JLabels文本,如下图所示:

没有JScrollPane的JPanel The code: 编码:

    public class TestApplication extends JPanel{
JPanel headers = new JPanel(new GridLayout(1, 5));

JPanel informationTable = new JPanel();
JPanel[] rows = new JPanel[30];

public TestApplication(){

    headers.add(new JLabel("Name"));
    headers.add(new JLabel("City"));
    headers.add(new JLabel("Adress"));
    headers.add(new JLabel("Phone"));
    headers.add(new JLabel("Mail"));

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

    //Populating the table with 20 rows
    for(int i = 0; i < 30; i++){
        rows[i] = new JPanel();

        rows[i].setLayout(new GridLayout(1, 6));

        rows[i].add(new JLabel("Undefined name"));
        rows[i].add(new JLabel("Undefined city"));
        rows[i].add(new JLabel("1529th Avenue street name"));
        rows[i].add(new JLabel("Undefined Phone"));
        rows[i].add(new JLabel("Undefined Mail"));

        informationTable.add(rows[i]);
    }
    super.setLayout(new BorderLayout());
    super.add(headers, BorderLayout.NORTH);
    super.add(informationTable, BorderLayout.CENTER);
}

However when I place the JPanel with the rows inside the JScrollPane it doesn't truncate the JLabel text, which then exceeds the JFrame, and miss aligns all of the columns, as seen in the picture below: 但是,当我将JPanel与JScrollPane内的行放置在一起时,它不会截断JLabel文本,该文本会超出JFrame,并且会丢失所有列的对齐,如下图所示: JPanel添加了JScrollPane The code: 编码:

    public class TestApplication extends JPanel{
JPanel headers = new JPanel(new GridLayout(1, 5));

JPanel informationTable = new JPanel();
JPanel[] rows = new JPanel[30];
JScrollPane scrollTable = new JScrollPane(informationTable);

public TestApplication(){

    headers.add(new JLabel("Name"));
    headers.add(new JLabel("City"));
    headers.add(new JLabel("Adress"));
    headers.add(new JLabel("Phone"));
    headers.add(new JLabel("Mail"));

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

    //Populating the table with 20 rows
    for(int i = 0; i < 30; i++){
        rows[i] = new JPanel();

        rows[i].setLayout(new GridLayout(1, 6));

        rows[i].add(new JLabel("Undefined name"));
        rows[i].add(new JLabel("Undefined city"));
        rows[i].add(new JLabel("1529th Avenue street name"));
        rows[i].add(new JLabel("Undefined Phone"));
        rows[i].add(new JLabel("Undefined Mail"));

        informationTable.add(rows[i]);
    }
    scrollTable.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

    super.setLayout(new BorderLayout());
    super.add(headers, BorderLayout.NORTH);
    super.add(scrollTable, BorderLayout.CENTER);
}

The main method for both examples is the same. 这两个示例的主要方法相同。 I don't think it has anything to do with the problem, but it can be seen in the code below: 我认为这与问题无关,但是可以在下面的代码中看到:

    public static void main(String[] args) {
    JFrame main = new JFrame("JScrollPane exceeds bounds");

    TestApplication app = new TestApplication();

    main.add(app);

    main.setSize(600, 513);
    main.setLocationRelativeTo(null);
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    main.setVisible(true);
}

When I add the JPanel to the Frame it truncates the JLabels text, if it exceeds the bounds of the frame 当我将JPanel添加到框架时,如果它超出了框架的界限,它将截断JLabels文本

Correct, because the layout manager forces the components to be sized in the space available. 正确,因为布局管理器会强制在可用空间中调整组件的大小。

However when I place the JPanel with the rows inside the JScrollPane it doesn't truncate the JLabel text, which then exceeds the JFrame 但是,当我将JPanel与JScrollPane内的行一起放置时,它不会截断JLabel文本,这会超出JFrame

Correct, because the point of using a JScrollPane is to allow each component to be displayed at its preferred size. 正确,因为使用JScrollPane的目的是允许每个组件以其首选大小显示。 If the component size doesn't fit then scrolling will result. 如果组件尺寸不合适,则将导致滚动。

If you want to control the width of the panel to fit the width of the scrollpane then you need to implement the Scrollable interface on your panel and override the getScrollableTracksViewportWidth() method to return Boolean.TRUE . 如果要控制面板的宽度以适合滚动窗格的宽度,则需要在面板上实现Scrollable接口,并重写getScrollableTracksViewportWidth()方法以返回Boolean.TRUE Read the API for more information about the Scrollable interface. 阅读API,以获取有关Scrollable接口的更多信息。

An easy way to do this is to use the Scrollable Panel which has methods to allow you to control the scrollable properties. 一个简单的方法是使用可滚动面板 ,该面板具有允许您控制可滚动属性的方法。

The only solution that is apparent to me is to overwrite the GridLayout you use. 对我而言,唯一显而易见的解决方案是覆盖您使用的GridLayout。 Does not sound nice, but is actually done quite easily: 听起来不太好,但实际上很容易做到:

package scrollPaneAndBoxLayout;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;

public class MyGridLayout extends GridLayout {

    public MyGridLayout(int rows, int cols) {
        super(rows, cols);
    }

    public Dimension preferredLayoutSize(Container parent) {
        return new Dimension(parent.getWidth(), (int) super.preferredLayoutSize(parent).getHeight());
    }

}

To explain it, preferredLayoutSize() returns the size the whole component applying the Layout will have if possible (which it will be most of the cases). 为了解释这一点, preferredLayoutSize()返回应用Layout的整个组件的大小(如果可能)(在大多数情况下)。 I set the width of this to the width of the parent component, so the container in which you put your table, while the height is taken from the original GridLayout class. 我将其宽度设置为父组件的宽度,因此将表放在其中的容器的高度是从原始GridLayout类获取的。

Another solution would be to use a JTable instead of your JPanel-construction but when you already implemented it this way I doubt that it would be easier for you to revise the whole concept. 另一个解决方案是使用JTable而不是JPanel构造,但是当您已经以这种方式实现它时,我怀疑修改整个概念会更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM