1

JFrame1 and JFrame2 are both classes I wrote that extend javax.swing.JFrame.

There is a button (a JButton) located in JFrame1 and I registered a MouseClicked event with it. When I click the button on JFrame1, it closes and JFrame2 is visible. (They don't have anything that links them other than JFrame2.setVisible(true))

So, should there be a relationship (association, aggregation, composition, dependency,...) between these two classes in my class diagram, and if yes, which relatioship?

Christophe
  • 68,716
  • 7
  • 72
  • 138
lurenia
  • 19
  • 3
  • [How to use Buttons, Check Boxes and Radio Buttons](https://docs.oracle.com/javase/tutorial/uiswing/components/button.html) – Abra May 30 '21 at 10:31
  • That has nothing to do with what I asked. – lurenia May 30 '21 at 10:34
  • 2
    I know. Just my way of letting you know that you should register an `ActionListener` with a `JButton` and not a `MouseListener` because clicking a `JButton` with the mouse is not the only way to activate the `JButton`. For example, if the `JButton` has keyboard focus, pressing the space bar on the computer keyboard will activate the `JButton`. And if the `JButton` is the default button, pressing will also activate the button, even if it does not have keyboard focus. Besides which, a _Swing_ application should have one `JFrame` only. Use `javax.swing.JDialog` instead of another `JFrame` – Abra May 30 '21 at 11:07

1 Answers1

1

A class diagram represents a structural view of your classes.

So if one class just happens to use the other in its implementation or during the execution, it’s not sufficient to make an association. So you could leave them completely separate in the diagram.

Nevertheless, the two classes are not fully independent either. JFrame1 needs JFrame2. It’s not mandatory, but you could represent this as a usage dependency (a dotted arrow from 1 to 2, with an explicit «use»)

Christophe
  • 68,716
  • 7
  • 72
  • 138