Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Initialize two <code>boolean</code> variables <code>aClicked</code> and <code>bClicked</code> to <code>false</code>.</p> <p>When the user clicks the button in class A, set <code>aClicked</code> to <code>true</code>. Then perform the following logic:</p> <pre><code>if (aClicked &amp;&amp; bClicked) { new ClassC(); } aClicked = false; bClicked = false; </code></pre> <p>When the user clicks the button in class B, set <code>bClicked</code> to <code>true</code>. Then perform the same logic.</p> <p>I'd need more details for a better answer.</p> <p>EDIT: Given your code sample and using anonymous classes, I wrote the following. I don't normally write GUI's in Java, and when I do, it's with WindowBuilder. Point being this probably isn't thread-safe and may not clean up resources properly, but the point is to demonstarte the concept of referencing the <code>boolean</code> variables from within an anonymous class using the <code>final</code> keyword.</p> <p>This worked for me. If it doesn't help you, I'll need more of your code.</p> <p>Also, for the <code>State</code> class, if your logic gets more complicated, I would recommend the FSM approach recommended by Jake Greene.</p> <pre><code>import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; public class Main { public static void main(String[] args) { final State state = new State(); final JTextArea textArea = new JTextArea(1, 10); textArea.setEditable(false); JButton buttonA = new JButton("Button A"); buttonA.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { state.clickA(); if (state.bothClicked()) { textArea.setText("both clicked."); } } }); JButton buttonB = new JButton("Button B"); buttonB.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { state.clickB(); if (state.bothClicked()) { textArea.setText("both clicked."); } } }); JButton reset = new JButton("Reset!"); reset.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { state.reset(); textArea.setText(""); } }); JPanel pane = new JPanel(); pane.add(buttonA); pane.add(buttonB); pane.add(textArea); pane.add(reset); JFrame frame = new JFrame(); frame.setContentPane(pane); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } } class State { private boolean aClicked; private boolean bClicked; public void clickA() { this.aClicked = true; } public void reset() { this.aClicked = false; this.bClicked = false; } public void clickB() { this.bClicked = true; } public boolean bothClicked() { return this.aClicked &amp;&amp; this.bClicked; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload