0

我想实现一个带有滚动条的小工具提示,就像在类或成员上方悬停时出现的 eclipse 中的那样。

我遇到的问题是找到一种方法来限制我在工具提示中的滚动窗格中组件的宽度而不是高度。该组件应该支持 HTML,并且当文本超出内部边界的宽度时也可以正确包装文本,但是我尝试过的所有组件都具有换行或 HTML 呈现,但不是两者兼而有之

仅限制宽度的方法也无处可寻,因为每个“setXSize”,其中 X 是“首选”“最大”“最小”等都需要两个参数,并且组件没有“setMaxWidth”方法。

设置“setMaximumSize(new Dimension(256, Integer.MAX_VALUE);”似乎是一个解决方案,但它不起作用,因为“max”和“min”设置的参数大部分时间都被忽略,这非常令人沮丧。

根据要求提供当前的实施示例:

public class MyTooltip extends JTooltip{
    private JScrollPane scroll;
    private JEditorPane pane;

    public MyTooltip(String htmlCode){
       this.pane = new JEditorPane();
       this.scroll = new JScrollPane(this.pane);
       this.pane.setEditable(false);
       this.pane.setContentType("text/html");
       this.scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

       //Here the problems begin
       this.pane.setMaximumSize(new Dimension(512, Integer.MAX_VALUE));
       this.scroll.setMaximumSize(new Dimension(512, Integer.MAX_VALUE));
       this.pane.setText(htmlCode);

       this.add(scroll);
    }
}
  • 实际的代码有点复杂。但我认为这是一个很好的近似值......
4

2 回答 2

1

您是否尝试JTextPaneHTMLEditorKit(内容类型文本/html)?

我认为这就是你所需要的。

于 2013-03-14T12:10:27.230 回答
0

Ok, the whole problem just solved itself: One had the idea to let the user write his own texts to be displayed on the tooltips, but that included letting him use multiple "spaces" for indentation when he called for it.

To let HTML render this as intended we replaced every "space" with an & nbsp; so no optimization on gaps between characters would be performed, but this of course has the result that no algorithm for automatic line wrapping would accept any "gap" between words as a suitable place to break the line.

So our implementation actually works as intended, only the Strings we give the tooltip to display are not suitable to be line broken.

于 2013-03-14T20:56:10.257 回答