3

我问了这个问题,关于我在使用 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-renderer 中的备用行着色无法正常工作有关)。如果您运行 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);
  }
}
4

1 回答 1

1

我认为这可能是一个错误(感谢scala swing 团队的Ingo Meier对 scala-users 邮件列表的帮助)。它在 scala trac 下归档为 #2292

于 2009-08-25T12:30:57.897 回答