Hi this is a design question more than anything. Lets say I have a JTabbedPane with 2 tabs, A & B. On tab A is a button. When I click tab A it performs some processing and then disables tab B. The only way I can think of doing this is to add a listener to the button that has a reference to the tabbedPane so that in the event processing it can call the setEnabled method to disable tab B. This doesnt sound right to me - is there a better way? I get the feeling that i`m missing a fundamental event processing point here! I did think of another way involving singletons etc, but that was even nastier! Thanks Rich
Welcome to the Ranch Rich, The fundamentals needed for the GUI are: Do not acces the state of a GUI class from outside the Drawing-dispatching thread if the GUI has already been visible. This includes not to call fireXXX(XXEvent) methods from outside such thread. Never clog the mentioned thread. Thus your solution is ok.
SCJP2. Please Indent your code using UBB Code
posted 14 years ago
Hi Thanks for the reply. In my example I would have to pass a reference to the JTabbedPane into panel B and then into the button so that the buttons listener can set the state of the tabbed pane. Its the passing around of object references that bothers me as it doesnt seem very clean. I read in another thread about writing a "state notifier". In my example it would work like this : -- TabbedPane implements StateNotify interface - something like newState(String name, int newState) -- TabbedPane registers interest in a particular state with the notifier singleton something like Notifier.getInstance().registerInterest("TAB A STATE", this) -- When the button on the other pane is clicked, the listener fires some code like Notifier.getInstance().setState("TAB A STATE", 3); -- When the notifier recieves a new state it checks to see if any other objects have registered an interest. If they have it calls their newState() method, which takes the appropriate action. (I just typed that on the fly, so sorry if its a little hazy. And obviously you'd use constants for the names and the states.) Using this, the button listener does not need to have a reference to the JTabbedPane (or multiple object references if neccessary), and everything will still take place in the same thread. It seems a little neater, although it comes with more execution overhead. If passing object references around is the correct way, then thats cool - I just don't want to write rubbish code!
posted 14 years ago
In my example I would have to pass a reference to the JTabbedPane into panel B and then into the button so that the buttons listener can set the state of the tabbed pane.
Only the ActionListener for the button needs a reference to the JTabbedPane. This can be easily accomplished in two ways: A) At the time of creation of the ActionListener pass it a reference to the JTabbedPane in the constructor. This reference will be assigned to a private field. B) The ActionListener is an inner class of the class that declares the JTabbedPane as one of its fields. Thus the ActionListener will have access to such field. Do not bother to implement the "StateNotifier" unless there are more objects interested in modifying their behavior/state depending on the pressing of the button. If you are only concern about the way to pass the reference to the ActionListener use one on the approaches mentioned above.
In case you do not want to use an extra-linguistic tool to ensure that public classes in a package are only accessible from another package; place a public Facade class in the ui package and make all the other classes in that package "friendly" (package access) . The controller has a reference to the facade object which, as usual, delegates the proper accion to the classes it "protects" --the real gui clases. A good article about the Swing Architecture Overview shows two ways of notifying the model's changes. Though in this article MVC is explained in the context of GUI widgets design. I have seen these very same two ways of notification applied as well to program design in a thread in our magnific OO,UML and Patterns Forum. Look at it for examples on MVC.