Java 是否可以将JXBusyLabel用作JTable中呈现的单元格?
我想在单元格中使用Java 是否可以将JXBusyLabel用作JTable中呈现的单元格?,java,user-interface,swingx,Java,User Interface,Swingx,我想在单元格中使用JXBusyLabel来通知用户当前正在发生JXBusyLabel所在行的事件。 例如,双击一行以打开它将触发JXBusyLabel的任意信息 这有意义吗 如果您想知道什么是JXBusyLabel,请查看 谢谢 [编辑]基于@kleopatra answer的解决方案: import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.
JXBusyLabel
来通知用户当前正在发生JXBusyLabel
所在行的事件。
例如,双击一行以打开它将触发JXBusyLabel
的任意信息
这有意义吗
如果您想知道什么是JXBusyLabel
,请查看
谢谢
[编辑]基于@kleopatra answer的解决方案:
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();
}
}
};
}
}
是的,这是可能的。我在桌子上也用过这个。对于延迟加载,看起来非常酷
private class YourCellRenderer implements TableCellRenderer {
private final JLabel okLabel;
private final JXBusyLabel busyLabel;
public YourCellRenderer() {
busyLabel = new JXBusyLabel(new Dimension(50, 50));
busyLabel.setHorizontalAlignment(JXLabel.CENTER);
busyLabel.setBusy(true);
okLabel = new JLabel();
}
@Override
public Component getTableCellRendererComponent(final JTable table,
final Object value, final boolean isSelected, final boolean hasFocus,
final int row, final int column) {
if (((YourClass)value).isBusy()) {
return busyLabel;
} else {
okLabel.set(((YourClass)value).getText());
return okLabel;
}
return null;
}
将新渲染器设置为类的默认渲染器:
table.setDefaultRenderer(YourClass.class, new YourCellRenderer());
当然,您的表模型需要返回类的实例。根据具体的用例,您不希望使用JXBusyLabel作为渲染组件(由于它是渲染器,因此无法获得anitmation),您所需要的只是一个配置了BusyPaint的PainterHighlighter,BusyPaint的frame属性由计时器控制。画师是否可见应该绑定到数据的某个属性,该属性触发highlighters HighlightPredicate的打开/关闭 例如,请参见swingx测试层次结构包渲染器中的PaintServiceSualCheck 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();
}
编辑(回答扩展问题):
要仅在特定条件下激活忙碌的highlighter,请实现一个自定义HighlightPredicate并将其设置为而不是“始终”。F.i.列中的特定行:
HighlightPredicate predicate = new HighlightPredicate() {
@Override
public boolean isHighlighted(Component renderer,
ComponentAdapter adapter) {
return
adapter.convertRowIndexToModel(adapter.row) == mySpecialRow;
}
};
请看这篇文章@I82Much-1链接到一个完全错误的方法如果我没记错的话,swinglabs demos(由于java.net的转换问题,目前没有可用的webstartable,叹气…)有一个可能的例子,但缺少动画(至少;-)这很好,但是如果没有动画,那么它就是一个悲伤的
JXBusyLabel
;(谢谢@Stephan的回答!:)酷,这个有用:)谢谢!还有一个问题,我如何指定它来影响表中的特定单元格而不是整个列?我已经编辑了我的主要帖子,其中包含了我用来实现这一点的示例代码。顺便说一句,很抱歉延迟了我的反馈:)