我不明白,在雨云如何备用行着色的作品。 这似乎很疯狂! 我想在这里澄清一些事情。
为了演示,让我们说, 我们需要一个JTable交替红色和粉红色行 (我不关心它的颜色是第一个)。
而不重新定义执行自己的“模2”的事情定制cellRenderers,并没有从JTable中覆盖的任何方法,我想列出一个启动的应用程序时遇到一个JTable 只使用灵气属性自定义备用行颜色之间的强制性措施。
下面是我将遵循以下步骤:
- 安装雨云PLAF
- 自定义“Table.background”灵气属性
- 自定义“Table.alternateRowColor”灵气属性
- 创建具有简单的数据/报头一个JTable
- 包裹的JTable在JScrollPane中,并将其添加到JFrame
- 显示的JFrame
这里的源代码:
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代码。 有人能告诉我去错在这里?
按@克列奥帕特拉的评论和整个社会这里的/方式的贡献获得备用行只用雨云性能着色
公共类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);
}
}
貌似几个错误的干扰...
为了改变这两个默认的表背景和默认条带化,预期的(不仅是你,我的也一样)UIManager的配置(同样为这尊重alternateRow财产所有的LAF)将是:
UIManager.put("Table.background", Color.RED);
UIManager.put("Table.alternateRowColor", Color.PINK);
不行的,既不是金属,也不是雨云
- 在金属:无条纹,表是全红
- 在雨云:分带白/粉红色,这是表格的背景被忽略
第一个根本的原因可以在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);
这看上去很好(除了一个复选框渲染器的典型问题,但这是另一个错误的故事;-)金属,仍然没有运气的雨云。
下一步是查找雨云默认值 ,这可能是相关的,并应用(设置LAF了!):
UIManager.getLookAndFeelDefaults().put("Table:\"Table.cellRenderer\".background",
new ColorUIResource(Color.RED));
编辑 (因为它被要求在评论)
JXTable试图侧步完全的问题 - 它的装置,用于条纹是从HighlighterFactory检索的荧光笔。 需要通过从lookAndFeelDefaults的alternateRowColor财产去脏雨云,并用新的密钥将其添加“UIColorHighlighter.stripingBackground”
使用Nimbus的属性(+1到@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
设置为true
对MyTable
:
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;
}
}
参考文献:
- http://elliotth.blogspot.com/2004/09/alternating-row-colors-in-jtable.html
- JTable的条纹背景