6 Replies - 719 Views - Last Post: 02 November 2012 - 08:26 PM Rate Topic: -----

#1 tanny_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-June 12

How to add object to a JPanel from an outside class

Posted 02 November 2012 - 03:56 PM

I have a class which extends a regular JPanel, and then another class from which I would like to add objects to the JPanel to be displayed. I have an external GUI driver which I have verified is working correctly, by creating the objects in the constructor of my JPanel they are visible.

It seems that I can't add StateIcon objects outside the constructor because my reference to the JPanel (FsaPanel) is wrong somehow. I thought the pan=this; statement would make the global variable equal the FsaPanel.

Everything here compiles, however the StateIcon object is not visible.

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.geom.*;

public class FsaPanel extends JPanel implements FsaListener
{
	private FsaPanel pan;
	
	//constructor
	public FsaPanel()
	{	
		pan=this;
		this.setSize(500,500);
		this.setLayout(null);
	}

	public void statesChanged()
	{
	        StateIcon icon=new StateIcon();
		icon.setLocation(10,10);
		pan.add(icon);
	}
	
	public class StateIcon extends JComponent implements StateListener
	{
		private Ellipse2D.Double circle;
		public StateIcon(State s)
		{
			circle =new Ellipse2D.Double(0,0,80, 80);	
			this.setSize(100,100); 
		}
		
		protected void paintComponent(Graphics g)
		{
			super.paintComponent(g);
			Graphics2D g2d=(Graphics2D)g;
			g2d.setColor(Color.blue);
			g2d.fill(circle);
			
		}
	}	
}



Is This A Good Question/Topic? 0
  • +

Replies To: How to add object to a JPanel from an outside class

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1372
  • View blog
  • Posts: 3,022
  • Joined: 05-April 11

Re: How to add object to a JPanel from an outside class

Posted 02 November 2012 - 04:09 PM

I don't believe you when you say it compiles. Here is why

The constructor
public StateIcon(State s)


How you initialize it
StateIcon icon=new StateIcon();


Where is the missing State object parameter?
Was This Post Helpful? 0
  • +
  • -

#3 tanny_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-June 12

Re: How to add object to a JPanel from an outside class

Posted 02 November 2012 - 04:18 PM

View PostCasiOo, on 02 November 2012 - 04:09 PM, said:

I don't believe you when you say it compiles. Here is why

The constructor
public StateIcon(State s)


How you initialize it
StateIcon icon=new StateIcon();


Where is the missing State object parameter?


SORRY! this is part of a larger FSA implementation, I removed fsa specifics to simplify it for the point of this post. (Obviously not thoroughly enough sorry my fault!)Here is my full code.

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.geom.*;

public class FsaPanel extends JPanel implements FsaListener
{
	private Set <State> panelStates=new HashSet<State>();
	private Fsa f;
	private FsaPanel pan;
	
	//constructor
	public FsaPanel(Fsa f)
	{	
		pan=this;
		f.addListener(this);
		this.f=f;
		this.setSize(500,500);
		this.setLayout(null);
	}

	public void statesChanged()
	{
		System.out.println("States Changed");
		if(panelStates.size()<f.getStates().size())
		{
			System.out.println("added state");
			Set<State> temp = new HashSet();
			temp.addAll(f.getStates());
			temp.removeAll(panelStates);
			Iterator tempIT=temp.iterator();
			State s=null;
			while(tempIT.hasNext()){
				s=(State)tempIT.next();
			}
			
			StateIcon icon=new StateIcon(s);
			int x=s.getXpos();
			int y=s.getYpos();
			icon.setLocation(x,y);
			pan.add(icon);
			s.addListener(icon);
			panelStates.addAll(f.getStates());
			//added-> look in f.getStates to see whats new
		}
		else if(panelStates.size()>f.getStates().size())
		{
			//deleted
		}
	}
	
	public void transitionsChanged()
	{
		System.out.println("Transitions Changed");
	}
	
	public void otherChanged()
	{
		System.out.println("Other change");
	}
	
	public class StateIcon extends JComponent implements StateListener
	{
		State s;
		private Ellipse2D.Double circle;
		public StateIcon(State s)
		{
			this.s=s;
			circle =new Ellipse2D.Double(0,0,80, 80);	
			this.setSize(100,100); 
		}
		public void StateHasChanged()
		{	
			System.out.println("State has changed");
		}
		
		protected void paintComponent(Graphics g)
		{
			super.paintComponent(g);
			Graphics2D g2d=(Graphics2D)g;
			g2d.setColor(Color.blue);
			g2d.fill(circle);
			
		}
	}
	
}


Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1372
  • View blog
  • Posts: 3,022
  • Joined: 05-April 11

Re: How to add object to a JPanel from an outside class

Posted 02 November 2012 - 04:25 PM

Don't do the follow, just use the keyword 'this' instead
pan=this;



You will have to invalidate and maybe even repaint after you have added a new component to your container, before it will not show

Try and resize your frame, and see if the new component shows. A resize will normally make the JPanel invalidate itself :)
Was This Post Helpful? 1
  • +
  • -

#5 tanny_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-June 12

Re: How to add object to a JPanel from an outside class

Posted 02 November 2012 - 04:42 PM

View PostCasiOo, on 02 November 2012 - 04:25 PM, said:

You will have to invalidate and maybe even repaint after you have added a new component to your container, before it will not show

Try and resize your frame, and see if the new component shows. A resize will normally make the JPanel invalidate itself :)


Yes that's it! I didn't realise you had to repaint, for some reason I thought it did it automatically. Just to clarify, every time you add something to the FsaPanel or make a change, you must call repaint() to make it display? is this all you need to call?
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1372
  • View blog
  • Posts: 3,022
  • Joined: 05-April 11

Re: How to add object to a JPanel from an outside class

Posted 02 November 2012 - 04:51 PM

View Posttanny_, on 02 November 2012 - 11:42 PM, said:

View PostCasiOo, on 02 November 2012 - 04:25 PM, said:

You will have to invalidate and maybe even repaint after you have added a new component to your container, before it will not show

Try and resize your frame, and see if the new component shows. A resize will normally make the JPanel invalidate itself :)


Yes that's it! I didn't realise you had to repaint, for some reason I thought it did it automatically. Just to clarify, every time you add something to the FsaPanel or make a change, you must call repaint() to make it display? is this all you need to call?


It depends on the situation
invalidate() and validate() on the container are sometimes necessary
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to add object to a JPanel from an outside class

Posted 02 November 2012 - 08:26 PM

Never a good idea to add component to a container on the fly that can screw up a few layout quite fast
Add all components to the container at construction time. Set their visibility to false if required and toggle them to true later on if required
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1