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)
- 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? - 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.