Solved

design help

Posted on 2003-11-12
7
245 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
  • 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now