This is my second post related to Java Swing, so pardon me if my question is too straightforward. I'm trying to get multiple JPanels to communicate with each other. I'm building a simple 2D grid that I can add walls/blocked cells to, and then run a simple Floodfill/A* Search algorithm on (given start and goal locations).
To isolate my issue, I decided that working with an example would be easier. So I created a simple application, that allows the user to write to a text box, provided that he/she has clicked on a "Start" button. Once writing to the text box is done, the user can click on the "Start" button to flip it to the "Stop" state. In the "Stop" state, the user cannot add any text to the text box (i.e. the application should not register any keystrokes at all). This is a simple problem that really brings out my question here. Here's how the UI looks like right now:
My question: I should be able to write when the button shows "Stop" (since it is in edit mode) and I shouldn't be able to write in the text area when the button shows "Start" (since it is not it edit mode). However, from the above images, you can see that I'm able to write in the text area in any case. How do I then make the editing of the text area dependent on the button state?
Here's my code which attempts at setting up that connection between the button panel and the text panel, but it somehow isn't working as expected.
I looked at StackOverflow posts here and here, but frankly, the answers didn't seem clear to me.
SimpleTextPanel:
public class SimpleTextPanel extends JPanel implements PropertyChangeListener, ChangeListener {
private boolean canWrite;
public SimpleTextPanel() {
// set the border properties
canWrite = true;
TitledBorder title = BorderFactory.createTitledBorder("Simple Text Panel");
title.setTitleColor(Color.BLACK);
title.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,
Color.DARK_GRAY, Color.GRAY));
this.setBorder(title);
JTextArea editSpace = new JTextArea(10, 20);
editSpace.setEditable(true);
editSpace.addPropertyChangeListener(this);
this.add(editSpace);
}
@Override
public void stateChanged(ChangeEvent changeEvent) {
JButton button = (JButton)changeEvent.getSource();
canWrite = button.getText().equals("Start");
}
@Override
public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
JTextArea area = (JTextArea)propertyChangeEvent.getSource();
if(!canWrite) area.setText((String)propertyChangeEvent.getOldValue());
}
}
SimpleButtonPanel:
public class SimpleButtonPanel extends JPanel implements ActionListener {
JButton switchButton;
private boolean canWrite = true;
public SimpleButtonPanel(SimpleTextPanel txt) {
// set the border properties
TitledBorder title = BorderFactory.createTitledBorder("Simple Button Panel");
title.setTitleColor(Color.BLACK);
title.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED,
Color.DARK_GRAY, Color.GRAY));
this.setBorder(title);
switchButton = new JButton("Start");
switchButton.addActionListener(this);
switchButton.addChangeListener(txt);
this.add(switchButton);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(switchButton.getText().equals("Start")) {
switchButton.setText("Stop");
canWrite = false;
} else if(switchButton.getText().equals("Stop")) {
switchButton.setText("Start");
canWrite = true;
}
}
}
SimpleExampleTest:
public class SimpleExampleTest extends JFrame {
public SimpleExampleTest() {
setLayout(new BorderLayout());
setTitle("Simple Example");
setSize(300, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
SimpleTextPanel text = new SimpleTextPanel();
SimpleButtonPanel button = new SimpleButtonPanel(text);
add(text, BorderLayout.NORTH);
add(button, BorderLayout.SOUTH);
//button.addPropertyChangeListener(text); // so that text editor knows whether to allow writing or not
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
SimpleExampleTest ex = new SimpleExampleTest();
ex.setVisible(true);
}
});
}
}
Any/all help is appreciated. Thank you!