gpt4 book ai didi

scala - 使用Nimbus和Scala的Table Cell渲染器

转载 作者:行者123 更新时间:2023-12-04 17:54:21 24 4
gpt4 key购买 nike

我问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 -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);
}
}

最佳答案

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

关于scala - 使用Nimbus和Scala的Table Cell渲染器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1326763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com