5

我正在开发一个基于 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);
    }
}
4

1 回答 1

7

您需要Component在创建JScrollPane时通过。

JScrollPane scrollPane = new JScrollPane(table);
JPContainer.add(scrollPane);
于 2012-10-04T05:29:03.230 回答