Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

design help

Posted on 2003-11-12
7
Medium Priority
?
253 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 100 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 100 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

597 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