java 更改 JTable 的背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11606852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change background color of JTable
提问by Gomathi
I have added a table, but the problem is, the panel doesnt show its background color. I have tried setting scrollpane background color, etc. But it doesn't work. The frame has a button 'Verify', which when clicked, displays a table beneath it. Until it is clicked, the portion where the table will appear is solid gray. I want the whole portion to be ivory background. Kindly help me diagnose the problem.
我添加了一个表格,但问题是面板没有显示其背景颜色。我曾尝试设置滚动窗格背景颜色等。但它不起作用。该框架有一个“验证”按钮,单击该按钮会在其下方显示一个表格。在单击它之前,将出现表格的部分是纯灰色的。我希望整个部分都是象牙色背景。请帮助我诊断问题。
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn1=DriverManager.getConnection("jdbc:odbc:vasantham","","");
Statement st1=conn1.createStatement();
ResultSet rs1=st1.executeQuery("select * from try where DATEDIFF('d',NOW(),exdate) < 61 order by tname");
ResultSetMetaData md1=rs1.getMetaData();
int cols1=md1.getColumnCount();
model1=new DefaultTableModel();
model1.addColumn("Purpose");
model1.addColumn("Name");
model1.addColumn("Composition");
model1.addColumn("Expiry");
model1.addColumn("Stock");
model1.addColumn("Cost");
model1.addColumn("Type");
model1.addColumn("Supplier");
model1.addColumn("Supplier Number");
model1.addColumn("Rack");
table1=new JTable(model1);
Color ivory=new Color(255,255,208);
table1.setOpaque(false);
table1.setBackground(ivory);
String[] tabledata1=new String[cols1];
int i=0;
while(rs1.next())
{
for(i=0;i<cols1;i++)
{
if(i==3)
{
Date intr1=(rs1.getDate(i+1));
tabledata1[i]=formatter1.format(intr1);
}
else
tabledata1[i]=rs1.getObject(i+1).toString();
}
model1.addRow(tabledata1);
}
JScrollPane scroll1 = new JScrollPane(table1);
scroll1.setBackground(new Color(255,255,208));
scroll1.getViewport().setBackground(ivory);
panel1.setLayout(new BorderLayout());
panel1.setBackground(ivory);
table1.getTableHeader().setBackground(ivory);
panel1.add(scroll1,BorderLayout.CENTER);
frame1.add(panel1,BorderLayout.CENTER);
conn1.close();
}
回答by MadProgrammer
Scroll Panes contain another component, known as the ViewPort. This is actually where the components been assigned to the scroll pane get added.
滚动窗格包含另一个组件,称为 ViewPort。这实际上是添加分配给滚动窗格的组件的地方。
If you want to maintain the JTable as transparent (table1.setOpaque(false);
), then you need to change the view ports background
如果要保持 JTable 为透明 ( table1.setOpaque(false);
),则需要更改视口背景
scroll1.getViewport().setBackground(ivory);
Otherwise, set the table to opaque and table1.setFillsViewportHeight(true);
to force the table to fill the entire viewport
否则,将表格设置为不透明并table1.setFillsViewportHeight(true);
强制表格填充整个视口
UPDATED
更新
Works fine for me
对我来说很好用
model1 = new DefaultTableModel();
model1.addColumn("Purpose");
model1.addColumn("Name");
model1.addColumn("Composition");
model1.addColumn("Expiry");
model1.addColumn("Stock");
model1.addColumn("Cost");
model1.addColumn("Type");
model1.addColumn("Supplier");
model1.addColumn("Supplier Number");
model1.addColumn("Rack");
for (int index = 0; index < 10; index++) {
Vector vector = new Vector();
vector.add("p" + index);
vector.add("n" + index);
vector.add("c" + index);
vector.add("e" + index);
vector.add("s" + index);
vector.add("c" + index);
vector.add("t" + index);
vector.add("s" + index);
vector.add("s" + index);
vector.add("r" + index);
model1.addRow(vector);
}
table1 = new JTable(model1);
Color ivory = new Color(255, 255, 208);
table1.setOpaque(true);
table1.setFillsViewportHeight(true);
table1.setBackground(ivory);
JScrollPane scroll1 = new JScrollPane(table1);
table1.getTableHeader().setBackground(ivory);
add(scroll1, BorderLayout.CENTER);
You can comment out the row creation section and it will still paint in ivory.
您可以注释掉行创建部分,它仍然会以象牙色绘制。
回答by mKorbel
why do you mean doesn't work ???
为什么你的意思是不起作用???
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableCellClass {
private JFrame frame = new JFrame("Table Demo");
private String[] columnNames = {"String", "Integer", "Float", "Double", "Locale & Double", "Boolean"};
private Object[][] data = {
{"aaa", new Integer(12), new Float(12.15), new Double(100.05), new Double(12.05), true},
{"bbb", new Integer(5), new Float(7.154), new Double(6.1555), new Double(417.55), false},
{"CCC", new Integer(92), new Float(0.1135), new Double(3.1455), new Double(11.05), true},
{"ddd", new Integer(12), new Float(31.15), new Double(10.05), new Double(23.05), true},
{"eee", new Integer(5), new Float(5.154), new Double(16.1555), new Double(17.55), false},
{"fff", new Integer(92), new Float(4.1135), new Double(31.1455), new Double(3.05), true}};
private TableModel model = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;
@Override
public Class<?> getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
private JTable table = new JTable(model);
public TableCellClass() {
table.setRowHeight(20);
//table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setBackground(Color.red);
JScrollPane scroll = new JScrollPane(table);
scroll.getViewport().setBackground(Color.blue);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scroll);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
TableCellClass tableCellClass = new TableCellClass();
}
});
}
}
this could be better, in the case that you want to play with opacity ???
这可能会更好,如果你想玩不透明度???
import java.awt.*;
import javax.swing.*;
public class TableWithTimer {
private static final long serialVersionUID = 1L;
private JFrame frame = new JFrame();
private JScrollPane scroll = new JScrollPane();
private JTable myTable;
private String[] head = {"One", "Two", "Three", "Four", "Five", "Six"};
private String[][] data = new String[25][6];
public TableWithTimer() {
myTable = new TableBackroundPaint0(data, head);
myTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
myTable.setGridColor(Color.gray);
myTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scroll.setViewportView(myTable);
frame.add(scroll, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100, 100);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TableWithTimer tableWithTimer = new TableWithTimer();
}
});
}
}
class TableBackroundPaint0 extends JTable {
private static final long serialVersionUID = 1L;
TableBackroundPaint0(Object[][] data, Object[] head) {
super(data, head);
setOpaque(false);
((JComponent) getDefaultRenderer(Object.class)).setOpaque(false);
}
@Override
public void paintComponent(Graphics g) {
Color background = new Color(168, 210, 241);
Color controlColor = new Color(230, 240, 230);
int width = getWidth();
int height = getHeight();
Graphics2D g2 = (Graphics2D) g;
Paint oldPaint = g2.getPaint();
g2.setPaint(new GradientPaint(0, 0, background, width, 0, controlColor));
g2.fillRect(0, 0, width, height);
g2.setPaint(oldPaint);
for (int row : getSelectedRows()) {
Rectangle start = getCellRect(row, 0, true);
Rectangle end = getCellRect(row, getColumnCount() - 1, true);
g2.setPaint(new GradientPaint(start.x, 0, controlColor, (int) ((end.x + end.width - start.x) * 1.25), 0, Color.orange));
g2.fillRect(start.x, start.y, end.x + end.width - start.x, start.height);
}
super.paintComponent(g);
}
}