design help

looks big, but the question context is in "passing object references only by value"

                  PCPanel1 p1 = new PCPanel1(data1);
                  p1.addComp2();
i have trouble passing the PCMainPanel object also here so that it can be called back by the back button using the reference (oops...reference passed by value..)

how to acheive this without creating one more instance or clone()
--------------------------------------------------------------------------------------------
package com.samples.testmemory;

import javax.swing.*;
import java.awt.*;
import java.beans.*;
import java.io.*;

public class PCApp
{

  public static PCFrame frame;

  public PCApp()
  {
        try
        {
          jbInit();
        }
        catch(Exception e)
        {
          e.printStackTrace();
        }
  }

  private void jbInit() throws Exception
  {
      frame = (PCFrame) Beans.instantiate(getClass().getClassLoader(), PCFrame.class.getName());

      frame.setVisible(true);
      frame.setStatusText2("");
      frame.setStatusText3("");

      frame.setTitle("Passing reference by value test ");
      frame.toFront();
      frame.setVisible(true);
      frame.requestFocus();

      System.out.println("App class jbInit()");
  }

  public void RunProg()
  {
      //if all property file , connections correct
      System.out.println("App class runProg()");
      frame.RunProg();
  }

  public static void main(String[] args)
  {
      //security checking
      System.out.println("Main method");
      PCApp a1 = new PCApp();
      a1.RunProg();
  }

}
-----------------------------------------------------------------------------------
package com.samples.testmemory;

import com.samples.beans.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import com.samples.testmemory.*;

public class PCFrame extends LayoutBean
{
  private PCMainPanel mainPanelClass;
  private PCData DataClass;

  public PCFrame()
  {
        try  {
          jbInit();
        }
        catch(Exception e) {
          System.out.println("Exception: " + e.getMessage());
        }
  }

  private void jbInit() throws Exception
  {
      System.out.println("App class jbInit()");
  }

  public void RunProg()
  {
      //if all property file , connections correct
        try
        {
            DataClass = new PCData("test", "conn");
            System.out.println("Frame class RunProg()");
            launchMainPanel();
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(this, "Error retreiving data. See error log.");
        }
  }

  public void launchMainPanel()
  {
      System.out.println("Launch main panel");
      mainPanelClass = new PCMainPanel(DataClass);
      mainPanelClass.addComp1();
  }
}

-----------------------------------------------------------------------------------
package com.samples.testmemory;

public class PCData
{
  private String dbconn;
  private String dbname;

  public PCData(String databaseName, String conn)
  {
        System.out.println("Data constructor()");
        dbconn = conn;
        dbname = databaseName;
  }
}
-----------------------------------------------------------------------------------
package com.samples.testmemory;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class PCMainPanel
{
  private PCFrame frame1;
  private PCData data1;

  private JPanel panel1;
  private JButton btnDetails;

  public PCMainPanel(PCData data)
  {
      data1 = data;
      try {
          jbInit();
      }
      catch(Exception e) {
          e.printStackTrace();
      }
  }

    public void jbInit() throws Exception  {
    }

    public void addComp1() {

          PCApp.frame.getMasterPanel().removeAll();

          panel1 = new JPanel();
          panel1.add(new JLabel("screen1"));

          btnDetails = new JButton("Claim Details");
          btnDetails.setPreferredSize(new Dimension(110, 22));
          btnDetails.setMinimumSize(new Dimension(110, 22));

          btnDetails.addActionListener(new ActionListener ()
          {
              public void actionPerformed(ActionEvent e)
              {
                  PCPanel1 p1 = new PCPanel1(data1);
                  p1.addComp2();
              }
          });

          panel1.add(btnDetails);

          PCApp.frame.getMasterPanel().add(panel1);
          PCApp.frame.repaint();
          PCApp.frame.validate();
    }
}

-----------------------------------------------------------------------------------
package com.samples.testmemory;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class PCPanel1
{
  private PCFrame frame1;
  private PCData data1;
  private PCMainPanel mainPanel;

  private JPanel panel1;
  private JButton btnBack;

  public PCPanel1(PCData data)
  {
    data1 = data;
  }

  public PCPanel1(PCData data, PCMainPanel PCPanel)
  {
      data1 = data;
      mainPanel = PCPanel;
      try {
          jbInit();
      }
      catch(Exception e) {
          e.printStackTrace();
      }
  }

    public void jbInit() throws Exception  {
    }

    public void addComp2() {
          panel1 = new JPanel();
          panel1.add(new JLabel("screen2"));

          btnBack = new JButton(" Back ");
          btnBack.setPreferredSize(new Dimension(110, 22));
          btnBack.setMinimumSize(new Dimension(110, 22));
          btnBack.addActionListener(new ActionListener ()
          {
              public void actionPerformed(ActionEvent e)
              {
                  mainPanel.addComp1();
              }
          });

          panel1.add(btnBack);

          PCApp.frame.getMasterPanel().add(panel1);
          PCApp.frame.repaint();
          PCApp.frame.validate();
    }
}
-----------------------------------------------------------------------------------
dhayaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimYatesCommented:
1) Your code doesn't compile (no LayoutBean for one)
2) I don't understand the question.
0
dhayaAuthor Commented:
I couldn't use cordlayout for architexture reasons, so i am trying to simulate a back button without creating many instances..

ie my first screen is invoked like this in Frame class

public void launchMainPanel()
  {
      System.out.println("Launch main panel");
      mainPanelClass = new PCMainPanel(DataClass);
      mainPanelClass.addComp1();
  }

my second screen is invoked like this in another class
  PCPanel1 p1 = new PCPanel1(data1);
  p1.addComp2();

???
  PCPanel1 p1 = new PCPanel1(data1, ??????????);
???
i have trouble to get this object here and pass it on to the next class...

i am trying to think how to pass the mainPanelClass to another class so that when back button is clicked i can call the method back without creating one more instance

          btnBack.addActionListener(new ActionListener ()
          {
              public void actionPerformed(ActionEvent e)
              {
                  mainPanel.addComp1();
              }
          });
             
finally, i am just trying to split into many classes the code which can be done in one class

0
dhayaAuthor Commented:
just take away the layoutbean , it just has a specific layout, it can just be a frame.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

dhayaAuthor Commented:
sorry for the long code,

any example that has 2 screens with a back button and can navigate without a cord layout is what i need. (i assume these two screens will be adding and removing components from a frame).
0
tomboshellCommented:
So, you want to have a cache of screens.  Should not be a problem, just think of it differently.  Lets say you have a new seperate class that is a subclass of something like a hashmap.  Then you make sure that this class only has a Singleton instance.  Then when you build your gui data you would add a screen to the cache, and conversly, when needed you could summon it back.  Or you could use an Vector or ArrayList if you want index selection.  

summoning the data back could be something like:
JPanel p = GuiMemory.getMemory().getScreenForName("Screen1");


skeleton code

public class GuiMemory extends HashMap{
  private static GuiMemory gm= null;
  private GuiMemory(){
    // initalization stuff here
  }
  public static GuiMemory getMemory(){
    if(gm == null) gm = new GuiMemory();
    return gm;
  }
  public void addScreen(JComponent jc, String name){
   // or whatever your componenet actually is
   this.put(name, jc);
  }
  public JComponet getScreenForName(String name){
    Object o = get(name);
    // test for null, convert to what you need and return it.
  }
}


That should give you some ideas to play with.  I would just suggest to make sure that you only place what you need into such a memory, and to delete items that are no longer needed.  Any kind of memory object will take up real space in your memory on the running machine and can thereby affect the performance.  
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dhayaAuthor Commented:
         btnDetails.addActionListener(new ActionListener ()
          {
              public void actionPerformed(ActionEvent e)
              {
                  PCPanel1 p1 = new PCPanel1(data1,PCMainPanel.this);
                  p1.addComp2();
              }
          });

i just got some help and looks like i can pass like this

PCMainPanel.this

i am new and don't know if this is the right way to do ..
0
tomboshellCommented:
That way may work, but you will still be creating some parts new again.  But if you are happy with it then thats good.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.