G Smith
G Smith

Reputation: 1

Invoking classes in Java

I want to write three classes in Java where when a button is clicked in class A and another button is clicked in class B, the two events combined creates an instance of class C. How do I go about this?

(Update from comments:)

Below is the event code.

// There is a text area showing when it is clicked and when it 
// gets a response back from the thirdclass
...
buttonpanel = new JPanel(); 
getItButton = new JButton("Call ThirdClass"); 
getItButton.addActionListener (new ActionListener() { 
    public void actionPerformed (ActionEvent evt) { 
        textarea.append("Calling thirdclass...\n"); 
        String result = thirdclass.fetch_message(); 
        textarea.append(" Result = " + result + "\n\n"); 
    } 
}); 

Upvotes: 0

Views: 115

Answers (3)

Jesus is Lord
Jesus is Lord

Reputation: 15399

Initialize two boolean variables aClicked and bClicked to false.

When the user clicks the button in class A, set aClicked to true. Then perform the following logic:

if (aClicked && bClicked)
{
   new ClassC();
}
aClicked = false;
bClicked = false;

When the user clicks the button in class B, set bClicked to true. Then perform the same logic.

I'd need more details for a better answer.

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 boolean variables from within an anonymous class using the final keyword.

This worked for me. If it doesn't help you, I'll need more of your code.

Also, for the State class, if your logic gets more complicated, I would recommend the FSM approach recommended by Jake Greene.

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 && this.bClicked;
    }
} 

Upvotes: 2

Jake Greene
Jake Greene

Reputation: 5618

Depending on the application, you may wish to look into creating a Finite State Machine. You can build a simple State Machine in Java using enums, as described here

The state machine for your given example could have 4 states:

  1. Waiting
  2. APressed
  3. BPressed
  4. BothPressed

Waiting has two transitions:

  • If buttonA is pressed, transition to APressed
  • If buttonB is pressed, transition to BPressed

APressed has one transition:

  • If buttonB is pressed, transition to BothPressed

BPressed has one transition:

  • If buttonA is pressed, transition to BothPressed

BothPressed has one transition:

  • Immediately transition to Waiting

An instance of C is created the moment BothPressed is reached.

As you can see, State Machines require a lot of overhead. Unless you see the logic for your application getting much more complicated, I would recommend the boolean approach.

Upvotes: 1

Servy
Servy

Reputation: 203827

Attach the same event handler to both buttons. In that handler increment a counter. When the counter reaches two create an instances of Class C.

Upvotes: 0

Related Questions