Question

I asked this question about a problem I was seing with a cell renderer using the Nimbus look and feel and the issue has turned out to be possibly to do with Scala. Basically I have a cell renderer which extends Panel (as opposed to DefaultTableCellRenderer) and it is behaving oddly: it is not rendering the alternate row colors properly whereas an equivalent Java program behaves just fine. If anyone is interested, here is some Scala code to run:

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
    }
  }
}

If you run the code you will see what the problem is (it's to do with the alternate row coloring in the Panel-renderer not working correctly). If you run a Java equivalent, you will see that it works normally. Anyone have any idea why the Scala code doesn't work as expected? Here is the equivalent Java code:

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);
  }
}
Was it helpful?

Solution

I think this may be a bug (thanks to Ingo Meier from the scala swing team for help on the scala-users mailing list). It's filed under scala trac as #2292

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top