Java Swing - how to update GUI Objects ie. JTextField value from sub class in same package?

Public class Parent { private void init() { // ... combo. AddActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Object selected = combo.getSelectedItem(); textField. SetText(getTextBasedOnSelection(selected)); } }); // ... } /** * Returns the text to display when the given object is selected.

* Subclasses may override this method to display what they want */ protected String getTextBasedOnSelection(Object selected) { return selected.toString(); } // ... }.

I hope I get your problem right. You have a View Component with several subviews and you want to update one because of the changes done inside the other one. Therefore you write an action listener for your combobox in the main View: comboBox.

AddActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) { textField. SetText(comboBox.getSelectedItem()); } }).

Instead of inter-connecting components directly, I recommend to apply the Mediator pattern: Create a subclass of JPanel (e.g. XyzPane) where you put all your components in. This class becomes the Mediator.It listens for events of its components updates the components as needed fires its own events, if needed (this allows it to be a part of a parent Mediator: grouping components in Panes and then nesting the Panes).

1 @trashgod: thanks for the link. But maybe I didn't mean the Mediator pattern in the strict sense as shown in the Wikipedia example. Rather like the example shown by Anthea or JB Nizet: the comboBox and the textField are not directly connected, only indirectly via a listener registered by the parent.

– Puce Dec 1 at 20:43.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions