简体   繁体   English

JTable单元内的滚动条

[英]Scrollbars inside a JTable cell

I am trying to display HTML text inside a JTable's cell but the scrollbars arent showing up at all. 我试图在JTable的单元格中显示HTML文本,但滚动条没有显示出来。 Below is my code... 下面是我的代码...

public class TableCellTextRenderer {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,     
        boolean hasFocus, int row, int column) {

        final JTextPane jtextPane = new JTextPane();
        JScrollPane scrollPane = new JScrollPane(jtextPane);
        scrollPane.setPreferredSize(new Dimension(350,350));
        scrollPane.setVisible(true);
        jtextPane.setEditable(false);
        jtextPane.setAutoscrolls(true);
        jtextPane.setContentType("text/html");
        jtextPane.setText(myHtmlText);
        jtextPane.setVisible(true);
        jtextPane.setPreferredSize(new Dimension(350,350));

        //this setViewPort has no effect 
        scrollPane.setViewportView(jtextPane);

        jtextPane.setVisible(true);
        scrollPane.setAutoscrolls(true);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        JPanel jpanel = new JPanel();
        jpanel.setVisible(true);
        jpanel.setPreferredSize(new Dimension(360, 360));
        jpanel.add(scrollPane);

        return jpanel;
    }
}

The scroll bars appear, but the scrolling handles dont appear. 出现滚动条,但不出现滚动手柄。 Can you please tell me what I am missing? 你能告诉我我在想什么吗?

The table looks like this: 该表如下所示:

jTable的第一行

I'm afraid this is not going to work the way you'd want it to. 恐怕这将无法按您希望的方式工作。 The way JTables work is by obtaining a renderer component to draw the cell once, then cache it. JTables的工作方式是获得渲染器组件以绘制一次单元,然后对其进行缓存。 In simplest terms, the component you're returning is basically just used to draw the cell, but it does not work the way normal components do - for instance, it does not receive events. 用最简单的术语来说,您要返回的组件基本上仅用于绘制单元格,但它不能像普通组件那样工作-例如,它不接收事件。 You can use a cell editor (which will receive events), but the user would have to select the cell manually. 您可以使用单元格编辑器(它将接收事件),但是用户必须手动选择单元格。

There are a couple of tricks you can use by installing custom mouse listeners, but this stuff will get very complicated. 通过安装自定义鼠标侦听器,可以使用一些技巧,但是这些东西将变得非常复杂。 It'd probably be better to ask yourself if you're really looking for a JTable (as opposed to a custom layout), or if it would perhaps be possible to embed a JavaFX table. 最好问一下自己,是否真的要寻找JTable(而不是自定义布局),或者是否有可能嵌入JavaFX表。

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

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