java.net BalloonTip 未显示

标签 java swing jtable tooltip listselectionlistener

我看到了BallonTip在 java.net 上,我尝试将其集成到我的应用程序中,以便在用户单击表格单元格时显示。单击表格单元格时,气球提示会按预期显示,但是当您将其滚动到当前视口(viewport)之外时,您可以单击另一个单元格而不显示气球提示。当您滚动表格时,BallonTip 会再次显示。

这是一个例子:

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import net.java.balloontip.BalloonTip;
import net.java.balloontip.TablecellBalloonTip;
import net.java.balloontip.styles.EdgedBalloonStyle;

public class TableTest2 extends JFrame {

static final int LENGTH = 40;
TablecellBalloonTip tip;
JTable mainTable;
JPanel main;
JLayeredPane layeredPane;
JScrollPane mainScroll;

TableTest2() {
    mainTable = new JTable(LENGTH, LENGTH);
    CustomListSelectionListener clsl = new CustomListSelectionListener(mainTable);
    mainTable.getColumnModel().getSelectionModel().addListSelectionListener(clsl);
    mainTable.getSelectionModel().addListSelectionListener(clsl);
    mainTable.setTableHeader(null);
    mainTable.setColumnSelectionAllowed(true);
    mainScroll = new JScrollPane(mainTable);
    add(mainScroll);

    tip = new TablecellBalloonTip(mainTable, new JLabel("Hello World!"), -1, -1, new EdgedBalloonStyle(Color.WHITE,
            Color.BLUE), BalloonTip.Orientation.LEFT_ABOVE, BalloonTip.AttachLocation.ALIGNED, 5, 5, false);

    setPreferredSize(new Dimension(500, 400));
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();

}

public static void main(String[] args) {
    new TableTest2();
}

protected class CustomListSelectionListener implements ListSelectionListener {
    private int row, column, lead, anchor;
    private JTable table;

    public CustomListSelectionListener(JTable table) {
        this.table = table;
    }

    @Override
    public void valueChanged(ListSelectionEvent evt) {
        if (evt.getSource() == table.getSelectionModel() && table.getRowSelectionAllowed()) {
            // row selection changed
            row = table.getSelectedRow();
            column = table.getSelectedColumn();
            tip.setCellPosition(row, column);
            tip.refreshLocation();
        } else if (evt.getSource() == table.getColumnModel().getSelectionModel()
                && table.getColumnSelectionAllowed()) {
            // column selection changed
            lead = table.getColumnModel().getSelectionModel().getLeadSelectionIndex();
            anchor = table.getColumnModel().getSelectionModel().getAnchorSelectionIndex();
            if (lead <= anchor) {
                column = anchor;
            } else {
                column = lead;
            }
            row = table.getSelectedRow();
            tip.setCellPosition(row, column);
            tip.refreshLocation();
        }
    }
}
}

如何在单击表格中的单元格后强制显示 BalloonTip?我认为有一个监听器,它正在监听滚动事件并管理 BallonTip 的绘制,但我不知道它是哪一个。

最诚挚的问候 赫兹

最佳答案

根据to this mailing list ,BallonTip 1.2.1 版本中存在一个错误。现在,在 1.2.3 版本中,这个问题已得到修复。

关于java.net BalloonTip 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13010397/

相关文章:

java - Jframe setDefaultCloseOperation 不起作用

java - 在 JTable 中,如何呈现带有脏标记的复选框列?

java - 父子结构中的resty-gwt太多递归异常

java - 使用 NetBeans 和 Maven 进行增量构建以进行 jetty 热部署

java - 无法捕获 SSLException - Java

java - Apache Airflow 任务 : java: command not found

java - Java中如何使用swing Timer更好的实现动画

java - 在运行时更改 FormLayout 对齐方式

java - 如何在 JTable 中创建带有下拉菜单的额外第一行?

java - 如何编写自定义 DefaultTableCellRenderer 为特定单元格着色, "preserve"为其他单元格着色,Java