Results 1 to 8 of 8
  1. #1
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default [SOLVED] How to pass information from child class to parent class

    This is my first post so I hope I do everything right.

    I can pass information from parent class to child class, but I dont know how to do it the other way around. I have two classes, one is called from the other, and the inner class have two composites (could as well have been jpanels, I guess), one composite inside the other. In the outer class and also in the innermost composite are one textfield and one label. When hitting carriage return in the outer textfield I can pass the text to the inner label.

    Here is what I want to: When hitting carriage return in the inner textfield I want to pass the text in that field to the outer label. (Here should have been a link to make it more clear, but its my first post). Here are the code (btw I use eclipse):

    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.KeyEvent;
    import org.eclipse.swt.events.KeyListener;
    import org.eclipse.swt.graphics.Color;
    import org.eclipse.swt.layout.RowLayout;
    import org.eclipse.swt.widgets.Control;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Text;

    public class TestTextToLabel{

    Text text_0;
    Label label_0;
    Level_1_Class level_1;
    Control[] c;

    public void setLabel0(String s){
    this.label_0.setText(s);
    }

    public TestTextToLabel(Shell shell) {

    KeyListener textKeyListener = new KeyListener() {
    public void keyPressed(KeyEvent e) {
    if (e.widget == (Text) e.widget){
    switch (e.character) {
    case SWT.CR: actionCarrigeReturn(); break;
    default:
    }
    }
    }
    public void keyReleased(KeyEvent e) {
    }
    }; // End KeyListener

    // Text at level zero ... (white color)
    text_0 = new Text(shell, SWT.NONE);
    text_0.setBackground(new Color(shell.getDisplay(),255,255,255));
    text_0.setText("text_0");
    text_0.setData("text_0");
    text_0.addKeyListener(textKeyListener);

    // Label at level zero ... (white color)
    label_0 = new Label(shell, SWT.NONE);
    label_0.setBackground(new Color(shell.getDisplay(),255,255,255));
    label_0.setText("label_0");
    label_0.setData("label_0");

    // Level 1 class
    this.level_1 = new Level_1_Class(shell);

    }
    private void actionCarrigeReturn() {
    // Level zero textfield to level 1 label
    level_1.setLabel1(text_0.getText());
    }

    private static void createAndShowUI()
    {
    Display display = new Display();

    // The shell is level zero (yellow background)
    Shell shell = new Shell(display);
    shell.setLayout(new RowLayout( SWT.VERTICAL));
    shell.setBackground(new Color(display,255,255,177)); //yellow
    shell.setToolTipText("Level 0");

    @SuppressWarnings("unused")
    TestTextToLabel testTextToLabel = new TestTextToLabel(shell);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
    display.sleep();
    }
    display.dispose();
    }

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


    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.KeyEvent;
    import org.eclipse.swt.events.KeyListener;
    import org.eclipse.swt.graphics.Color;
    import org.eclipse.swt.layout.RowLayout;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Text;

    public class Level_1_Class {

    Composite compLevel_1;
    Composite compLevel_2;
    Label labelCompLevel_1Title;
    Label label2;
    Text text2;

    public void setLabel1(String s){
    this.label2.setText(s);
    }

    public Level_1_Class(Shell shell){

    KeyListener textKeyListener2 = new KeyListener() {
    public void keyPressed(KeyEvent e) {
    if (e.widget == (Text) e.widget){
    switch (e.character) {
    case SWT.CR: actionCarrigeReturn2(); break;
    default:
    }
    }
    }
    public void keyReleased(KeyEvent e) {
    }
    }; // End KeyListener

    // Level_1_Class label (still in level zero (the shell)) ... (green)
    Label level_1_ClassTitle = new Label(shell, SWT.NONE);
    level_1_ClassTitle.setText("Level_1_Class");
    level_1_ClassTitle.setBackground(new Color(shell.getDisplay(),162,255,128)); //green

    //----------------------------------------------------------------------
    // composite level 1 ... (grey)
    //----------------------------------------------------------------------
    this.compLevel_1 = new Composite(shell, SWT.BORDER_SOLID);
    compLevel_1.setLayout(new RowLayout(SWT.HORIZONTAL));
    compLevel_1.setBackground(new Color(compLevel_1.getDisplay(),228,228,228));
    compLevel_1.setData("compLevel_1");
    compLevel_1.setToolTipText("compLevel_1");
    // Label at level 1 ... (green color)
    labelCompLevel_1Title = new Label(compLevel_1, SWT.NONE);
    labelCompLevel_1Title.setText("CompLevel 1");
    labelCompLevel_1Title.setBackground(new Color(compLevel_1.getDisplay(),162,255,128)); //green

    //----------------------------------------------------------------------
    // composite level 2 ... (red)
    //----------------------------------------------------------------------
    this.compLevel_2 = new Composite(compLevel_1, SWT.BORDER);
    compLevel_2.setLayout(new RowLayout(SWT.VERTICAL));
    compLevel_2.setBackground(new Color(compLevel_2.getDisplay(),255,0,177));
    compLevel_2.setData("compLevel_2");
    compLevel_2.setToolTipText("compLevel_2");

    // Label at level 2 ... (green)
    Label labelCompLevel_2Title = new Label(compLevel_2, SWT.NONE);
    labelCompLevel_2Title.setText("compLevel2");
    labelCompLevel_2Title.setBackground(new Color(compLevel_2.getDisplay(),162,255,128)); //green

    // Text at level 2 ... (dark yellow)
    this.text2 = new Text(compLevel_2, SWT.NONE);
    text2.setBackground(new Color(compLevel_2.getDisplay(),255,255,128));
    text2.setText("text_2");
    text2.setData("text_2");
    text2.setToolTipText("text_2");
    text2.addKeyListener(textKeyListener2);

    // Label at level 2 ... (blue)
    this.label2 = new Label(compLevel_2, SWT.NONE);
    label2.setBackground(new Color(compLevel_2.getDisplay(),255,255,128));
    label2.setText("label_2");
    label2.setData("label_2");
    label2.setToolTipText("label_2");

    }
    private void actionCarrigeReturn2() {
    String s = this.text2.getText();
    // Here I want to pass the text in text2 to label_0 in the parent class
    }
    }

    A solution would be great ... an explanation would be fantastic!

    I hope someone can lead me in the right direction.

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Use super.method()

    For example:
    Let's say this is the parent class
    Java Code:
    public class Parent
    {
        private int var;
        public void setVarToValue(int value) {
            var = value;
        }
    }
    And this would be the child class
    Java Code:
    public class Child extends Parent
    {
        public void method() {
            super.setVarToValue(28);
        }
    }
    By using super, you can access methods and variables of the parent class, if you have the correct access set, that is.

    There you go. ;)
    I die a little on the inside...
    Every time I get shot.

  3. #3
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    Hi Supamagier

    Thank you for your quick answer. I now let the inner class be extended by the outer class and get "Implicit super constructor TestTextToLabel() is undefined. Must explicitly invoke another constructor". Uptil now I havent used extended classes so I have to go on with some study. Thank you!

  4. #4
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    Yes, it works ... its amazing. I feel I made a BIG step forward in java.

    Well, maybe not. Now I have the opposite problem. Now I cant pass the textfield in the superclass to the label in the subclass. Is it possible?

    tia for any help.

  5. #5
    einarb is offline Member
    Join Date
    Mar 2009
    Posts
    22
    Rep Power
    0

    Default

    Didn't read the entire post so I don't know if this is the best approach for you. But I'll give you an idea of one way to pass objects to subclasses through abstract methods.

    Java Code:
    public abstract class Parent{
      public Parent(){
      }
    
      public void printValue() {
        int newValue = calculateMethod(4)
        System.out.println(newValue);
      }
    
      public abstract int calculateMethod(int i);
    }
    Here, the parent object passes the value 4 to calculateMethod, even though it has no idea how calculateMethod is implemented. What calculateMethod does is up to the Child class to decide.

    You could probably define an abstract method that has a textfield parameter instead.
    Java Code:
    public class Child extends Parent{
      public Child(){
        super();
      }
    
      public int calculateMethod(int i){
        return i * 11;
      }
    }
    Here the subclass multiplies the value it gets by 11. Some other subclass might do something different, like subtract, add or whatever.
    Java Code:
    public TestClass {
      public static void main(String[] args){
        Parent object = new Child();
        object.printValue(); // should print 44
      }
    }

  6. #6
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    Yes, everything works. The superclass is now an abstract class. So its possible to pass from parent to child, and also from child to parent. For completion the final solution (for Eclipse) is enclosed ... maybe someone else on my level can use it.

    It should be possible to close this thread somewhere but I did not find the way.

    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.KeyEvent;
    import org.eclipse.swt.events.KeyListener;
    import org.eclipse.swt.graphics.Color;
    import org.eclipse.swt.layout.RowLayout;
    import org.eclipse.swt.widgets.Control;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Text;

    public abstract class TestTextToLabel{

    Text text_0;
    Label label_0;
    Level_1_Class level_1;
    Control[] c;

    public void setLabel0(String s){
    this.label_0.setText(s);
    }

    public void setLabelInLevel2(){
    String s = text_0.getText();
    setLabel2(s);
    }

    public abstract void setLabel2(String s);

    public TestTextToLabel(Shell shell) {

    KeyListener textKeyListener = new KeyListener() {
    public void keyPressed(KeyEvent e) {
    if (e.widget == (Text) e.widget){
    switch (e.character) {
    case SWT.CR: actionCarrigeReturn(); break;
    default:
    }
    }
    }
    public void keyReleased(KeyEvent e) {
    }
    }; // End KeyListener

    // Text at level zero ... (white color)
    text_0 = new Text(shell, SWT.NONE);
    text_0.setBackground(new Color(shell.getDisplay(),255,255,255));
    text_0.setText("text_0");
    text_0.setData("text_0");
    text_0.addKeyListener(textKeyListener);

    // Label at level zero ... (white color)
    label_0 = new Label(shell, SWT.NONE);
    label_0.setBackground(new Color(shell.getDisplay(),255,255,255));
    label_0.setText("label_0");
    label_0.setData("label_0");
    }
    private void actionCarrigeReturn() {
    // Level zero textfield to level 2 label
    setLabelInLevel2();
    }

    private static void createAndShowUI()
    {
    Display display = new Display();

    // The shell is level zero (yellow background)
    Shell shell = new Shell(display);
    shell.setLayout(new RowLayout( SWT.VERTICAL));
    shell.setBackground(new Color(display,255,255,177)); //yellow
    shell.setToolTipText("Level 0");

    @SuppressWarnings("unused")
    Level_1_Class testTextToLabel = new Level_1_Class(shell);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
    display.sleep();
    }
    display.dispose();
    }

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

    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.KeyEvent;
    import org.eclipse.swt.events.KeyListener;
    import org.eclipse.swt.graphics.Color;
    import org.eclipse.swt.layout.RowLayout;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Text;

    public class Level_1_Class extends TestTextToLabel{

    Composite compLevel_1;
    Composite compLevel_2;
    Label labelCompLevel_1Title;
    Label label2;
    Text text2;

    public void setLabel1(String s){
    this.label2.setText(s);
    }

    public Level_1_Class(Shell shell){
    super(shell);
    KeyListener textKeyListener2 = new KeyListener() {
    public void keyPressed(KeyEvent e) {
    if (e.widget == (Text) e.widget){
    switch (e.character) {
    case SWT.CR: actionCarrigeReturn2(); break;
    default:
    }
    }
    }
    public void keyReleased(KeyEvent e) {
    }
    }; // End KeyListener

    // Level_1_Class label (still in level zero (the shell)) ... (green)
    Label level_1_ClassTitle = new Label(shell, SWT.NONE);
    level_1_ClassTitle.setText("Level_1_Class");
    level_1_ClassTitle.setBackground(new Color(shell.getDisplay(),162,255,128)); //green

    //----------------------------------------------------------------------
    // composite level 1 ... (grey)
    //----------------------------------------------------------------------
    this.compLevel_1 = new Composite(shell, SWT.BORDER_SOLID);
    compLevel_1.setLayout(new RowLayout(SWT.HORIZONTAL));
    compLevel_1.setBackground(new Color(compLevel_1.getDisplay(),228,228,228));
    compLevel_1.setData("compLevel_1");
    compLevel_1.setToolTipText("compLevel_1");
    // Label at level 1 ... (green color)
    labelCompLevel_1Title = new Label(compLevel_1, SWT.NONE);
    labelCompLevel_1Title.setText("CompLevel 1");
    labelCompLevel_1Title.setBackground(new Color(compLevel_1.getDisplay(),162,255,128)); //green

    //----------------------------------------------------------------------
    // composite level 2 ... (red)
    //----------------------------------------------------------------------
    this.compLevel_2 = new Composite(compLevel_1, SWT.BORDER);
    compLevel_2.setLayout(new RowLayout(SWT.VERTICAL));
    compLevel_2.setBackground(new Color(compLevel_2.getDisplay(),255,0,177));
    compLevel_2.setData("compLevel_2");
    compLevel_2.setToolTipText("compLevel_2");

    // Label at level 2 ... (green)
    Label labelCompLevel_2Title = new Label(compLevel_2, SWT.NONE);
    labelCompLevel_2Title.setText("compLevel2");
    labelCompLevel_2Title.setBackground(new Color(compLevel_2.getDisplay(),162,255,128)); //green

    // Text at level 2 ... (dark yellow)
    this.text2 = new Text(compLevel_2, SWT.NONE);
    text2.setBackground(new Color(compLevel_2.getDisplay(),255,255,128));
    text2.setText("text_2");
    text2.setData("text_2");
    text2.setToolTipText("text_2");
    text2.addKeyListener(textKeyListener2);

    // Label at level 2 ... (blue)
    this.label2 = new Label(compLevel_2, SWT.NONE);
    label2.setBackground(new Color(compLevel_2.getDisplay(),255,255,128));
    label2.setText("label_2");
    label2.setData("label_2");
    label2.setToolTipText("label_2");

    }
    private void actionCarrigeReturn2() {
    // The text in text2 is passed to label_0 in the parent class
    super.setLabel0(text2.getText());
    }

    @Override
    public void setLabel2(String s) {
    label2.setText(s);
    }
    }
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  7. #7
    ron87 is offline Member
    Join Date
    Mar 2009
    Posts
    52
    Rep Power
    0

    Default

    you can use super keyword to call constructor of superclass and you can pass values from subclass to superclass
    u can also call methods of superclass and also access variables using super kyeword

  8. #8
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    Yes, ron87, now I know - my problems are solved. Thank you for your input.
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

Similar Threads

  1. how to get the child class in inheritance?
    By java_fun2007 in forum New To Java
    Replies: 7
    Last Post: 09-29-2010, 09:35 AM
  2. unable to pass value from one class to another
    By ddatta8 in forum New To Java
    Replies: 14
    Last Post: 12-28-2008, 02:24 PM
  3. Parent & Child window issues......
    By jithan in forum New To Java
    Replies: 2
    Last Post: 09-20-2008, 09:21 AM
  4. information used in other class
    By Ancalime in forum New To Java
    Replies: 1
    Last Post: 07-17-2008, 04:28 PM
  5. Getting class field information using Reflection
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-24-2008, 03:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •