James Zagada
James Zagada

Reputation: 39

How to execute a JButton code from another JFrame

I have this JFrame called receipt, and what I want is when the user clicked the Exit button (top right of the JFrame) the JButton from another class will be executed.

I have added a WindowLIstener for the closing of the JFrame receipt:

@Override
    public void windowClosing(WindowEvent e) {
        if (JOptionPane.showConfirmDialog(null, "Do you want to have another transaction?", "New Transaction", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            reciept.this.dispose();

            ---------> // JButton code to be executed from another class

        } else {
            JOptionPane.showMessageDialog(null, "Thank you for shopping with us!");
            System.exit(0);
        }
    }

The JButton new transaction from another class:

 if (e.getSource() == transaction) {
            total = 0;
            cartAmount.setText("Total: ");
            list.clear();
            list2.clear();
            list3.clear();
            model.setRowCount(0);
            model2.setRowCount(0);
            image.setIcon(null);
            iteminfo.setText("");
            itemprice.setText("Price: P ");
            itemstock.setText("Stocks: ");
        }

Basically, the JButton new transaction just resets the Class Store, thank you so much for any help!

Upvotes: 0

Views: 93

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

This sounds like an XY Problem where your best solution is to try another approach entirely:

  • Best if receipt not be displayed in a JFrame but rather a modal JDialog. This way, the calling code is stopped as soon as this window (the receipt dialog) is displayed, and then resumes as soon as the dialog is no longer visible.
  • Then, once receipt is no longer visible, the calling code, the other code, what you call the "JButton code" can be executed.

If this doesn't appear to answer your question, then consider fleshing out your problem and your code for us, including creating and posting a valid minimimal reproducible example program in your question.

Upvotes: 1

Related Questions