Hi all, i have a variable in a JFRAME that i need to use in a JPANEL. How can i do that.

Example: MainProgram that launches Java Application.
Window1 that is a JFRAME
Window2 that is a JPANEL

In Window1 I have a variable declared example int X = 0; When the program runs int X is changed to 200. After the change I want that Window2 uses the same variable X that know it is 200 to draw a line.

Thanks

Recommended Answers

All 22 Replies

Create a method in the class that uses JFrame that returns the value of x.

public int getX()
{
return x;
}

Then just called that method from your JPanel class.(I assume you know how to do that)

Sadly you assume wrong.

public int startX = 0;
      public int startY = 0;
 
public void mousePressed (MouseEvent ev) 
	 {
		    startX = ev.getX();
		    startY = ev.getY();
		    System.out.println("Mouse Pressed " + "X " + startX + " Y " + startY);
	 }

The code above is set in the JFARME. now i want to use the startX and startY into the JPANEL.

startX and startY should be private or protected.

public int getStartX()
{
	return startX;
}

I trust you can write the method for startY on your own.

hi and thanks for the quick reply. Im just a guy started learning java 1 month ago so please bare with me :)

in the JFRAME have:

int startX = 0;
int startY = 0; 

public void mousePressed (MouseEvent ev) 
	 {
		    startX = ev.getX();
		    startY = ev.getY();
		    System.out.println("Mouse Pressed " + "X " + startX + " Y " + startY);
	 }

public int getStartX()
	{
		return startX;
	}

now in the JPANEL i created:

public void setStartX (int startX)
{

}

AM i on the right track if so what should i enter between the curly brackets?

JPanel doesn't know the value of startX yet, so you can't pass that value in as a parameter. Instead, you should pass in the JFrame object and call the getStartX method inside of setStartX.

i would love to see some code :P an example would be gratefull. thanks

Based on what you already wrote, you know how to pass in parameters and call methods. You should be able to figure this one out.

Quick question, I assumed you had the JFrame and JPanel is separate classes, is this true?
If you have learned about creating instances of a class(objects) then to call a method from that class you precede the method name with the object name and a "."

If not, then we will then guide you in the proper direction.

yes my JFRAME is found in a different class then my JPANEL. my overall progress so far:

public class PlottingWindow extends JFrame implements ActionListener, MouseListener
	{
		private int startX;
		private int startY; 
		private int endX;
		private int endY;
		
		PlottingPanel pPanel = new PlottingPanel();
		JPanel panel = new JPanel();
		
		/* Constructor*/
		public PlottingWindow()
		{
			super("Plotting Panel");
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
			pPanel.addMouseListener(this);
			
			panel.setLayout(new BorderLayout(20, 20));
			panel.add(pPanel, "Center");/*Put plotting panel in centre region*/
			
			setContentPane(panel);
		}
		
		public void mousePressed (MouseEvent ev) 
		 {
			    pPanel.setStartX(ev.getX());
			    startY = ev.getY();
		 }
			
		public void mouseReleased (MouseEvent ev)
		{
			    endX = ev.getX();
			    endY = ev.getY();

		}
		
		public void mouseClicked(MouseEvent ev) {}
		public void mouseEntered(MouseEvent ev) {} 
		public void mouseExited(MouseEvent ev) {}
		
	}

JPANEL

public class PlottingPanel extends JPanel 
{
	
	private int startX;
	private int startY; 
	private int endX;
	private int endY;
	   
	protected void paintComponent (Graphics g)
	{
		super.paintComponent (g);
		Graphics2D graphics = (Graphics2D) g;
	graphics.

        }
public int getStartX()	
{
		return startX;
}	

public void setStartX(int startX)
{
		this.startX = startX;
}

}

So to explain my intention: I want to click on the JPANEL and after i release the mouse button a traingle would be drawn on the JPANEL where the mouse was dragged.

I had to remove many lines of my code. If anyone of you want my code just ask for it and i will pm it to you

Didn't you want the JFrame to set the value of startX and then use that value in the JPanel?

I want the variable to be set by the below mouse events:

public void mousePressed (MouseEvent ev) 
		 {
			    pPanel.setStartX(ev.getX());
			    startY = ev.getY();
		 }
			
		public void mouseReleased (MouseEvent ev)
		{
			    endX = ev.getX();
			    endY = ev.getY();			    
		}

These are found in the JFRAME to my knowledge. I will be sending my whole code to you by pm.

I don't know why your making this so complicated. As far as you have told me, you want the value of startX that is in the JFrame class, be accessible in the JPanel class.

This is a simple thing to do. Inside the JFrame class, you write a method:

public int getStartX()
{
return startX;
}

you don't need a setStartX because you set the value inside of this class(according to what you said)

So, in your mousePressed method, you just set startX = ev.getX().

I don't know why you placed the get and set methods inside the JPanel class either...again, going back to what you said, the value is set inside the JFrame class, which means that is the class that will use those methods, not JPanel. So remove those methods unless you need to use the values from JPanel somewhere else!

To access the value of startX that it is in the JFrame class
create an object of the JFrame

public class PlottingPanel{....

PlottingWindow pw = new PlottingWindow();
//Then to use the value of startX from the PlottingWindow class, you call the get Method you wrote in that class and set some variable to equal that!!
int value_of_startX_from_JFrame = pw.getStartX();

Now im starting to understand a little bit :) Im a completee idiot in programming an i need this credit to continue my course :(. Now i did the things you told me :) and i understood them but now I need to know how to use the value that is in startX.

basically i need to draw a rectangle by doing g.drawRec(intx,inty,intx,inty);

Sorry for being an IDIOT

basically i need to draw a rectangle by doing g.drawRec(intx,inty,intx,inty);

Sorry for being an IDIOT

Do you mean for this line>:

graphics.drawRect(startX, startY, endX, endY);

That is in your JPanel class? Do you want startX and startY here to be the value from the JFRame?

Yes :) because me target is to have when mouse pressed get startX and startY and when moused released get endX and endY :) by doing that i would draw the rectangle in the program i sent you. thats my first step for zooming :)

Yes :) because me target is to have when mouse pressed get startX and startY and when moused released get endX and endY :) by doing that i would draw the rectangle in the program i sent you. thats my first step for zooming :)

Ok then!

//using the PlottingWindow object
PlottingWindow pw = new PlottingWindow();
...
//pw.getStartX() returns an startX from the JFrame, therefore, pw.getStartX() is the value of startX from the JFrame.
//Therefore, to use this value in your drawRect(), simply put it in!
graphics.drawRect(pw.getStartX(),pw.getStartY(),pw.getEndX(),pw.getEndY());//you will need to create the get method for the other variables you need, startY,endX,endY

Akill10: I think the reason he defined setStartX in PlottingPanel is so that the data would only be sent after a mouse click. With your solution, PlottingPanel has no idea if the mouse has been clicked yet, and therefore startX would likely be zero (or undefined or something).

Akill10: I think the reason he defined setStartX in PlottingPanel is so that the data would only be sent after a mouse click. With your solution, PlottingPanel has no idea if the mouse has been clicked yet, and therefore startX would likely be zero (or undefined or something).

But startX gets set in the mousePressed method. So ,Data will only be sent once that method is called(a mouse click occurs). And once that method is called, startX has a value, and therefore getStartX() has a value. The mousePressed() function is called once after every time a mouse button is pressed.

I would honestly like to know why PlottingPanel would have no idea it has been pressed. If it is inside the mousePressed method, it knows it was pressed.

I would honestly like to know why PlottingPanel would have no idea it has been pressed.

Because PlottingWindow implements MouseListener and PlottingPanel does not. PlottingPanel will not receive the MouseEvent.

Because PlottingWindow implements MouseListener and PlottingPanel does not. PlottingPanel will not receive the MouseEvent.

PlottingPanel pPanel = new PlottingPanel();
...
pPanel.addMouseListener(this);

He adds the listener to the PlottingPanel using the listener interface implemented by PlottingWindow.

The only problem that may aris is that he may need to declare the variables and get methods static, because creating another PlottingWindow object inside the PlottingPanel to access the getStartX() may cause problems with stack overflow .

@OP, is this thread done now?

Found out another way !! thanks anyways

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.