1

I have a swing application. Here is simplified view of what i am trying to do.

enter image description here

The mainFrame is the parent frame that holds all the components in the application. It has a child called jPanel.

The jPanel has a child called button. When button is clicked i want to remove 'jPanel' from mainFrame and add a different panel.

NOTE: the buttom could be a direct child of the jPanel or a child of the jPanel's child (ie:jPanel>>some_other_panel>>button)

Basically i need a BroadcastReciever type of functionality that Android has. ( Android BroadcastReciever Example

Community
  • 1
  • 1
codeNinja
  • 1,442
  • 3
  • 25
  • 61

2 Answers2

3

"NOTE: the buttom could be a direct child of the jPanel or a child of the jPanel's child"

Not gonna happen. a component can only have one parent container.

"The jPanel has a child called button. When button is clicked i want to remove 'jPanel' from mainFrame and add a different panel."

A much cleaner way than adding an removing panels is to use a CardLayout where panels are "layered" and navigable through CardLayout's methods like show(), previous(), next(). See How to Use CardLayout. See a simple example here and if you happen to be using GUI Builder tool, see How to use CardLayout with Netbeans GUI Builder. Even if you aren't using GUI Builder, I'd still look at the link to get a visual of how it works.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Yes a component can have only one parent. What i was trying to say was that the Jpanel has some childJpanel which has this button. (the jpanel is not the parent of the button in this case. Rather childJpanel is). – codeNinja Mar 11 '14 at 18:42
  • I dont think using CardLayout is the answer. What i want to do is update the parent component from a child's child. Usually Card layout is for updating parent component from a child. – codeNinja Mar 11 '14 at 18:45
  • _"Card layout is for updating parent component from a child."_ - Not true. Try it. Have the parentPanel us a CardLayout and the actionPerformed of the button showing a different panel of the `parentPanel` I don't see anything wrong with that logic. Other than that, your question is too vague and requires a minimal complete runnable example to illustrate your problem. – Paul Samsotha Mar 11 '14 at 18:52
  • The main problem is how do i refer to the parentPanel from the buttons actionPerformed()? – codeNinja Mar 11 '14 at 19:03
  • 1
    +1 for the CardLayout. `how do i refer to the parentPanel from the buttons actionPerformed()?` - you know the source of the event is the button, so you can always use the `getParent()` method. Or you can always create a more formal interface for all you panels to implement so that they have access to the CardLayout and the appropriate card to display. – camickr Mar 11 '14 at 19:13
0

i ended up doing the following. Not the best but works.

//get the container
Container tempContainer = MetricTablesX.this.getTopLevelAncestor();
if (tempContainer == null || tempContainer.getComponentCount() == 0) {
    return;
}

//get the root pane
Component rootPane = tempContainer.getComponent(0);
if (rootPane == null || !(rootPane instanceof JRootPane)) {
    return;
}
JRootPane pane = (JRootPane) rootPane;
if (pane == null || pane.getComponentCount() == 0) {
    return;
}

//get the layer Pane
Component jLayerPane = pane.getComponent(1);
if (jLayerPane == null || !(jLayerPane instanceof JLayeredPane)) {
    return;
}
JLayeredPane layerPane = (JLayeredPane) jLayerPane;
if (layerPane == null || layerPane.getComponentCount() == 0) {
    return;
}

//get the junk panel
Component jPanel = layerPane.getComponent(0);
if (jPanel == null || !(jPanel instanceof JPanel)) {
    return;
}
JPanel junkPanel = (JPanel) jPanel;
if (junkPanel == null || junkPanel.getComponentCount() == 0) {
    return;
}

//get the main panel
Component mPanel = junkPanel.getComponent(0);
if (mPanel == null || !(mPanel instanceof JPanel)) {
    return;
}
JPanel mainPanel = (JPanel) mPanel;
if (mainPanel == null || mainPanel.getComponentCount() == 0) {
    return;
}

//get the dashHolder
for (int i = 0; i < mainPanel.getComponentCount(); i++) {
    Component dPanel = mainPanel.getComponent(i);
    if (dPanel == null || !(dPanel instanceof JPanel)) {
        return;
    }
    JPanel dashHolderPanel = (JPanel) dPanel;
    if (dashHolderPanel.getName().equalsIgnoreCase("dashHolderPanel")) {
        RetailRegionDashboard retailRegionDash = new RetailRegionDashboard(cell.mtr);
        dashHolderPanel.removeAll();
        dashHolderPanel.add(retailRegionDash);
        dashHolderPanel.revalidate();
        dashHolderPanel.repaint();
    }
}
codeNinja
  • 1,442
  • 3
  • 25
  • 61