我想在单元格中使用 JXBusyLabel 来通知用户当前正在为 JXBusyLabel 所在的行发生事件。
例如,双击一行打开它会触发 JXBusyLabel 的任意化。

这有意义吗?

如果您想知道 JXBusyLabel 是什么,请查看 here

谢谢!

[编辑] 基于@kleopatra 回答 的解决方案:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;

import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.AbstractHighlighter;
import org.jdesktop.swingx.decorator.HighlightPredicate;
import org.jdesktop.swingx.decorator.PainterHighlighter;
import org.jdesktop.swingx.painter.BusyPainter;

public class TableBusyLabelTest {

    private JFrame frame;
    private JXTable table;
    private JButton button;

    private BusyPainter busyPainter;
    private AbstractHighlighter highlighter;
    private Timer timer;

    private boolean on = false;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    TableBusyLabelTest window = new TableBusyLabelTest();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public TableBusyLabelTest() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        busyPainter = new BusyPainter(15);

        timer = new Timer(100, getTimerActionListener());

        highlighter = new PainterHighlighter(HighlightPredicate.NEVER,
                busyPainter);

        table = new JXTable();
        table.setModel(getTableExampleModel());
        // Tell which column will use the highlighter
        table.getColumnExt(0).addHighlighter(highlighter);

        button = new JButton("Start / Stop busy thing");
        button.addActionListener(getButtonActionListener());

        frame.getContentPane().add(table, BorderLayout.CENTER);
        frame.getContentPane().add(button, BorderLayout.SOUTH);
    }

    private DefaultTableModel getTableExampleModel() {
        return new DefaultTableModel(new Object[][] { { null, "Test1" },
                { null, "Test2" }, }, new String[] { "busy", "Name" });
    }

    private ActionListener getTimerActionListener() {
        return new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int frame = busyPainter.getFrame();
                frame = (frame + 1) % busyPainter.getPoints();
                busyPainter.setFrame(frame);
            }

        };
    }

    private ActionListener getButtonActionListener() {
        return new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if (on) {
                    on = false;
                } else {
                    on = true;
                }

                // on a change that should toggle the busy-ness on/off
                if (on) {
                    highlighter
                            .setHighlightPredicate(HighlightPredicate.ALWAYS);
                    timer.start();
                } else {
                    highlighter.setHighlightPredicate(HighlightPredicate.NEVER);
                    timer.stop();
                }
            }
        };
    }

}

最佳答案

根据您的具体用例,您不希望使用 JXBusyLabel 作为渲染组件(您不会获得动画,因为它是一个渲染器),您只需要一个 PainterHighlighter 配置一个 BusyPainter,其 frame 属性受到控制通过计时器。画家是否可见应该绑定(bind)到数据的某些属性,这些属性会触发荧光笔 HighlightPredicate 的开/关。

示例请参见 Swingx 测试层次结构中的 PainterVisualCheck interactiveAnimatedBusyPainterHighlight,包渲染器。就像是:

    BusyPainter painter = new BusyPainter();
    ActionListener l = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int frame = busyPainter.getFrame();
            frame = (frame+1)%busyPainter.getPoints();
            busyPainter.setFrame(frame);
        }

    };
    Timer timer = new Timer(delay, l);
    AbstractHighlighter hl = new PainterHighlighter(HighlightPredicate.NEVER, painter);
    table.getColumnExt().addHighlighter(hl);

    // on a change that should toggle the busy-ness on/off
    if (on) {
         hl.setHighlightPredicate(HighlightPredicate.ALWAYS);
         timer.start();
    } else {
         hl.setHighlightPredicate(HighlightPredicate.NEVER);
         timer.stop();
    }

编辑(回答扩展问题):

要仅针对特定条件激活繁忙的高亮器,请实现自定义 HighlightPredicate 并设置它而不是 ALWAYS。 F.i.列中的特定行:
    HighlightPredicate predicate = new HighlightPredicate() {

        @Override
        public boolean isHighlighted(Component renderer,
                ComponentAdapter adapter) {

            return
               adapter.convertRowIndexToModel(adapter.row) == mySpecialRow;
        }

    };

10-06 07:00