我正在使用JXTreeTable来显示一些数据,我想使用提供的SwingX机制来更改某些列的渲染器。
我以前使用了TableCellRenderer的JXTable和自定义实现,但这不再起作用了(我看到字符串,我应该有进度条,按钮,......)。
因此,我努力实现我的目标:
examsTable.getColumn(6).setCellRenderer(new DefaultTableRenderer(new ButtonProvider()));
但ComponentProvider的重写方法createRenderer
被调用一次(即使我的JXTreeTable中有多行)并且没有显示任何按钮(该方法只包含return new JButton();
)。
谢谢!
编辑>希望你能回答kleopatra,因此很开心。
我尽了最大努力但不知何故表格没有显示。我想我忘了一些东西(我是Java新手,但我认为这不是一个严重的问题,而且可能与我的主要问题无关。
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);
}
}
编辑&gt; 我实际上已经发现它有效。但不是我想要的方式。我想看一个真正的按钮(现在它看起来与线的其余部分略有不同),知道它的路径(父对象和列索引)并查看点击或悬停在其上的效果(按钮应该看起来按下,...)。
我如何实现这一目标?
答案 0 :(得分:4)
使用该按钮,它似乎只出现在某些LAF中: - )
从技术上讲,原因是
组合时,按钮看起来不像Metal的按钮(在Windows中它没有变化f.i.)
没有令人满意的解决方案,选项