使用Nimbus和Scala的表格单元格渲染器

编程入门 行业动态 更新时间:2024-10-27 16:38:10
本文介绍了使用Nimbus和Scala的表格单元格渲染器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我问了这个问题我当时正在使用使用Nimbus外观的单元格渲染器,结果发现问题可能与Scala有关.基本上,我有一个扩展了Panel(而不是DefaultTableCellRenderer)的单元格渲染器,它的行为很奇怪:它不能正确渲染备用行的颜色,而等效的Java程序的表现就很好.如果有人感兴趣,请运行以下 Scala 代码:

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

如果运行代码,您将看到问题所在(这与Panel -renderer中的替代行着色不正确有关).如果运行等效的Java,您将看到它正常工作.任何人都知道为什么Scala代码无法按预期工作吗?以下是等效的 Java 代码:

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

推荐答案

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

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

更多推荐

使用Nimbus和Scala的表格单元格渲染器

本文发布于:2023-11-23 02:04:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619697.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元格   表格   渲染器   Nimbus   Scala

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!