我不明白在Nimbus中交替行着色是如何工作的。简直太疯狂了!我想把这里的事情弄清楚。
对于演示,假设是,我们需要一个JTable,它可以交替使用红色和粉色行(而且我不在乎第一个颜色是哪种颜色)。
不需要重新定义执行自己的“模块化2”操作的自定义cellRenderers,也不需要重写JTable的任何方法,我想列出启动应用程序和使用Nimbus属性(仅使用)获得具有自定义备选行颜色的JTable之间的强制步骤。
以下是我希望遵循的步骤:
在这里,源代码:
public class JTableAlternateRowColors implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new JTableAlternateRowColors());
}
@Override
public void run() {
try {
UIManager.setLookAndFeel(new NimbusLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
UIManager.getDefaults().put("Table.background", Color.RED);
UIManager.getDefaults().put("Table.alternateRowColor", Color.PINK);
final JFrame jFrame = new JFrame("Nimbus alternate row coloring");
jFrame.getContentPane().add(new JScrollPane(new JTable(new String[][] {
{"one","two","three"},
{"one","two","three"},
{"one","two","three"}
}, new String[]{"col1", "col2", "col3"}
)));
jFrame.setSize(400, 300);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}这是JDK6代码。有人能告诉我这里出了什么问题吗?
正如@kleopatra的评论和整个社区的贡献一样,这里提供了一种只使用Nimbus属性实现行替换着色的方法
公共类JTableAlternateRowColors实现Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new JTableAlternateRowColors());
}
@Override
public void run() {
try {
UIManager.setLookAndFeel(new NimbusLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
UIManager.put("Table.background", new ColorUIResource(Color.RED));
UIManager.put("Table.alternateRowColor", Color.PINK);
UIManager.getLookAndFeelDefaults().put("Table:\"Table.cellRenderer\".background", new ColorUIResource(Color.RED));
final JFrame jFrame = new JFrame("Nimbus alternate row coloring");
final JTable jTable = new JTable(new String[][]{
{"one", "two", "three"},
{"one", "two", "three"},
{"one", "two", "three"}
}, new String[]{"col1", "col2", "col3"});
jTable.setFillsViewportHeight(true);
jFrame.getContentPane().add(new JScrollPane(jTable));
jFrame.setSize(400, 300);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}}
发布于 2012-10-22 13:29:15
看上去是几个虫子的干扰。
要同时更改默认表背景和默认条带,UIManager的预期配置(不仅是您的配置,我的配置)(对于所有尊重alternateRow属性的LAF都是一样的)将是:
UIManager.put("Table.background", Color.RED);
UIManager.put("Table.alternateRowColor", Color.PINK);对金属和Nimbus都不起作用
第一个原因可以在DefaultTableCellRenderer中找到:
Color background = unselectedBackground != null
? unselectedBackground
: table.getBackground();
if (background == null || background instanceof javax.swing.plaf.UIResource) {
Color alternateColor = DefaultLookup.getColor(this, ui, "Table.alternateRowColor");
if (alternateColor != null && row % 2 != 0) {
background = alternateColor;
}
}它的逻辑是不正确的:只有当表的背景是colorUIResource时,才会选择另一种颜色,这是一个相当弱的区别。总之,这将导致我们下一次尝试:
UIManager.put("Table.background", new ColorUIResource(Color.RED));
UIManager.put("Table.alternateRowColor", Color.PINK);这看起来很好(除了复选框呈现器的典型问题,但这是另一个错误故事;-)对于金属,仍然没有运气的Nimbus。
下一步是查找可能相关的Nimbus缺省值,并应用(在后面!设置黎巴嫩武装部队):
UIManager.getLookAndFeelDefaults().put("Table:\"Table.cellRenderer\".background",
new ColorUIResource(Color.RED));编辑(如评论中所要求的)
JXTable试图完全回避这个问题--它的条纹方法是从HighlighterFactory中检索到的荧光笔。需要从alternateRowColor属性中移除lookAndFeelDefaults属性,并使用新的键"UIColorHighlighter.stripingBackground“添加Nimbus
发布于 2012-10-22 11:21:11
使用Nimbus属性(+1 to @Kleopatra )证明我错了:()您可以通过
UIManager.put("Table.alternateRowColor", Color.PINK);
UIManager.getLookAndFeelDefaults().put("Table:\"Table.cellRenderer\".background", Color.RED);另一办法是:
扩展JTable并重写prepareRenderer(TableCellRenderer renderer, int row, int column),以便绘制所需的颜色(红色和粉红色)。
下面是一个简短的例子,我希望它能有所帮助。
额外特性:还重写了paintComponent(..),后者将调用paintEmptyRows(Graphics g),后者将为JScrollPane视图的整个高度和宽度绘制行,但这仅在setFillsViewPortHeight设置为MyTable上的true时才适用。

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import javax.swing.table.TableCellRenderer;
public class JTableAlternateRowColors {
public JTableAlternateRowColors() {
initComponents();
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(new NimbusLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JTableAlternateRowColors();
}
});
}
private void initComponents() {
final JFrame jFrame = new JFrame("Nimbus alternate row coloring");
MyTable table = new MyTable(new String[][]{
{"one", "two", "three"},
{"one", "two", "three"},
{"one", "two", "three"}
}, new String[]{"col1", "col2", "col3"});
table.setFillsViewportHeight(true);//will fill the empty spaces too if any
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane jScrollPane = new JScrollPane(table);
jFrame.getContentPane().add(jScrollPane);
jFrame.pack();
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}
class MyTable extends JTable {
public MyTable(String[][] data, String[] fields) {
super(data, fields);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (getFillsViewportHeight()) {
paintEmptyRows(g);
}
}
/**
* Paints the backgrounds of the implied empty rows when the table model is
* insufficient to fill all the visible area available to us. We don't
* involve cell renderers, because we have no data.
*/
protected void paintEmptyRows(Graphics g) {
final int rowCount = getRowCount();
final Rectangle clip = g.getClipBounds();
if (rowCount * rowHeight < clip.height) {
for (int i = rowCount; i <= clip.height / rowHeight; ++i) {
g.setColor(colorForRow(i));
g.fillRect(clip.x, i * rowHeight, clip.width, rowHeight);
}
}
}
/**
* Returns the appropriate background color for the given row.
*/
protected Color colorForRow(int row) {
return (row % 2 == 0) ? Color.RED : Color.PINK;
}
/**
* Shades alternate rows in different colors.
*/
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if (isCellSelected(row, column) == false) {
c.setBackground(colorForRow(row));
c.setForeground(UIManager.getColor("Table.foreground"));
} else {
c.setBackground(UIManager.getColor("Table.selectionBackground"));
c.setForeground(UIManager.getColor("Table.selectionForeground"));
}
return c;
}
}参考资料:
https://stackoverflow.com/questions/13008241
复制相似问题