I have two questions ( these may be very simple, but I'm concentrating on different things so I need somebody push me in right direction as not to waste time, bold said but that is reality)

  1. Passing object from top component to lower ones
    I have GUI where after successful login main frame keeps hold of user unique ID and pass it down to lower components by the use of constructor. It is very inefficient as the frame holds an panel in which I'm displaying different panels that are often combinations of other panels. So this unique ID is passed down by 1-2 class constructors that do not have any use of it. How can I access variable in the frame from a component 2-3 levels bellow?
  2. Lower component passing data to higher.
    You may say little reverse situation to previous question. Let say that from the frame menu I selected to "view clients" and these are displayed in the panel hold directly by frame. I collect data from DB initialize the "view clients" panel and this displayed in the frame's panel.
    JMenuItem jmiAllClients = new JMenuItem("View Clients", KeyEvent.VK_C);
    jmiAllClients.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.ALT_DOWN_MASK));
    jmiAllClients.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
              mainPanel.removeAll();
              ViewClientsPanel viewCP = new ViewClientsPanel();
              mainPanel.add(viewCP);
              validate();
              repaint();
          }
      });

    VievClientsPanel is a panel with table in, direct table editing is disabled. However if you select a column in a table edit button become enabled and once you hit it full set of selected client data are fill in and showed in different panel. I do this by following peace of code

    editClient = new JButton("Edit Client Details");
    editClient.setEnabled(false);
    editClient.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ae)
        {
            tablePanel.removeAll();
            EditClientPanel ecp = new EditClientPanel((String) table.getValueAt(table.getSelectedRow(), 0));
            tablePanel.add(ecp);
            setVisible(true);
            tablePanel.getParent().validate();
            tablePanel.getParent().repaint();
        }
    });

    I'm not happy with the red section, which is rather difficult to monitor if I will have to apply to some component 2-3 level from target display.

If there are any question I will try to provide more info as need it.

How can I access variable in the frame from a component 2-3 levels bellow?

The class that contains the user ID and password should have a get method. Just call it and return the relevant info to caller. No need to pass it to things that don't need it.

If you can enlighten me I would be thankful.
In simple terms. MainFrame receive userID, here I implement method getUserID() that will return the ID. MainFrame holds MainPanel in which I display any panel as requested, for this association let say AddClientPanel. How is AddClientPanel supposed to access getUserID() method of MainFrame, if I cannot call new as that will create new instance of MainFrame therefore have null return value from getUserID()?

I'm not sure I understand your class structure, so take this with a grain of salt. You could pass the MainFrame as an argument to AddClientPanel's constructor and then store the MainFrame in a static variable in the AddClientPanel class. AddClientPanel could have a get method that, using the static MainFrame variable, returns whatever info you need. (AddClientPanel is a class, right?)

MainFrame
AddClientPanel ... constructor takes MainFrame variable, then stores it in a static variable. Also has getter.
Other classes ... get Mainframe's info by using AddClientPanel's getter.

commented: Thanx for reminder on statics ;) +14

You basically need a main application context that all necessary components have access to and can call upon for access to the various sub-systems in your app. The reference to that context can be supplied via the constructors.

Static methods solved both issues. I think I used them only one or twice since started with Java programming and that was some time ago. So be it as it is I did not have any need for them and absolutely forgotten about it. Hence the need of it now when I try to do extra then just requirements of my assignment. I start to see things differently.

Glad you solved the problem... and yes, static methods can be pretty nice

While statics can be "nice" in some cases, they should generally be reserved for constants and stateless utility methods. Using them to share state can cause all kinds of headaches further down the road. I'd recommend reading through some of this discussion:GlobalVariablesAreBad and perhaps some of this one: SingletonGlobalProblems as well.

I will have look at it as I need to know how to share data across, unfortunately it will not be before I finish this assignment :$.
I have too much work :sad:, on top of all girlfriend took holiday from work and all I hear is "what do you want to do?", "shall we go outside?", "lets go shopping..." :confused:

hehe, then you had better take her out. Reading about design practices can wait :)

While statics can be "nice" in some cases, they should generally be reserved for constants and stateless utility methods. Using them to share state can cause all kinds of headaches further down the road. I'd recommend reading through some of this discussion:GlobalVariablesAreBad and perhaps some of this one: SingletonGlobalProblems as well.

I'll check those articles out, but my first thought is just because something can be dangerous doesn't mean it shouldn't be used. I also haven't had any serious trouble with statics. But maybe I'll change my opinion after reading.

edit: Read the whole first article, skimmed through some of the second. There are some good points there, some of which I have heard before. But I still think (despite the list of 'bad uses of globals' in the first article) that Peter's solution to use a static method and static variable is just as good passing the Object around to every place that he needs it, although I'm less sure of that now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.