Hi
I would like to know how it is done right (second week of learning)
let say I build GUI with several panels
each panel is a class and have several components inside it
now if I press button A at panel 1 it should (for example) disable all other buttons in all other panels
how this can be done
- at panel level I do not know all other components
- does addActionListener and actionPerformed should be in level of frame (for all), panel (for all component of current panel)or component( for specific component )
I prefer to do it in component level so each component handle his request, but than how it know about the other components
thnkas
6 Replies - 15686 Views - Last Post: 16 March 2012 - 03:32 AM
#1
how to do it right: swing gui, panels, communication between compo
Posted 15 March 2012 - 05:57 AM
Replies To: how to do it right: swing gui, panels, communication between compo
#2
Re: how to do it right: swing gui, panels, communication between compo
Posted 15 March 2012 - 06:51 AM
You could do this by having your JPanel fire an Event. The Oracle tutorials cover this in more detail.
#3
Re: how to do it right: swing gui, panels, communication between compo
Posted 15 March 2012 - 07:37 AM
thanks
think I got it
all panel listen for event from panel A
if event than they enable/disable their component
- implementation issue: I am confused is there example how many class listen for custom fired event
for all different panel classes to be able to response to the fired event
should project use one general "class guiLevelEvent implements ActionListener" and imported it to all of the rest of the classes
but it look to me like breaking the oop
if I use panel C in another GUI it will still wait for the event, even if there is not
any panel in new GUI that going to fire this event
this is way I thought that panel A should be response to disable/enable all components
( panel A response to disable enable all component in GUI)
- am I thinking wrong?
- is there a way doing it (need to send all frame component to panel A constructor?)
think I got it
all panel listen for event from panel A
if event than they enable/disable their component
- implementation issue: I am confused is there example how many class listen for custom fired event
for all different panel classes to be able to response to the fired event
should project use one general "class guiLevelEvent implements ActionListener" and imported it to all of the rest of the classes
but it look to me like breaking the oop
if I use panel C in another GUI it will still wait for the event, even if there is not
any panel in new GUI that going to fire this event
this is way I thought that panel A should be response to disable/enable all components
( panel A response to disable enable all component in GUI)
- am I thinking wrong?
- is there a way doing it (need to send all frame component to panel A constructor?)
#4
Re: how to do it right: swing gui, panels, communication between compo
Posted 15 March 2012 - 09:36 AM
What about having your Panel A have an ActionListener and then have it pass a boolean value to the other panels? Then in each of your panels have something that says if this boolean variable is true then disable these buttons.
#5
Re: how to do it right: swing gui, panels, communication between compo
Posted 15 March 2012 - 09:49 PM
It can be done
any java command get all component in forum?
but what is the right way doing it?
panel A ?
actionlistnner?
other?
any java command get all component in forum?
but what is the right way doing it?
panel A ?
actionlistnner?
other?
#6
Re: how to do it right: swing gui, panels, communication between compo
Posted 15 March 2012 - 10:06 PM
Have a class ButtonRegistry with a static ArrayList<Jbutton>
A static register() mmethod
A static click() method that is called by the ActionListener of all your JPanel
Or use a Singleton
I'll show a static approach a little less complicated
When you create your button call: ButtonRegistry.register(button);
in your actionPerformed() call: ButtonRegistry.click(button);
A static register() mmethod
A static click() method that is called by the ActionListener of all your JPanel
Or use a Singleton
I'll show a static approach a little less complicated
class ButtonRegistry {
static ArrayList<JButton> al = new ArrayList<Jbutton>();
static void register(JButton B)/> {
al.add(B)/>;
}
static void click(JButton B)/> {
for(JButton but : al) {
if(but != B)/>
but.setEnabled(false);
}
}
}
When you create your button call: ButtonRegistry.register(button);
in your actionPerformed() call: ButtonRegistry.click(button);
#7
Re: how to do it right: swing gui, panels, communication between compo
Posted 16 March 2012 - 03:32 AM
Got it
nice and clean
thanks
nice and clean
thanks
Page 1 of 1

New Topic/Question
Reply


MultiQuote







|