Question

I saw the BallonTip at java.net and I tried to integrate it into my application to be displayed when a user clicks a table cell. When a table cell is clicked, the BalloonTip is showing up as intended, but when you scroll it out of the current viewport, you can click another cell without the BalloonTip showing up. When you scroll the table, the BallonTip is shown again.

Here is an example:

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();
        }
    }
}
}

How can I force the BalloonTip to be shown after I click a cell in the table? I think there is a listener, which is listening for the scrolling event and manages the painting of the BallonTip, but I do not have a clue which one it is.

best regards htz

Was it helpful?

Solution

According to this mailing list, there was a bug in the BallonTip version 1.2.1. Now, with version 1.2.3, this is fixed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top