更改 JTable 非内容背景颜色
Change JTable non Content background color
我正在编写一个自定义主题,它是 NimbusLookAndFeel
的扩展。
我想像
一样重新设计 table 主题
我的Table主题代码
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import javax.swing.Painter;
import infotrax.nimbus.NimbusBaseUI;
public class TableTheme {
protected int strokeSize = 2; // changed to 2 from 1 to fill the gap in corners
protected Color shadowColor = new Color(128, 128, 128, 140);
protected boolean shady = true;
protected boolean highQuality = false;
/** Double values for Horizontal and Vertical radius of corner arcs */
protected Dimension arcs = new Dimension(10, 10);
/** Distance between shadow border and opaque panel border */
protected int shadowGap = 1;
/** The offset of shadow. */
protected int shadowOffset = 1; // width of the shadow
/** The transparency value of shadow. ( 0 - 255) */
protected int shadowAlpha = 130;
public TableTheme(NimbusBaseUI nimbusUI) {
Insets ins = new Insets(2,2,2,2);
nimbusUI.getDefaults().put("Table.contentMargins",ins
);
nimbusUI.getDefaults().put("Table.background",
new Color(255, 255, 255)); // Modified to set background color of table to be white 01/08/2016
nimbusUI.getDefaults().put(
"Table.borderPainter",
new TableBorderPaintGradient(new Color(255, 255, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put(
"Table[Enabled].borderPainter",
new TableBorderPaintGradient(new Color(255, 255, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put(
"Table[Enabled+Selected].borderPainter",
new TableBorderPaintGradient(new Color(255, 255, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put(
"Table[Selected].borderPainter",
new TableBorderPaintGradient(new Color(255, 255, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put(
"Table[Focused].borderPainter",
new TableBorderPaintGradient(new Color(255, 255, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put(
"Table[Disabled].borderPainter",
new TableBorderPaintGradient(new Color(255, 255, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put("TableHeader.font",
new Font("Myriad Pro Light", Font.BOLD, 13));
// Table Header color
nimbusUI.getDefaults()
.put("TableHeader:\"TableHeader.renderer\"[MouseOver].backgroundPainter",
new TableheaderPainter(new Color(188,188,188),
new Color(188,188,188)));
nimbusUI.getDefaults()
.put("TableHeader:\"TableHeader.renderer\"[Enabled].backgroundPainter",
new TableheaderPainter(new Color(188,188,188),
new Color(188,188,188)));
}
public class TableheaderPainter implements Painter {
private static final long serialVersionUID = 1L;
private Color light, dark;
private GradientPaint gradPaint;
public TableheaderPainter(Color light, Color dark) {
this.light = light;
this.dark = dark;
}
@Override
public void paint(Graphics2D paramGraphics2D, Object paramT,
int paramInt1, int paramInt2) {
paramGraphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
gradPaint = new GradientPaint((paramInt1 / 2.0f), 0, light,
(paramInt1 / 2.0f), (paramInt2 / 2.0f), dark, true);
paramGraphics2D.setPaint(gradPaint);
paramGraphics2D.fillRoundRect(0, 0, (paramInt1 - 0),
(paramInt2 - 0), 0, 0);
}
}
public class Tablebackground_Painter implements Painter {
private Color light, dark;
private GradientPaint gradPaint;
int shadowGap = 2;
public Tablebackground_Painter(Color light, Color dark) {
this.light = light;
this.dark = dark;
}
@Override
public void paint(Graphics2D g, Object c, int w, int h) {
Color shadowColorA = new Color(shadowColor.getRed(),
shadowColor.getGreen(), shadowColor.getBlue(), shadowAlpha);
if (highQuality) {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
if (shady) {
g.setColor(shadowColorA);
g.fillRoundRect(shadowOffset,// X position
shadowOffset,// Y position
w - strokeSize - shadowOffset, // width
h - strokeSize - shadowOffset, // height
arcs.width, arcs.height);// arc Dimension
} else {
shadowGap = 1;
}
gradPaint = new GradientPaint((w / 2.0f), 0, new Color(255, 255,
255), (w / 2.0f), (h / 2.0f), new Color(255, 255, 255),
false);
g.setPaint(gradPaint);
g.fillRoundRect(0, 0, w - shadowGap, h - shadowGap, arcs.width,
arcs.height);
g.setColor(Color.GREEN);
g.setStroke(new BasicStroke(strokeSize));
g.drawRoundRect(0, 0, w - shadowGap, h - shadowGap, arcs.width,
arcs.height);
g.setStroke(new BasicStroke());
}
}
public class TableBorderPainter implements Painter {
private Color light, dark;
private GradientPaint gradPaint;
public TableBorderPainter(Color light, Color dark) {
this.light = light;
this.dark = dark;
}
@Override
public void paint(Graphics2D g, Object c, int w, int h) {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
gradPaint = new GradientPaint((w / 2.0f), 0, new Color(255, 255,
255), (w / 2.0f), (h / 2.0f), new Color(255, 255, 255), true); // making background of border paint to white to match the list background
g.setPaint(gradPaint);
g.fillRoundRect(0, 0, (w - 0), (h - 0), 10, 10);
RoundRectangle2D roundedRectangle = new RoundRectangle2D.Float(0,
0, w - 1, h - 1, 10, 10);
g.setColor(Color.RED); // here we are making red border color for list
g.draw(roundedRectangle);
}
}
public class TableBorderPaintGradient implements Painter {
private Color light, dark;
private GradientPaint gradPaint;
protected int strokeSize = 1;
protected Color shadowColor = new Color(128, 128, 128, 140);
/** Sets if it drops shadow */
protected boolean shady = true;
/** Sets if it has an High Quality view */
protected boolean highQuality = false;
/** Double values for Horizontal and Vertical radius of corner arcs */
protected Dimension arcs = new Dimension(10, 10);
/** Distance between shadow border and opaque panel border */
protected int shadowGap = 1;
/** The offset of shadow. */
protected int shadowOffset = 1; // width of the shadow
/** The transparency value of shadow. ( 0 - 255) */
protected int shadowAlpha = 130;
public TableBorderPaintGradient(Color light, Color dark) {
this.light = light;
this.dark = dark;
}
@Override
public void paint(Graphics2D g, Object object, int w, int h) {
Color shadowColorA = new Color(shadowColor.getRed(),
shadowColor.getGreen(), shadowColor.getBlue(), shadowAlpha);
if (highQuality) {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
if (shady) {
g.setColor(shadowColorA);
g.drawRoundRect(0, 0, w - shadowGap, h - shadowGap, arcs.width,
arcs.height);
} else {
shadowGap = 1;
}
gradPaint = new GradientPaint(0, 0, light, 0, h * .5f, dark, false);
g.setPaint(gradPaint);
g.drawRoundRect(shadowOffset,// X position
shadowOffset,// Y position
w - strokeSize - shadowOffset, // width
h - strokeSize - shadowOffset, // height
arcs.width, arcs.height);// arc Dimension
g.setColor(new Color(166,166,166));
g.setStroke(new BasicStroke(strokeSize));
g.drawRoundRect(shadowOffset,// X position
shadowOffset,// Y position
w - strokeSize - shadowOffset, // width
h - strokeSize - shadowOffset, // height
arcs.width, arcs.height);// arc Dimension
/* Changed to fillRoundedRect to drawRoundRect as per veryant suggestion */
g.setStroke(new BasicStroke());
}
}
}
并扩展 NimbusLookAndFeel
并设置为默认应用我在 Java NimbusBaseUI
中的所有组件
import javax.swing.UIManager;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
public class NimbusBaseUI extends NimbusLookAndFeel {
public NimbusBaseUI() {
super(); // Initialisation and installating
try {
new TableTheme(this);
UIManager.setLookAndFeel(this);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void initialize() {
// TODO Auto-generated method stub
super.initialize();
}
}
我的主类看起来像这样
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
class NimbusBaseDemo extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JTextField jtxt, txtDisEnabled;
int i;
private UIManager.LookAndFeelInfo[] lafs;
public NimbusBaseDemo() {
try {
// Set nimbus look and feel. nimbusBase works only for it.
new NimbusBaseUI();
} catch (Exception e) {
e.printStackTrace();
}
setTitle("Nimbus Base Demo");
setSize(400, 400);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
String columnNames[] = { "Column 1", "Column 2", "Column 3" ,"Column 4"};
String dataValues[][] = { { "12", "234", "67", " " },
{ "-123", "43", "853", "" }, { "93", "89.2", "109",""},
{ "279", "9033", "3092",""} };
JTable table1 = new JTable(dataValues, columnNames);
JScrollPane scrollPane = new JScrollPane(table1);
add(scrollPane);
}
public static void main(String args[]) {
new NimbusBaseDemo();
}
}
最后我得到 结果 作为
但我无法将 JTable(非内容区域)的背景更改为白色...
求推荐。
要使用带有行的 JTable
组件的完整 space,您需要设置 setFillsViewportHeight
参数:
table.setFillsViewportHeight(true);
这会将白色区域扩展到 table 的底部。
这是 doc 说法:
Sets whether or not this table is always made large enough to fill the height of an enclosing viewport. If the preferred height of the table is smaller than the viewport, then the table will be stretched to fill the viewport. In other words, this ensures the table is never smaller than the viewport. The default for this property is false.
没有这个,只有 JScrollPane 可见(或插入 JTable 的组件),因此需要更新的是背景。
PS:这并不是真正的主题问题,因为这是 JTable 的预期行为,即仅使用所需的 space,除非您告诉它使用完整的 space .
我正在编写一个自定义主题,它是 NimbusLookAndFeel
的扩展。
我想像
我的Table主题代码
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import javax.swing.Painter;
import infotrax.nimbus.NimbusBaseUI;
public class TableTheme {
protected int strokeSize = 2; // changed to 2 from 1 to fill the gap in corners
protected Color shadowColor = new Color(128, 128, 128, 140);
protected boolean shady = true;
protected boolean highQuality = false;
/** Double values for Horizontal and Vertical radius of corner arcs */
protected Dimension arcs = new Dimension(10, 10);
/** Distance between shadow border and opaque panel border */
protected int shadowGap = 1;
/** The offset of shadow. */
protected int shadowOffset = 1; // width of the shadow
/** The transparency value of shadow. ( 0 - 255) */
protected int shadowAlpha = 130;
public TableTheme(NimbusBaseUI nimbusUI) {
Insets ins = new Insets(2,2,2,2);
nimbusUI.getDefaults().put("Table.contentMargins",ins
);
nimbusUI.getDefaults().put("Table.background",
new Color(255, 255, 255)); // Modified to set background color of table to be white 01/08/2016
nimbusUI.getDefaults().put(
"Table.borderPainter",
new TableBorderPaintGradient(new Color(255, 255, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put(
"Table[Enabled].borderPainter",
new TableBorderPaintGradient(new Color(255, 255, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put(
"Table[Enabled+Selected].borderPainter",
new TableBorderPaintGradient(new Color(255, 255, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put(
"Table[Selected].borderPainter",
new TableBorderPaintGradient(new Color(255, 255, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put(
"Table[Focused].borderPainter",
new TableBorderPaintGradient(new Color(255, 255, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put(
"Table[Disabled].borderPainter",
new TableBorderPaintGradient(new Color(255, 255, 255), new Color(255,
255, 255)));
nimbusUI.getDefaults().put("TableHeader.font",
new Font("Myriad Pro Light", Font.BOLD, 13));
// Table Header color
nimbusUI.getDefaults()
.put("TableHeader:\"TableHeader.renderer\"[MouseOver].backgroundPainter",
new TableheaderPainter(new Color(188,188,188),
new Color(188,188,188)));
nimbusUI.getDefaults()
.put("TableHeader:\"TableHeader.renderer\"[Enabled].backgroundPainter",
new TableheaderPainter(new Color(188,188,188),
new Color(188,188,188)));
}
public class TableheaderPainter implements Painter {
private static final long serialVersionUID = 1L;
private Color light, dark;
private GradientPaint gradPaint;
public TableheaderPainter(Color light, Color dark) {
this.light = light;
this.dark = dark;
}
@Override
public void paint(Graphics2D paramGraphics2D, Object paramT,
int paramInt1, int paramInt2) {
paramGraphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
gradPaint = new GradientPaint((paramInt1 / 2.0f), 0, light,
(paramInt1 / 2.0f), (paramInt2 / 2.0f), dark, true);
paramGraphics2D.setPaint(gradPaint);
paramGraphics2D.fillRoundRect(0, 0, (paramInt1 - 0),
(paramInt2 - 0), 0, 0);
}
}
public class Tablebackground_Painter implements Painter {
private Color light, dark;
private GradientPaint gradPaint;
int shadowGap = 2;
public Tablebackground_Painter(Color light, Color dark) {
this.light = light;
this.dark = dark;
}
@Override
public void paint(Graphics2D g, Object c, int w, int h) {
Color shadowColorA = new Color(shadowColor.getRed(),
shadowColor.getGreen(), shadowColor.getBlue(), shadowAlpha);
if (highQuality) {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
if (shady) {
g.setColor(shadowColorA);
g.fillRoundRect(shadowOffset,// X position
shadowOffset,// Y position
w - strokeSize - shadowOffset, // width
h - strokeSize - shadowOffset, // height
arcs.width, arcs.height);// arc Dimension
} else {
shadowGap = 1;
}
gradPaint = new GradientPaint((w / 2.0f), 0, new Color(255, 255,
255), (w / 2.0f), (h / 2.0f), new Color(255, 255, 255),
false);
g.setPaint(gradPaint);
g.fillRoundRect(0, 0, w - shadowGap, h - shadowGap, arcs.width,
arcs.height);
g.setColor(Color.GREEN);
g.setStroke(new BasicStroke(strokeSize));
g.drawRoundRect(0, 0, w - shadowGap, h - shadowGap, arcs.width,
arcs.height);
g.setStroke(new BasicStroke());
}
}
public class TableBorderPainter implements Painter {
private Color light, dark;
private GradientPaint gradPaint;
public TableBorderPainter(Color light, Color dark) {
this.light = light;
this.dark = dark;
}
@Override
public void paint(Graphics2D g, Object c, int w, int h) {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
gradPaint = new GradientPaint((w / 2.0f), 0, new Color(255, 255,
255), (w / 2.0f), (h / 2.0f), new Color(255, 255, 255), true); // making background of border paint to white to match the list background
g.setPaint(gradPaint);
g.fillRoundRect(0, 0, (w - 0), (h - 0), 10, 10);
RoundRectangle2D roundedRectangle = new RoundRectangle2D.Float(0,
0, w - 1, h - 1, 10, 10);
g.setColor(Color.RED); // here we are making red border color for list
g.draw(roundedRectangle);
}
}
public class TableBorderPaintGradient implements Painter {
private Color light, dark;
private GradientPaint gradPaint;
protected int strokeSize = 1;
protected Color shadowColor = new Color(128, 128, 128, 140);
/** Sets if it drops shadow */
protected boolean shady = true;
/** Sets if it has an High Quality view */
protected boolean highQuality = false;
/** Double values for Horizontal and Vertical radius of corner arcs */
protected Dimension arcs = new Dimension(10, 10);
/** Distance between shadow border and opaque panel border */
protected int shadowGap = 1;
/** The offset of shadow. */
protected int shadowOffset = 1; // width of the shadow
/** The transparency value of shadow. ( 0 - 255) */
protected int shadowAlpha = 130;
public TableBorderPaintGradient(Color light, Color dark) {
this.light = light;
this.dark = dark;
}
@Override
public void paint(Graphics2D g, Object object, int w, int h) {
Color shadowColorA = new Color(shadowColor.getRed(),
shadowColor.getGreen(), shadowColor.getBlue(), shadowAlpha);
if (highQuality) {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
if (shady) {
g.setColor(shadowColorA);
g.drawRoundRect(0, 0, w - shadowGap, h - shadowGap, arcs.width,
arcs.height);
} else {
shadowGap = 1;
}
gradPaint = new GradientPaint(0, 0, light, 0, h * .5f, dark, false);
g.setPaint(gradPaint);
g.drawRoundRect(shadowOffset,// X position
shadowOffset,// Y position
w - strokeSize - shadowOffset, // width
h - strokeSize - shadowOffset, // height
arcs.width, arcs.height);// arc Dimension
g.setColor(new Color(166,166,166));
g.setStroke(new BasicStroke(strokeSize));
g.drawRoundRect(shadowOffset,// X position
shadowOffset,// Y position
w - strokeSize - shadowOffset, // width
h - strokeSize - shadowOffset, // height
arcs.width, arcs.height);// arc Dimension
/* Changed to fillRoundedRect to drawRoundRect as per veryant suggestion */
g.setStroke(new BasicStroke());
}
}
}
并扩展 NimbusLookAndFeel
并设置为默认应用我在 Java NimbusBaseUI
import javax.swing.UIManager;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
public class NimbusBaseUI extends NimbusLookAndFeel {
public NimbusBaseUI() {
super(); // Initialisation and installating
try {
new TableTheme(this);
UIManager.setLookAndFeel(this);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void initialize() {
// TODO Auto-generated method stub
super.initialize();
}
}
我的主类看起来像这样
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
class NimbusBaseDemo extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JTextField jtxt, txtDisEnabled;
int i;
private UIManager.LookAndFeelInfo[] lafs;
public NimbusBaseDemo() {
try {
// Set nimbus look and feel. nimbusBase works only for it.
new NimbusBaseUI();
} catch (Exception e) {
e.printStackTrace();
}
setTitle("Nimbus Base Demo");
setSize(400, 400);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
String columnNames[] = { "Column 1", "Column 2", "Column 3" ,"Column 4"};
String dataValues[][] = { { "12", "234", "67", " " },
{ "-123", "43", "853", "" }, { "93", "89.2", "109",""},
{ "279", "9033", "3092",""} };
JTable table1 = new JTable(dataValues, columnNames);
JScrollPane scrollPane = new JScrollPane(table1);
add(scrollPane);
}
public static void main(String args[]) {
new NimbusBaseDemo();
}
}
最后我得到 结果 作为
但我无法将 JTable(非内容区域)的背景更改为白色...
求推荐。
要使用带有行的 JTable
组件的完整 space,您需要设置 setFillsViewportHeight
参数:
table.setFillsViewportHeight(true);
这会将白色区域扩展到 table 的底部。
这是 doc 说法:
Sets whether or not this table is always made large enough to fill the height of an enclosing viewport. If the preferred height of the table is smaller than the viewport, then the table will be stretched to fill the viewport. In other words, this ensures the table is never smaller than the viewport. The default for this property is false.
没有这个,只有 JScrollPane 可见(或插入 JTable 的组件),因此需要更新的是背景。
PS:这并不是真正的主题问题,因为这是 JTable 的预期行为,即仅使用所需的 space,除非您告诉它使用完整的 space .