0

Hi guys im trying to set a string value on my main frame with a value generated in an other frame, but i dont quite know how to do it this is my code so far.

This is my mainframe opens the query frame where i input the data

public class InfoCd extends JFrame {

    private JPanel contentPane;
    private JTable table;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    InfoCd frame = new InfoCd();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
Connection conn=null;
private JTextField verConsulta;


    /**
     * Create the frame.
     */
    public InfoCd() {

        conn=sqliteConnection.dbConnector();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 753, 477);
        contentPane = new JPanel();
        contentPane.setBackground(SystemColor.activeCaption);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnHacerConsulta = new JButton("Hacer Consulta");

        btnHacerConsulta.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    Query q = new Query();
                    q.setVisible(true);
                    String userQuery = Query.getqueryField();//here is the problem im sending the value before filling it in the second frame
                    Statement statement = conn.createStatement();
                    ResultSet rs = statement.executeQuery(userQuery);
                    table.setModel(DbUtils.resultSetToTableModel(rs));

                    //insert delete update

                } catch (Exception e2) {
                    e2.printStackTrace();
                }

            }
        });
        btnHacerConsulta.setBounds(20, 388, 169, 39);
        contentPane.add(btnHacerConsulta);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 0, 737, 367);
        contentPane.add(scrollPane);

        table = new JTable();
        scrollPane.setViewportView(table);

        verConsulta = new JTextField();
        verConsulta.setBounds(260, 388, 359, 29);
        contentPane.add(verConsulta);
        verConsulta.setColumns(10);
    }
}

here is the query field

public class Query extends JFrame {

    private JPanel contentPane;
    private static JTextField queryField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Query frame = new Query();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */


    public Query() {
        setBackground(SystemColor.activeCaption);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 641, 146);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnAceptar = new JButton("Aceptar");
        btnAceptar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                getqueryField();
                dispose();
            }
        });
        btnAceptar.setBounds(226, 52, 155, 34);
        contentPane.add(btnAceptar);

        JLabel lblConsulta = new JLabel("Consulta:");
        lblConsulta.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 13));
        lblConsulta.setBounds(69, 11, 101, 39);
        contentPane.add(lblConsulta);

        queryField = new JTextField();
        queryField.setBounds(135, 21, 480, 20);
        contentPane.add(queryField);
        queryField.setColumns(10);
    }

    public static String getqueryField() {
        return queryField.getText();
    }
}
Nicolas Albarracin
  • 207
  • 1
  • 5
  • 16
  • 1
    Possible duplicate of [Passing values between JFrames](http://stackoverflow.com/questions/7017063/passing-values-between-jframes) – MordechayS Nov 07 '16 at 23:01
  • You should only need one _main_ method per application... you might also want to have a look at using a [_JDialog_](https://docs.oracle.com/javase/8/docs/api/javax/swing/JDialog.html). – Gulllie Nov 12 '16 at 02:29

0 Answers0