简体   繁体   中英

Java adding components to JOptionPane

I'm trying create a custom panel for a simple data entry dialog. I've created a custom panel which I add my labels and text fields. I'm trying to add it to a JOptionPane for display.

When I call it, my components do not display but the JOptionPane buttons do. What is the correct way of doing this? Thanks in advanced!!

Here is where I create my custom panel and call JOptionPane:

    public class ListenCustEdit implements ActionListener {
    @Override
    @SuppressWarnings("empty-statement")
    public void actionPerformed(ActionEvent e) {
        TestPanel panel = new TestPanel();
        int input = JOptionPane.showConfirmDialog(frame, panel, "Edit Customer:"
                        ,JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (input == 0) {
            // OK
            JOptionPane.showMessageDialog(frame,"Changes savewd");
        } else {
            // Cancel
            JOptionPane.showMessageDialog(frame, "No changes were saved");
        }
    }
}

Here is my custom panel class:

public class TestPanel extends JPanel {

JTextField custIdTextField;
JTextField companyTextField;
JTextField firstNameTextField;
JTextField lastNameTextField;

public TestPanel() {
    initUI();
}

public final void initUI() {

    // create the panel and set the layout
    JPanel main = new JPanel();
    main.setLayout(new GridLayout(4,4));

    // create the labels
    JLabel custIdLabel = new JLabel("Cust Id: ");
    JLabel companyLabel = new JLabel("Company: ");
    JLabel firstNameLabel = new JLabel("First Name: ");
    JLabel lastNameLabel = new JLabel("Last Name: ");

    // create the text fields
    custIdTextField = new JTextField();
    companyTextField = new JTextField();
    firstNameTextField = new JTextField();
    lastNameTextField = new JTextField();

    // add componets to panel
    main.add(custIdLabel);
    main.add(custIdTextField);
    main.add(companyLabel);
    main.add(companyTextField);
    main.add(firstNameLabel);
    main.add(firstNameTextField);
    main.add(lastNameLabel);
    main.add(lastNameTextField);
}

You need to add the main panel to your TestPanel.

public final void initUI() {
    // ...
    add(main);
}

In initUI, you create a JPanel (called "main") and stuff it full of components, but don't do anything with it. You need to either add "main" to the actual instance of TestPanel, or just skip the step of creating the "main" panel altogether.

The first way:

public final void initUI() {
    // ... everything as before, then 
    this.add(main);
}

The second way:

public final void initUI() {
    this.setLayout(new GridLayout(4,4));

    // create the labels
    JLabel custIdLabel = new JLabel("Cust Id: ");
    JLabel companyLabel = new JLabel("Company: ");
    JLabel firstNameLabel = new JLabel("First Name: ");
    JLabel lastNameLabel = new JLabel("Last Name: ");

    // create the text fields
    custIdTextField = new JTextField();
    companyTextField = new JTextField();
    firstNameTextField = new JTextField();
    lastNameTextField = new JTextField();

    // add componets to panel
    this.add(custIdLabel);
    this.add(custIdTextField);
    this.add(companyLabel);
    this.add(companyTextField);
    this.add(firstNameLabel);
    this.add(firstNameTextField);
    this.add(lastNameLabel);
    this.add(lastNameTextField);
}

(The "this."'s aren't strictly necessary, but I added them for illustration.)

Hope this helps!

您没有在构造函数中将主Panel添加到TestPanel实例。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM