简体   繁体   中英

How to setText into a jTextField that is inside a jPanel that is inside a jFrame to from Other jFrame

This is the first Class, the thing is that i have to receive the values from the other class triggered by the event in the button (action performed), so in this class i want to display it!

public class PanelCotizacion extends javax.swing.JPanel {
    private int numCotizacion = 0;
    public int getNumCotizacion() {
        return numCotizacion;
    }
    public void setNumCotizacion(int numCotizacion) {
        this.numCotizacion = numCotizacion;
    }
    public PanelCotizacion() {
        initComponents();
        showTextFields();
    }
    show textFields(){
        this.txtCosTra.setText(String.valueOf(cosTra));
    }
}

This is the second Class, where i want to send the value that is in the jTextField, remember that i mentioned that in both jFrames, have jPanels and the jTextFields are inside of it.

public class BusquedaCotizacionGUI extends javax.swing.JFrame {
    public BusquedaCotizacionGUI() {
        initComponents();
        this.setLocationRelativeTo(null);        
    }

    private void cmdOKActionPerformed(java.awt.event.ActionEvent evt) {
        PanelCotizacion p = new PanelCotizacion();
        p.setNumCotizacion(Integer.parseInt(this.txtCotizacion.getText()));
        p.setVisible(true);
        p.revalidate();
        p.updateUI();
        this.dispose();
    }
}

So please don't look the sintaxis, if you can give me an idea to solve that problem, i think maybe dont display it in the jTextFields cause are private, Are there any way to display it or How can i update the jPanel components to display the update TextFields? Thanks a lot!

Your example suffers from a reference issues. The instance of PanelCotizacion has nothing to do with what's on the screen (or at least, you've never added it to the screen - which could the solution to the problem I don't know)

The simplest solution would be to attach some kind of listener to the second class (source of the event), which provides notification that the value has changed and then provide some kind of accessor to extract the value from the class, like public String getText() {...} for example.

In BusquedaCotizacionGUI add...

public void addActionListener(ActionListener listener) {
    cmdOk.addActionListener(listener);
}

public void removeActionListener(ActionListener listener) {
    cmdOk.removeActionListener(listener);
}

public String getText() {
    return txtCotizacion.getText();
}

Either in PanelCotizacion or the container controlling the two instances of the classes, you need to attach an actionListener to BusquedaCotizacionGUI via the addActionListener method. When actionPeformed is called, you need to set the text of instance of PanelCotizacion you already have

try to use a your jframe and text and panel as parameters in the other constructor of your jframe than use them inside it when you invoke your action button like this

public constructoroftheotherJFrame (firstJFrame frame , String yourtext){
this.frame=frame;
this.text=text;
// then type your code there
}

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