2

I am making an application in swing. I have more than 20 jpanel. I want to display that jpanel to which I want display :

example:

WelcomePanel welcomePanel = new WelcomePanel();
BindingDetailsPanel bindingDetailsPanel = new BindingDetailsPanel();
FooterMainPanel footerMainPanel = new FooterMainPanel();
KeyMessageSetupPanel keyMessageSetupPanel = new KeyMessageSetupPanel();
SMSCSetupPanel sMSCSetupPanel = new SMSCSetupPanel();
LiveProcessorDetailsPanel runTimePanel = new LiveProcessorDetailsPanel();
RuntTimeInformationPanel welcomePanel=new RuntTimeInformationPanel();
BlockedSmscDetailsPanel blockedSmscDetailsPanel = new BlockedSmscDetailsPanel();

I am making object of above listed panel in my main jFrame. It is working fine

because:

public mainFrame(){
    requestResponseReportPanel.setVisible(false);
    keyMessageSetupPanel.setVisible(false);
    sMSCSetupPanel.setVisible(false);
    welcomePanel.setVisible(true);
    keyMessageDetailsPanel.setVisible(false);
    blockedSmscDetailsPanel.setVisible(false);
    runTimePanel.setVisible(false);
}

first time when my application starts it will display only one panel welcome panel which I am making visible welcomePanel.setVisible(true); and I have menu if I click it should be display keyMessageSetupPanel following is code to display this panel:

private void keyMessageSetupMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                                        
    try {
        mainFrameContentPanel.add(keyMessageSetupPanel, BorderLayout.CENTER);
        keyMessageSetupPanel.setVisible(true);
        sMSCSetupPanel.setVisible(false);
        welcomePanel.setVisible(false);
        homeButton.setVisible(true);
        bindingDetailsPanel.setVisible(false);
        requestResponseReportPanel.setVisible(false);
        keyMessageDetailsPanel.setVisible(false);
        blockedSmscDetailsPanel.setVisible(false);
    } catch (Exception e) {
      printStackTrace();
    }
}   

My question is if I used like this my application is working. But it loads all panel at runtime and takes long time to load. If I did not like this it displays welcomePanel and if I went to another panel by clicking ( menu or button ) :

Example:

  1. Welcome Panel displays
  2. I went keyMessageSetupPanel (This is working fine)
  3. Now I went to bindingDetailsPanel (it works)
  4. But the problem is I want again to keyMessageSetupPanel (it does not hides the bindingDetailsPanel).

Edited This is code which displays bindingDetailsPanel

mainFrameContentPanel.add(bindingDetailsPanel, BorderLayout.CENTER);
bindingDetailsPanel.setVisible(true);
keyMessageSetupPanel.setVisible(false);
sMSCSetupPanel.setVisible(false);
welcomePanel.setVisible(false);
homeButton.setVisible(true);
requestResponseReportPanel.setVisible(false);
keyMessageDetailsPanel.setVisible(false);
blockedSmscDetailsPanel.setVisible(false);

Please help me. Note If you do not understand comment where you did not understand my question I will try to explain.

Thanks

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
Yubaraj
  • 3,800
  • 7
  • 39
  • 57
  • Could you post the code enabling bindingDetailsPanel? – Paweł Piecyk Aug 18 '13 at 13:05
  • Sure but I am giving example of `bindingDetailsPanel`. I have problem like this for all (>20) panel. See my edited question where I am providing code to display `bindingDetailsPanel` – Yubaraj Aug 18 '13 at 13:07

1 Answers1

3

The basic reason why your code isn't working is that you're not calling revalidate() and repaint() on the container that holds the swapping JPanels after doing your swapping. These calls tell the layout managers to re-layout the components held and the repaint manager to repaint the container and its children.

Having said this, a better way to swap visible components in a container is to use the CardLayout which was built explicitly for this.

For example, please have a look here: https://stackoverflow.com/a/6176115/522444

And for another example of the above program, this time with some better separation of concerns:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

class MainClass implements ReceiveAction {
   private JPanel mainPanel = new JPanel();
   private JMenuBar menuBar = new JMenuBar();
   private JMenu chooseViewMenu = new JMenu("Choose View");

   public MainClass() {
      Registration registration = new Registration();
      ButtonPanel buttonPanel = new ButtonPanel();

      ReceiveAction[] recActions = {this, buttonPanel};
      new ViewSwapControl(registration, recActions);

      buttonPanel.setBorder(BorderFactory.createTitledBorder("Button Panel"));
      registration.setBorder(BorderFactory.createTitledBorder("Registration Panel"));

      mainPanel.setLayout(new BorderLayout());
      mainPanel.add(registration, BorderLayout.CENTER);
      mainPanel.add(buttonPanel, BorderLayout.SOUTH);

      menuBar.add(chooseViewMenu);
   }

   public JComponent getMainPanel() {
      return mainPanel;
   }

   public JMenuBar getMenuBar() {
      return menuBar ;
   }

   @Override
   public void addAction(Action action) {
      chooseViewMenu.add(new JMenuItem(action));
   }

   private static void createAndShowUI() {
      MainClass mainInstance = new MainClass();


      JFrame frame = new JFrame("Registration");
      frame.getContentPane().add(mainInstance.getMainPanel());
      frame.setJMenuBar(mainInstance.getMenuBar());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }

}

class ViewSwapControl {
   private Registration registration;
   private List<Action> actionList = new ArrayList<>(); 

   public ViewSwapControl(Registration registration, ReceiveAction... recActions) {
      this.registration = registration;
      for (final String keyText : Registration.KEY_TEXTS) {
         actionList.add(new MyAction(keyText));
      }
      for (ReceiveAction receiveAction : recActions) {
         for (Action action : actionList) {
            receiveAction.addAction(action);
         }
      }
   }

   @SuppressWarnings("serial")
   private class MyAction extends AbstractAction {
      public MyAction(String name) {
         super(name);
      }

      @Override
      public void actionPerformed(ActionEvent evt) {
         if (registration != null) {
            registration.swapView(getValue(NAME).toString());
         }
      }
   }

}

interface ReceiveAction {
   public void addAction(Action action);
}

@SuppressWarnings("serial")
class ButtonPanel extends JPanel implements ReceiveAction {

   public ButtonPanel() {
      setLayout(new GridLayout(1, 0, 10, 0));
   }

   @Override
   public void addAction(Action action) {
      add(new JButton(action));
   }
}

@SuppressWarnings("serial")
class Registration extends JPanel {
   // use these same constants as button texts later
   private static final Dimension PREF_SIZE = new Dimension(450, 300);
   public static final String USER_AGREEMENT = "User Agreement";
   public static final String USER_INFO = "User Information";
   public static final String ENROLLMENT = "Enrollment";
   // we'll extract them from this array
   public static final String[] KEY_TEXTS = {USER_AGREEMENT, USER_INFO, ENROLLMENT};
   private CardLayout cardlayout = new CardLayout();
   private JPanel cards = new JPanel(cardlayout);

   public Registration() {
      cards.add(createUserAgreePanel(), USER_AGREEMENT);
      cards.add(createUserInfoPanel(), USER_INFO);
      cards.add(createEnrollmentPanel(), ENROLLMENT);
      setLayout(new BorderLayout());
      add(cards, BorderLayout.CENTER);
   }

   @Override
   public Dimension getPreferredSize() {
      return PREF_SIZE;
   }

   private JPanel createEnrollmentPanel() {
      JPanel enrol = new JPanel();
      enrol.add(new JLabel("Enrollment"));
      return enrol;
   }

   private JPanel createUserAgreePanel() {
      JPanel userAgree = new JPanel();
      userAgree.add(new JLabel("User Agreement"));
      return userAgree;
   }

   private JPanel createUserInfoPanel() {
      JPanel userInfo = new JPanel();
      userInfo.add(new JLabel("User Information"));
      return userInfo;
   }

   public void swapView(String key) {
      cardlayout.show(cards, key);
   }

}
Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373