The most simple case is when your JPanel instance and JButton instance "see" each other in your code i.e.:
JButton button = new JButton ("Click me");
JPanel panel = new JPanel ();
...
container.add (button);
container.add (panel);
In that case you can add some event listener to your panel (or to your button) and change the second component from event handler:
panel.addMouseListener (new MouseAdapter () {
public void mouseClicked (MouseEvent e) {
button.setText ("new text");
}
});
The only thing you should count here is that you should use final
modifier near button
declaration (due to java doesn't have real closures):
final JButton button = new JButton ("Click me");
JPanel panel = new JPanel ();
panel.addMouseListener (new MouseAdapter () {
....
});
More complicated case is when your components don't know about each other or when system state is changed and components state (like button name or something more serious) should be changed too. In this case you should consider using MVC pattern. Here is a very nice tutorial from JavaWorld: MVC meets Swing.