我问this question我正在使用Nimbus外观和细胞渲染器查看的问题,问题可能与Scala有关。基本上我有一个扩展Panel
(而不是DefaultTableCellRenderer
)的单元格渲染器,它表现得很奇怪:它没有正确渲染备用行颜色而等效的Java程序表现得很好。如果有人有兴趣,可以运行一些 Scala 代码:
import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel
import java.awt.{Dimension, Color}
import java.lang.String
import javax.swing.table.{DefaultTableCellRenderer, AbstractTableModel, TableCellRenderer}
import javax.swing.{UIManager, JComponent, JLabel, JTable}
import swing.{Component, MainFrame, Label, BorderPanel, Panel, Table, ScrollPane, Frame, SimpleGUIApplication}
object SwingTest extends SimpleGUIApplication {
UIManager.setLookAndFeel(new NimbusLookAndFeel)
val tcr = new TCR
val dtcr = new DefaultTableCellRenderer
val t = new Table {
model = new AbstractTableModel {
def getColumnCount = 2
def getRowCount = 3
override def getColumnName(column: Int) = "Headings"
def getValueAt(rowIndex: Int, columnIndex: Int) = rowIndex match {
case 0 => "Hello"
case 1 => "World"
case 2 => "Again"
}
}
override protected def rendererComponent(isSelected: Boolean, hasFocus: Boolean, row: Int, column: Int) = {
if (column == 0)
Component.wrap(tcr.getTableCellRendererComponent(peer, model.getValueAt(row, column), isSelected, hasFocus, row, column).asInstanceOf[JComponent])
else
Component.wrap(dtcr.getTableCellRendererComponent(peer, model.getValueAt(row, column), isSelected, hasFocus, row, column).asInstanceOf[JComponent])
}
}
val top = new MainFrame {
title = "Test"
contents = new ScrollPane {
viewportView = t
}
size = new Dimension(300, 300)
}
class TCR extends BorderPanel with TableCellRenderer {
val label = new Label
label.foreground = Color.CYAN
add(label, BorderPanel.Position.Center)
def getTableCellRendererComponent(table: JTable, value: Any, isSelected: Boolean, hasFocus: Boolean, row: Int, column: Int) = {
label.text = String.valueOf(value)
if (isSelected)
background = table.getSelectionBackground
else {
println("row=%d, t_back=%s, t_alt=%s".format(row, table.getBackground, UIManager.getColor("Table.alternateRowColor")))
background = if (row % 2 == 0) UIManager.getColor("Table.alternateRowColor") else table.getBackground
}
peer
}
}
}
如果您运行代码,您将看到问题所在(它与Panel
中的备用行着色有关 - 渲染器无法正常工作)。如果运行Java等价物,您将看到它正常工作。任何人都知道为什么Scala代码不能按预期工作?这是等效的 Java 代码:
import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
public class SwingTest {
public static class StringCellRenderer extends JPanel implements TableCellRenderer {
private JLabel l1 = new JLabel();
public StringCellRenderer() {
setLayout(new BorderLayout());
l1.setForeground(Color.CYAN);
add(l1, BorderLayout.CENTER);
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
l1.setText(String.valueOf(value));
if (isSelected) {
setBackground(table.getSelectionBackground());
}
else {
if ( row % 2 == 0 ) {
setBackground(UIManager.getColor("Table.alternateRowColor"));
} else {
setBackground(table.getBackground());
}
}
return this;
}
}
public static void main(String[] args) throws UnsupportedLookAndFeelException {
JTable t = new JTable(new AbstractTableModel() {
public int getRowCount() {
return 3;
}
public int getColumnCount() {
return 2;
}
public Object getValueAt(int rowIndex, int columnIndex) {
switch (rowIndex) {
case 0:
return "Hello";
case 1:
return "World";
case 2:
return "Again";
default:
throw new IllegalArgumentException();
}
}
@Override
public String getColumnName(int column) {
return "Headings";
}
@Override
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == 0) {
return String.class;
} else {
return Object.class;
}
}
});
t.setDefaultRenderer(String.class, new StringCellRenderer());
t.setDefaultRenderer(Object.class, new DefaultTableCellRenderer());
UIManager.setLookAndFeel(new NimbusLookAndFeel());
JFrame f = new JFrame("Test");
f.setContentPane(new JScrollPane(t));
f.setSize(300, 300);
f.pack();
f.setVisible(true);
}
}
答案 0 :(得分:1)
我认为这可能是一个错误(感谢来自scala swing团队的 Ingo Meier ,以获取有关scala-users邮件列表的帮助)。它是在scala trac下提交的#2292