Вопрос

I have a JFrame, I have a check box in the JFrame that once clicked opens a JDialog, which then adds a JPanel I have created.

Inside the JPanel I have 2 arrays of JTextField. I need to get the values from these JTextField into my JFrame to use them in a method.

I have tried creating methods in the panel, but the JDialog can't see them.

Any help appreciated.

public ProdDialog(String popUp, JFrame jFrame, DigiProduct p) {

    this.p = p;
    this.jframe = jFrame;

    JPanel popupPanels[] = new JPanel[] { cd = new CDPopup(p)};

    cdDialog = new JDialog(jFrame, true);
    cdDialog.setLocationRelativeTo(null);





    for (int i = 0; i < popupPanels.length; i++) {
        if (popUp.equals(popupPanels[i].getName())) {
            cdDialog.add(popupPanels[i]);
            cdDialog.setSize(popupPanels[i].getSize());
        }
    }
    cdDialog.setVisible(true);
}


public CDPopup(DigiProduct p) {

    this.setName("cd");
    this.p = p;
    this.setLayout(new BorderLayout());
    if(p.getAlbum().getSongList().size() >= 15)
    {
        this.setSize(690, 600);
    }
    else if(p.getAlbum().getSongList().size() < 11)
    {
        this.setSize(690, 410);
    }
    else
    {
        this.setSize(690, 490);
    }
    JPanel top = new JPanel(new GridBagLayout());
    this.setBorder(new TitledBorder("CD"));


    JPanel detailsPanel = new JPanel(new BorderLayout());
    detailsPanel.setPreferredSize(new Dimension(240, 80));
    this.add(detailsPanel, BorderLayout.WEST);

    JPanel details = new JPanel(new GridBagLayout());
    details.setBorder(border);
    details.setPreferredSize(new Dimension(150, 130));
    detailsPanel.add(details, BorderLayout.NORTH);

    JPanel spacer = new JPanel();
    spacer.setPreferredSize(new Dimension(200, 100));
    detailsPanel.add(spacer, BorderLayout.SOUTH);


    gc.gridx = 0;
    gc.gridy = 0;
    artist = new JLabel(" Artist");
    details.add(artist, gc);

    gc.gridx = 0;
    gc.gridy = 1;
    artistTBox = new JTextField(10);

    details.add(artistTBox, gc);


    JPanel songs = new JPanel(new GridBagLayout());
    songs.setPreferredSize(new Dimension(380, 200));
    songs.setBorder(border);

    songNumber = new JLabel(" No.");
    songName = new JLabel("Song");
    songLength = new JLabel("Length");

    JTextField[] digiProdDetailBx = new JTextField[p.getAlbum().getSongList().size()];
    JTextField[] digiProdDetailBx2 = new JTextField[p.getAlbum().getSongList().size()];
    JLabel[] digiProdDetailLb = new JLabel[p.getAlbum().getSongList().size()];


    for (int i = 0; i < p.getAlbum().getSongList().size(); i++) {

        int num = i +1;

        if (i == 0) {
            gc.gridx = 0;
            gc.gridy = 0;
            gc.anchor = GridBagConstraints.WEST;
            songs.add(songNumber, gc);
        }

        gc.gridx = 0;
        gc.gridy = i + 1;
        gc.gridwidth = 1;
        gc.gridheight = 1;
        gc.weighty = 0.0;
        gc.weightx = 2.0;
        gc.anchor = GridBagConstraints.WEST;
        digiProdDetailLb[i] = new JLabel(" " + num);
        digiProdDetailLb[i].setFont(font);
        songs.add(digiProdDetailLb[i], gc);

        if (i == 0) {
            gc.gridx = 1;
            gc.gridy = 0;
            songs.add(songName, gc);
        }
        gc.gridx = 1;
        gc.gridy = i + 1;
        gc.gridwidth = 1;
        gc.gridheight = 1;
        gc.weighty = 0.0;
        gc.weightx = 2.0;
        gc.gridwidth = 2;
        digiProdDetailBx[i] = new JTextField(p.getAlbum().getSongList().get(i).getSong_name());
        digiProdDetailBx[i].setPreferredSize(new Dimension(250, 20));
        songs.add(digiProdDetailBx[i], gc);

        if (i == 0) {
            gc.gridx = 3;
            gc.gridy = 0;
            songs.add(songLength, gc);
        }
        gc.gridx = 3;
        gc.gridy = i + 1;
        gc.gridwidth = 1;
        gc.gridheight = 1;
        gc.weighty = 0.0;
        gc.weightx = 2.0;
        gc.gridwidth = 2;
        digiProdDetailBx2[i] = new JTextField(p.getAlbum().getSongList().get(i).getSong_length());
        digiProdDetailBx2[i].setPreferredSize(new Dimension(50, 20));
        songs.add(digiProdDetailBx2[i], gc);

    }
    ;

    this.add(songs, BorderLayout.EAST);

    JPanel bottom = new JPanel(new FlowLayout());
    this.add(bottom, BorderLayout.SOUTH);

    add = new JButton("Add");
    add.setPreferredSize(new Dimension(60, 30));
    add.addActionListener(this);
    bottom.add(add);

    update = new JButton("Update");
    update.setPreferredSize(new Dimension(60, 30));
    update.addActionListener(this);
    bottom.add(update);

    this.setVisible(true);




}




public JTextField[] getDigiProdDetailBx() {
    return digiProdDetailBx;
}


public void setDigiProdDetailBx(JTextField[] digiProdDetailBx) {
    this.digiProdDetailBx = digiProdDetailBx;
}


public JTextField[] getDigiProdDetailBx2() {
    return digiProdDetailBx2;
}


public void setDigiProdDetailBx2(JTextField[] digiProdDetailBx2) {
    this.digiProdDetailBx2 = digiProdDetailBx2;
}

public void actionPerformed(ActionEvent e) {

    if(e.getSource().equals(update))
    {


    }


}

}

Это было полезно?

Решение

To get a JPanel you've added to a JDialog you should use a loop like this one:

for (Component c : cdDialog.getContentPane().getComponents()) {
    if (c instanceof JPanel) {
        //do work
    }
}

When you add components to heavyweight containers, it actually adds them to the content pane, and thus you need to search the content pane for any children.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top