Linking components from 2 different classes

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 528 Views - Last Post: 20 June 2020 - 11:35 AM Rate Topic: -----

#1 Larry71   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 94
  • Joined: 05-June 12

Linking components from 2 different classes

Posted 28 April 2020 - 05:57 AM

Hi, I have two classes in my code:

First class:

public class Start extends JFrame {

private JButton startButton;
private JButton newClassButton;

public Start()

newClassButton.addActionListener(new ActionListener() {

    @Override
	 public void actionPerformed(ActionEvent e) {

   Next next = new Next();
   next.setVisible(true);

}

});


public static void main(String[] args) {

Start start = new Start()
start.setVisible(true);

}

}




and the second one:

public class Next extends JFrame {

private JButton testButton;

public Next();


}



I want in the actionlistener of the testButton, to be able to change the properties of startButton that belongs to another class.
I have tried in the constructor of the Next Class to put an instance of the Start Class, but still have no access.
Any help?

Is This A Good Question/Topic? 0
  • +

Replies To: Linking components from 2 different classes

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,402
  • Joined: 25-December 13

Re: Linking components from 2 different classes

Posted 28 April 2020 - 05:59 AM

For code in one class to access the contents of an instance of another class, the first class needs a reference to the instance with the contents to be modified.

Since the instance of the Next class is created in the Start class, a reference to the Start class instance could be passed in the Next class's constructor.

This post has been edited by NormR: 28 April 2020 - 06:06 AM

Was This Post Helpful? 0
  • +
  • -

#3 Larry71   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 94
  • Joined: 05-June 12

Re: Linking components from 2 different classes

Posted 28 April 2020 - 06:10 AM

View PostNormR, on 28 April 2020 - 05:59 AM, said:

For code in one class to access the contents of an instance of another class, the first class needs a reference to the instance with the contents to be modified.

Since the instance of the Next class is created in the Start class, a reference to the Start class instance could be passed in the Next class's constructor.


Right.

I have tried

public class Next extends JFrame {

private JButton testButton;
private Start start;

public Next(Start start);

}




and in the constructor

Next next = new Next(start);
next.setVisible(true);




but I get an error "start cannot be resolved to a variable".
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,402
  • Joined: 25-December 13

Re: Linking components from 2 different classes

Posted 28 April 2020 - 06:23 AM

Quote

error "start cannot be resolved to a variable".

The compiler can not find start. Where is it declared?
What value is supposed to be in the variable start?

Do you know about the this variable? It refers to the current instance of the class it is in.
Using this in an inner class to reference the outer class requires qualifying with the name of the class. EG Start.this

This post has been edited by NormR: 28 April 2020 - 06:24 AM

Was This Post Helpful? 1
  • +
  • -

#5 Larry71   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 94
  • Joined: 05-June 12

Re: Linking components from 2 different classes

Posted 28 April 2020 - 06:31 AM

View PostNormR, on 28 April 2020 - 06:23 AM, said:

Quote

error "start cannot be resolved to a variable".

The compiler can not find start. Where is it declared?
What value is supposed to be in the variable start?

Do you know about the this variable? It refers to the current instance of the class it is in.
Using this in an inner class to reference the outer class requires qualifying with the name of the class. EG Start.this


Yes, I knew that was the problem.

I also tried just "this", but also didn't work.

Now it works fine, thank you. I guess it's a poor design, but I had no other options.
Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,402
  • Joined: 25-December 13

Re: Linking components from 2 different classes

Posted 28 April 2020 - 06:36 AM

Quote

but also didn't work.

You should copy and paste here the full text of any error messages that you need help with.
Just saying "it didn't work" is not much to go on.
Was This Post Helpful? 0
  • +
  • -

#7 idlewild   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 19-June 20

Re: Linking components from 2 different classes

Posted 20 June 2020 - 05:43 AM

also you can achieve the same effect with
extends
static

modifiers as in the code below


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

 
public class Start extends JPanel implements ActionListener {
JFrame f=new JFrame();
static JButton  startButton=new JButton("start");
 static  JButton newClassButton=new JButton("new Class");
 
 Start st=this;
Start st2;
public static void main(String[] args) {
 
new Start().go();
}



private void go() {
	// TODO Auto-generated method stub
f.setVisible(true);
f.setSize(400, 400);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
this.add(startButton);this.add(newClassButton);
f.add(this);
  st2=new GoToClass();
startButton.addActionListener(st2);
newClassButton.addActionListener(st2);

}



@Override
public void actionPerformed(ActionEvent arg0) {
	// TODO Auto-generated method stub
	
}
	
}
class GoToClass extends Start implements ActionListener {

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		st2.startButton.setText("i start first");
		newClassButton.setText("i am a new class maker");
	}
	 
}
  



Was This Post Helpful? 0
  • +
  • -

#8 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,402
  • Joined: 25-December 13

Re: Linking components from 2 different classes

Posted 20 June 2020 - 05:44 AM

Could you fix the indentations in the posted code? Unformatted code or sloppy formatted code is harder to read and understand.
Was This Post Helpful? 0
  • +
  • -

#9 idlewild   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 19-June 20

Re: Linking components from 2 different classes

Posted 20 June 2020 - 08:47 AM

with indentations the code would be

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Start extends JPanel implements ActionListener {
	JFrame f=new JFrame();
	static JButton  startButton=new JButton("start");
	static  JButton newClassButton=new JButton("new Class");

	Start st=this;
	Start st2;
	public static void main(String[] args) {

		new Start().go();
	}



	private void go() {
		// TODO Auto-generated method stub
		f.setVisible(true);
		f.setSize(400, 400);
		f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
		this.add(startButton);this.add(newClassButton);
		f.add(this);
		st2=new GoToClass();
		startButton.addActionListener(st2);
		newClassButton.addActionListener(st2);

	}



	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

	}

}
class GoToClass extends Start implements ActionListener {

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		st2.startButton.setText("i start first");
		newClassButton.setText("i am a new class maker");
	}

}





Was This Post Helpful? 0
  • +
  • -

#10 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,402
  • Joined: 25-December 13

Re: Linking components from 2 different classes

Posted 20 June 2020 - 08:59 AM

Thanks, that looks better.

The recommended way to reference a static in a class is to use the classname NOT a reference to an instance of the class.
 st2.startButton
vs 
Start.startButton

This post has been edited by NormR: 20 June 2020 - 09:03 AM

Was This Post Helpful? 0
  • +
  • -

#11 idlewild   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 19-June 20

Re: Linking components from 2 different classes

Posted 20 June 2020 - 09:30 AM

yes thank you . i as well think there is a llogic error in my code.

Were it only one Componenet passed, my code would operate alright.
but here 2 button are set to new values upon only one button click.
class GoToClass extends Start implements ActionListener {
  

	    @Override
	    public void actionPerformed(ActionEvent arg0) {

	        // TODO Auto-generated method stub

	        st2.startButton.setText("i start first");

	        newClassButton.setText("i am a new class maker");

	    }

	}

So Sorry!
Was This Post Helpful? 0
  • +
  • -

#12 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,402
  • Joined: 25-December 13

Re: Linking components from 2 different classes

Posted 20 June 2020 - 10:26 AM

One problem is the variables that are declared as static. The first instance of Start and the instance of GoToClass both access the same variables. They do not have their own copies.
Was This Post Helpful? 1
  • +
  • -

#13 idlewild   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 19-June 20

Re: Linking components from 2 different classes

Posted 20 June 2020 - 10:56 AM

NormR


is this legit

with defining new classes with distinct JButtons?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Start extends JPanel implements ActionListener, ComponentListener {
	JFrame f=new JFrame();
	static JButton  startButton=new JButton("start");
	static  JButton newClassButton=new JButton("new Class");

	Start st=this;
	Start st2;
	public static void main(String[] args) {

		new Start().go();
	}



	private void go() {
		// TODO Auto-generated method stub
		f.setVisible(true);
		f.setSize(400, 400);
		f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
		this.add(startButton);this.add(newClassButton);
		f.add(this);
		st2=new GoToClass();
		startButton.addActionListener(st2 );
		C next=new C();
		newClassButton.addActionListener(next);

	}



	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

	}



	@Override
	public void componentHidden(ComponentEvent arg0) {
		// TODO Auto-generated method stub

	}



	@Override
	public void componentMoved(ComponentEvent arg0) {
		// TODO Auto-generated method stub

	}



	@Override
	public void componentResized(ComponentEvent arg0) {
		// TODO Auto-generated method stub

	}



	@Override
	public void componentShown(ComponentEvent arg0) {
		// TODO Auto-generated method stub

	}

}
class GoToClass extends Start implements ActionListener {

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		startButton.setVisible(false);
	}

}




class C extends Start implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		newClassButton.setVisible(false);

	}


}

Was This Post Helpful? 0
  • +
  • -

#14 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 834
  • View blog
  • Posts: 6,402
  • Joined: 25-December 13

Re: Linking components from 2 different classes

Posted 20 June 2020 - 10:59 AM

Quote

defining new classes with distinct JButtons?

Please explain:
What classes?
What JButtons?

The JButtons should not be static.

Note: Single letter names for classes and variables makes the code hard to read and understand. i is the standard for for loops

Names should describe what the variable contains or what it is used for.

This post has been edited by NormR: 20 June 2020 - 11:01 AM

Was This Post Helpful? 0
  • +
  • -

#15 idlewild   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 19-June 20

Re: Linking components from 2 different classes

Posted 20 June 2020 - 11:11 AM

i have tried and amended the code

could you please focus on the approach of extend-ing classes?

static buttons are there to transcend to the extending class to do what is required
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Start extends JPanel implements ActionListener {
	JFrame f=new JFrame();
	static JButton  startButton=new JButton("button 1 to be invisible");
	static  JButton newClassButton=new JButton("button two to vanish");

	Start st=this;
	Start st2;
	public static void main(String[] args) {

		new Start().go();
	}



	private void go() {
		// TODO Auto-generated method stub
		f.setVisible(true);
		f.setSize(400, 400);
		f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
		this.add(startButton);this.add(newClassButton);
		f.add(this);
		st2=new GoToClass();
		startButton.addActionListener(st2 );
		SecondListener next=new SecondListener();
		newClassButton.addActionListener(next);
  
    

}
class GoToClass extends Start implements ActionListener {

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		startButton.setVisible(false);
	}

}




class SecondListener extends Start implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		newClassButton.setVisible(false);

	}


}




@Override
public void actionPerformed(ActionEvent arg0) {
	// TODO Auto-generated method stub
	
}


 

}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2