Working in Java: I have a JFrame class, and separate classes for my two JPanels that are added to the JFrame. One of the JPanel classes has some buttons in it, which can interact with each other(when I click on one button, it can disable another button). However, I can’t figure out how to get the button to call a method in the other JPanel (written in a separate class).
So, my program look like this:
JFrame
- Jpanel1
- Jpanel2 – This class has my buttons in it, I’m trying to get them to interact with the JPanel1 object.
Any tips appreciated, thanks!
One way to do this is to pass an instance of (to use your terminology) Jpanel1 into Jpanel2. This doesn’t have to be done in the constructor, you can have a
setConnectedPanel(JPanel)method, for example.Here’s some code that demonstrates what you want to do:
MyFrame.java
ActionPanel.java
ReactionPanel.java
As you can see, I tend to override all of my JFrames/JPanels when I write Swing GUIs as I find it easier and more flexible but YMMV.