Gom*_*thi 1 java swing background jtable jscrollpane
我添加了一个表,但问题是,面板没有显示其背景颜色.我试过设置滚动窗格背景颜色等.但它不起作用.框架上有一个"验证"按钮,单击该按钮可在其下方显示一个表格.在单击之前,表格将显示的部分为灰色.我希望整个部分是象牙背景.请帮我诊断问题.
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();
}
Run Code Online (Sandbox Code Playgroud)

滚动窗格包含另一个组件,称为ViewPort.这实际上是添加了分配给滚动窗格的组件的位置.
如果要将JTable维护为透明(table1.setOpaque(false);),则需要更改视图端口背景
scroll1.getViewport().setBackground(ivory);
Run Code Online (Sandbox Code Playgroud)
否则,将表设置为opaque并table1.setFillsViewportHeight(true);强制表填充整个视口
更新
对我来说很好
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);
Run Code Online (Sandbox Code Playgroud)
您可以注释掉行创建部分,它仍将以象牙形式绘制.
| 归档时间: |
|
| 查看次数: |
14805 次 |
| 最近记录: |