6 Replies - 15686 Views - Last Post: 16 March 2012 - 03:32 AM Rate Topic: -----

#1 natanel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-February 12

how to do it right: swing gui, panels, communication between compo

Posted 15 March 2012 - 05:57 AM

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

Is This A Good Question/Topic? 0
  • +

Replies To: how to do it right: swing gui, panels, communication between compo

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

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.
Was This Post Helpful? 0
  • +
  • -

#3 natanel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-February 12

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?)
Was This Post Helpful? 0
  • +
  • -

#4 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 120
  • View blog
  • Posts: 1,384
  • Joined: 14-January 10

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.
Was This Post Helpful? 0
  • +
  • -

#5 natanel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-February 12

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?
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

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

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);
Was This Post Helpful? 1
  • +
  • -

#7 natanel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-February 12

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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1