我正在开发一个基于gui的项目,我需要在jpanel中添加一个jtable。但是我没有理解为什么在添加滚动窗格时没有显示该表。另外,当添加没有滚动窗格时,表头是没有显示。 谢谢你的帮助... 以下是我正在使用的代码..
import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class FrmAddEditDN extends JDialog{
JButton JBUpdate = new JButton(new ImageIcon("images/save.png"));
JButton JBReset = new JButton("Reset",new ImageIcon("images/reset.png"));
JButton JBCancel = new JButton("Cancel",new ImageIcon("images/cancel.png"));
JLabel JLPic1 = new JLabel();
JLabel JLBanner = new JLabel("Please fill-up all the required fields.");
public FrmAddEditDN(boolean ADD_STATE,JFrame OwnerForm,Connection srcCN,String srcSQL){
super(OwnerForm,true);
cnAEDN = srcCN;
ADDING_STATE = ADD_STATE;
JPanel JPContainer = new JPanel();
JPContainer.setLayout(null);
String[] columnNames = {
"Sr No","Invoice No","Invoice Date","Consignee","Description","Basic Amount","Invoice Amount","Payment Received",
"EFT Date","Payment Earlier Received","Comm. @ %","Comm.Claim @ %","Comm. Received","Date","Bank","Remarks"};
String[][] data = {
{"","","","","","","","","","","","","","","",""}
};
DefaultTableModel DTModel = new DefaultTableModel(data,columnNames);
JTable table = new JTable(5,16);
table.setBounds(15,295,screen.width-40,150);
table.setRowHeight(30);
table.setForeground(Color.black);
table.setBackground(Color.white);
JTableHeader header = table.getTableHeader();
header.setForeground(Color.red);
header.setBackground(Color.green);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.getColumnModel().getColumn(0).setPreferredWidth(2);
table.getColumnModel().getColumn(1).setPreferredWidth(50);
JScrollPane tableContainer = new JScrollPane();
tableContainer.setViewportView(table);
JPContainer.add(tableContainer);
getContentPane().add(JPContainer);
setSize(screen.width-5,screen.height-45);
setResizable(false);
setLocation(0,0);
}
}
答案 0 :(得分:7)
创建JScrollPane时,您需要传递Component
。
JScrollPane scrollPane = new JScrollPane(table);
JPContainer.add(scrollPane);