【发布时间】:2011-05-19 22:35:11
【问题描述】:
我想在单元格中使用JXBusyLabel 来通知用户JXBusyLabel 所在行当前正在发生事件。
例如,双击一行打开它会触发JXBusyLabel 的anymation。
这有意义吗?
如果您想知道JXBusyLabel 是什么,请查看here。
谢谢!
[EDIT] 基于@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();
}
}
};
}
}
【问题讨论】:
-
@I82Much -1 链接到一个完全错误的方法
-
如果我没记错的话,swinglabs-demos(目前没有可用的 webstartable,由于 java.net 的转换问题,叹息......)有一个例子
标签: java user-interface swingx