我正在使用 JXTreeTable 来显示一些数据,并且我想使用 SwingX 提供的机制来更改某些列的渲染器。
我以前使用过 JXTable 和 TableCellRenderer 的自定义实现,但这不再起作用(我看到应该有进度条、按钮等的字符串)。
因此,我试图通过以下方式实现我想要的:
examsTable.getColumn(6).setCellRenderer(new DefaultTableRenderer(new ButtonProvider()));
但是 ComponentProvider 的覆盖方法createRenderer
被调用一次(即使我的 JXTreeTable 中有不止一行)并且没有显示任何按钮(该方法仅包含return new JButton();
)。
谢谢!
编辑>希望你能回答 kleopatra,因此你很高兴。
我尽了最大努力,但不知何故没有显示表格。我想我忘记了一些东西(我是一名刚接触 Java 的 C++ 开发人员),但我想这不是一个严重的问题,它可能与我的主要问题无关。
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.decorator.HighlighterFactory;
import org.jdesktop.swingx.renderer.CellContext;
import org.jdesktop.swingx.renderer.ComponentProvider;
import org.jdesktop.swingx.renderer.DefaultTableRenderer;
import org.jdesktop.swingx.treetable.AbstractTreeTableModel;
public class Test extends JFrame {
JXTreeTable table = new JXTreeTable();
JPanel panel = new JPanel();
public Test() {
setMinimumSize(new Dimension(400, 400));
table.setEditable(false);
table.setDragEnabled(false);
table.setColumnSelectionAllowed(false);
table.setHighlighters(HighlighterFactory.createAlternateStriping());
table.setRowHeight(20);
table.setMinimumSize(new Dimension(200, 200));
class Series {
public String seriesInstanceUID;
public String patientName;
public String patientBirthDate;
public String securityToken;
public Series(String seriesInstanceUID, String patientName, String patientBirthDate, String securityToken) {
this.seriesInstanceUID = seriesInstanceUID;
this.patientName = patientName;
this.patientBirthDate = patientBirthDate;
this.securityToken = securityToken;
}
}
class Study {
public List<Series> series = new ArrayList<Series>();
}
class Root {
public List<Study> studies = new ArrayList<Study>();
}
AbstractTreeTableModel model = new AbstractTreeTableModel() {
Root root = new Root() {
{
studies.add(new Study() {
{
series.add(new Series("Series 1.1", "Mr. X", "1988-10-23", "sec-xx-1"));
series.add(new Series("Series 1.2", "Mr. X", "1988-10-23", "sec-xx-2"));
series.add(new Series("Series 1.3", "Mr. X", "1988-10-23", "sec-xx-3"));
}
});
studies.add(new Study() {
{
series.add(new Series("Series 2.1", "Mrs. Y", "1960-02-11", "sec-yy-1"));
}
});
studies.add(new Study() {
{
series.add(new Series("Series 3.1", "HAL 9000", "1975-04-21", "sec-zz-1"));
series.add(new Series("Series 3.2", "HAL 9000", "1975-04-21", "sec-zz-2"));
}
});
}
};
@Override
public String getColumnName(int columnIndex) {
switch (columnIndex) {
case 0:
return "Series Instance UID";
case 1:
return "Patient Name";
case 2:
return "Patient Birth Date";
case 3:
return "View online";
default:
return "";
}
}
@Override
public int getIndexOfChild(Object parent, Object child) {
if (parent == root) {
return root.studies.indexOf(child);
}
if (parent instanceof Study) {
Study study = (Study) parent;
return study.series.indexOf(child);
}
return -1;
}
@Override
public int getChildCount(Object parent) {
if (parent == root) {
return root.studies.size();
}
if (parent instanceof Study) {
Study study = (Study) parent;
return study.series.size();
}
return 0;
}
@Override
public Object getChild(Object parent, int index) {
if (parent == root) {
return root.studies.get(index);
}
if (parent instanceof Study) {
Study study = (Study) parent;
return study.series.get(index);
}
return null;
}
@Override
public Object getValueAt(Object node, int columnIndex) {
if (!(node instanceof Series) && !(node instanceof Study))
return null;
if (columnIndex < 0 || columnIndex >= getColumnCount())
return null;
if (root == null)
return null;
if (node instanceof Series) {
Series series = (Series) node;
if (columnIndex == 0)
return series.seriesInstanceUID;
else if (columnIndex == 1)
return series.patientName;
else if (columnIndex == 2)
return series.patientBirthDate;
else if (columnIndex == 3)
return series.securityToken;
} else if (node instanceof Study) {
// Empty for now
}
return null;
}
@Override
public int getColumnCount() {
return 4;
}
@Override
public Object getRoot() {
return root;
}
public void update() {
modelSupport.fireNewRoot();
}
};
table.setTreeTableModel(model);
table.getColumnModel().getColumn(3).setCellRenderer(new DefaultTableRenderer(new ComponentProvider<JButton>() {
{
rendererComponent.setHorizontalAlignment(JButton.CENTER);
}
@Override
protected void format(CellContext context) {
rendererComponent.setText(getValueAsString(context));
}
@Override
protected void configureState(CellContext context) {
rendererComponent.setHorizontalAlignment(getHorizontalAlignment());
}
@Override
protected JButton createRendererComponent() {
return new JButton("View online");
}
}));
panel.add(table);
this.setContentPane(panel);
}
public static void main(String[] args) {
(new Test()).setVisible(true);
}
}
编辑>我实际上已经观察到它有效。但不是我想要的方式。我想看到一个真正的按钮(现在它看起来与该行的其余部分有点不同),知道它的路径(父对象和列索引)并查看单击或悬停在它上时的效果(按钮应该看起来被按下,...)。
我该如何做到这一点?