如何在JTable的单元格中添加类似于工具提示的气球提示?

3

我想在JTableCell中添加类似于Tooltips的气球提示(Balloon Tips)。也就是说,当鼠标进入一个Cell时,它会出现,并在一段时间后消失 (与Tooltips相同,但不是Tooltip)。我尝试过这个方法,但并没有按照我的意愿工作。

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

    final JLabel lable = new JLabel(value.toString());

    EdgedBalloonStyle style = new EdgedBalloonStyle(new Color(255, 253, 245),
            new Color(64, 64, 64));
    BalloonTip tooltipBalloon = new BalloonTip(lable, new JLabel(value.toString()), style, new LeftAbovePositioner(15, 10), null);
    ToolTipUtils.balloonToToolTip(tooltipBalloon, 400, 2000);

    return lable;
}

这个没有任何效果。 同时我尝试了这个。

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

    final JLabel lable = new JLabel(value.toString());

    EdgedBalloonStyle style = new EdgedBalloonStyle(new Color(255, 253, 245), new Color(64, 64, 64));
    TablecellBalloonTip tcb = new TablecellBalloonTip(table, new JLabel(value.toString()),
            row, column, style, BalloonTip.Orientation.LEFT_ABOVE,
            BalloonTip.AttachLocation.ALIGNED, 30, 10, false);

    return lable;
}

这只能作为气球提示的工作,不是我想要的。有什么建议吗?

1个回答

1
我认为问题在于您将气球提示附加到新创建的JLabel上...尝试将其添加到您的渲染单元组件(renderedCellComponent)上:
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
    boolean isSelected, boolean hasFocus, int row, int column) {

    final JLabel lable = new JLabel(value.toString());

    EdgedBalloonStyle style = new EdgedBalloonStyle(new Color(255, 253, 245), new Color(64, 64, 64));

    //look, here is your mistake: you append it onto a new JLabel
    //TablecellBalloonTip tcb = new TablecellBalloonTip(table, 
    //     new JLabel(value.toString()), row, column, style, 
    //      BalloonTip.Orientation.LEFT_ABOVE,
    //      BalloonTip.AttachLocation.ALIGNED, 30, 10, false);

    //instead append it on your rendered Component
    TablecellBalloonTip tcb = new TablecellBalloonTip(table, 
        lable, // !!here!!
        row, column, style, BalloonTip.Orientation.LEFT_ABOVE,
        BalloonTip.AttachLocation.ALIGNED, 30, 10, false);

    return lable;
}

我希望这能行…

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接