Solved

design help

Posted on 2003-11-12
7
250 Views
Last Modified: 2010-03-31
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();
    }
}
-----------------------------------------------------------------------------------
0
Comment
Question by:dhaya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 25 total points
ID: 9731219
1) Your code doesn't compile (no LayoutBean for one)
2) I don't understand the question.
0
 

Author Comment

by:dhaya
ID: 9731291
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
 

Author Comment

by:dhaya
ID: 9731367
just take away the layoutbean , it just has a specific layout, it can just be a frame.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:dhaya
ID: 9731646
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
 
LVL 7

Accepted Solution

by:
tomboshell earned 25 total points
ID: 9731863
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
 

Author Comment

by:dhaya
ID: 9731997
         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
 
LVL 7

Expert Comment

by:tomboshell
ID: 9732122
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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question