0

我有一个 JTable,其中包含一个项目列表和一个带有一些组件的 JPanel。当我点击我的按钮时,我希望 JTable 中所选项目的所有信息都将加载到 JPanel。第一次,它工作得很好,但是当我点击我的按钮时,Jpanel 会出现空组件。

单击我的按钮时的代码:

jbtUpdateContract.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
            Object contractID = getValueOfSelectedRow(contractTable, 0);
            Contract contract = contractTransaction.getContractByID(Integer.parseInt(contractID.toString()));

            //Create Jpanel to display information
            jplUpdateContractForm.removeAll();
            jplUpdateContractForm = createContractForm(contract);
            jplUpdateContractForm.revalidate();

            //Modify contract frame
            jfrmUpdateContract.getContentPane().add(jplUpdateContractForm,
                    BorderLayout.CENTER);
            jfrmUpdateContract.setSize(400, 300);
            jfrmUpdateContract.setVisible(true);
            jfrmUpdateContract.setResizable(false);
        }
    });

我的createContractForm功能:

public static JPanel createContractForm(Contract contract)
{
    JPanel jplContractForm = new JPanel(new SpringLayout());
    JLabel jlblAppointmentDate = new JLabel("Appointment date:", JLabel.TRAILING);
    jlblAppointmentDate.setName("jlblAppointmentDate");
    jplContractForm.add(jlblAppointmentDate);

    final JTextField jtxtAppointmentDate = new JTextField(15);
    jtxtAppointmentDate.setName("jtxtAppointmentDate");
    jtxtAppointmentDate.setText(contract.getAppointmentDate());
    jtxtAppointmentDate.setEditable(false);
    jtxtAppointmentDate.addMouseListener(appointmentDateMouseListener(jtxtAppointmentDate));        
    jlblAppointmentDate.setLabelFor(jtxtAppointmentDate);
    jplContractForm.add(jtxtAppointmentDate);
    jplContractForm.setOpaque(true);    

    //***Customer Cobobox
    JLabel jlblCustomer= new JLabel("Customer:", JLabel.TRAILING);
    jlblCustomer.setName("jlblCustomer");
    jplContractForm.add(jlblCustomer);

    final JComboBox  jcbxCustomer = new JComboBox();
    jcbxCustomer.setName("jcbxCustomer");
    //Load customers from DB to combobox
    Customer[] customers = customerTransaction.getAllCustomer();
    for(int i = 0; i < customers.length; i++)
        jcbxCustomer.addItem(customers[i]);
    System.out.println("----------CUSTOMER----------" + getIndexOfCustomerComboBoxItem(jcbxCustomer, contract.getCustomerSeq()));
    jcbxCustomer.setSelectedIndex(getIndexOfCustomerComboBoxItem(jcbxCustomer, contract.getCustomerSeq()));

    jlblCustomer.setLabelFor(jcbxCustomer);
    jplContractForm.add(jcbxCustomer);
    jplContractForm.setOpaque(true);
}

请帮助我解释为什么 JPanel 是空的,如上所述。

问候。

4

1 回答 1

3

框架已经可见,所以我认为这些代码行没有任何帮助。

jfrmUpdateContract.setSize(400, 300);
jfrmUpdateContract.setVisible(true);
jfrmUpdateContract.setResizable(false);

revalidate() 什么都不做,因为您还没有将面板添加到框架中。在面板添加到框架后尝试 revalidate() 。

jplUpdateContractForm = createContractForm(contract);
//jplUpdateContractForm.revalidate();
jfrmUpdateContract.getContentPane().add(jplUpdateContractForm, BorderLayout.CENTER);
jplUpdateContractForm.revalidate();

如果您需要更多帮助,请发布适当的SSCCE.

于 2013-10-06T03:12:25.707 回答