gpt4 book ai didi

java - 是否可以使用 JXBusyLabel 作为在 JTable 中呈现的单元格?

转载 作者:搜寻专家 更新时间:2023-11-01 03:12:49 26 4
gpt4 key购买 nike

我想在单元格中使用 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;
}

};

关于java - 是否可以使用 JXBusyLabel 作为在 JTable 中呈现的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6065595/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com