Hello could someone please help me as my program is generating some weird error" java.lang.IllegalArgumentException: adding a window to a container"
Could someone please look at it and tell me what I'm doing wrong. Im trying to add an object of fan to the panel it creates that error. whenever I remove the add(fan, BorderLayout.CENTER); the error goes away. Please someone help me come up with a
solution:(
I have attached my programs

import javax.swing.JPanel;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
	public class FanControl extends JPanel {
		  private RunFan fan = new RunFan();
		  private JButton jbtStart = new JButton("Start");
		  private JButton jbtStop = new JButton("Stop");
		  private JButton jbtReverse = new JButton("Reverse");
		  private JScrollBar jsbDelay = new JScrollBar();

		  public FanControl() {
		    // Group buttons in a panel
		    JPanel panel = new JPanel();
		    panel.add(jbtStart);
		    panel.add(jbtStop);
		    panel.add(jbtReverse);
		    panel.add(jsbDelay);
		    
		    
		    jsbDelay.setOrientation(JScrollBar.HORIZONTAL);
		    setLayout(new BorderLayout());
		    add(panel,BorderLayout.NORTH);
		    add(jsbDelay, BorderLayout.SOUTH);
		    add(fan, BorderLayout.CENTER);
		    jbtStart.addActionListener(new ActionListener() {
		        public void actionPerformed(ActionEvent e) {
		        	fan.start();
		        }
		      });
		      jbtStop.addActionListener(new ActionListener() {
		        public void actionPerformed(ActionEvent e) {
		          fan.stop();
		        }
		      });
		      jbtReverse.addActionListener(new ActionListener() {
		        public void actionPerformed(ActionEvent e) {
		          fan.reverse();
		        }
		      });
		    }

Edited 6 Years Ago by peter_budo: Keep it Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

Attachments
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
	public class FanControl extends JPanel {
		  private RunFan fan = new RunFan();
		  private JButton jbtStart = new JButton("Start");
		  private JButton jbtStop = new JButton("Stop");
		  private JButton jbtReverse = new JButton("Reverse");
		  private JScrollBar jsbDelay = new JScrollBar();

		  public FanControl() {
		    // Group buttons in a panel
		    JPanel panel = new JPanel();
		    panel.add(jbtStart);
		    panel.add(jbtStop);
		    panel.add(jbtReverse);
		    panel.add(jsbDelay);
		    
		    
		    jsbDelay.setOrientation(JScrollBar.HORIZONTAL);
		    setLayout(new BorderLayout());
		    add(panel,BorderLayout.NORTH);
		    add(jsbDelay, BorderLayout.SOUTH);
		    add(fan, BorderLayout.CENTER);
		    jbtStart.addActionListener(new ActionListener() {
		        public void actionPerformed(ActionEvent e) {
		        	fan.start();
		        }
		      });
		      jbtStop.addActionListener(new ActionListener() {
		        public void actionPerformed(ActionEvent e) {
		          fan.stop();
		        }
		      });
		      jbtReverse.addActionListener(new ActionListener() {
		        public void actionPerformed(ActionEvent e) {
		          fan.reverse();
		        }
		      });
		    }

		
			
		


	}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RunFan extends JFrame {
	Timer RunningFanTimer;
  public RunFan() {
	  
    setTitle("RunningFan");
    add(new ArcsPanel()); 
   
  }
  public void stop() {
	  RunningFanTimer.start(); // Suspend timer
	  }

	  public void start() {
		  RunningFanTimer.start();	 // Resume timer
	  }

	  public void reverse() {
		  RunningFanTimer.start();	 // Resume timer
	  }
 

}


class ArcsPanel extends JPanel {
	public static int increment=0;
	
	
	//Creates and sets the value for the timer.
	public ArcsPanel(){
	  Timer RunningFanTimer = new Timer(50, new TimerListener());
	}
	
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    int xCenter = getWidth() / 2;
    int yCenter = getHeight() / 2;
    int radius = (int)(Math.min(getWidth(), getHeight()) * 0.4);

    int x = xCenter - radius;
    int y = yCenter - radius;
    
 
    increment = increment + 10;
   
    //The start of the arc is initially set to zero.
    
    g.fillArc(x, y, 2 * radius, 2 * radius, 0+increment, 30);
    g.fillArc(x, y, 2 * radius, 2 * radius, 90+increment, 30);
    g.fillArc(x, y, 2 * radius, 2 * radius, 180 + increment, 30);
    g.fillArc(x, y, 2 * radius, 2 * radius, 270 + increment, 30);
    
    /*
    g.fillArc(x, y, 2 * radius, 2 * radius, 0- decrement, 30);
    g.fillArc(x, y, 2 * radius, 2 * radius, 90- decrement, 30);
    g.fillArc(x, y, 2 * radius, 2 * radius, 180 - decrement, 30);
    g.fillArc(x, y, 2 * radius, 2 * radius, 270 - decrement, 30); 
    */
  }

  

  //The listener class.
  class TimerListener implements ActionListener{
	  public void actionPerformed(ActionEvent e){
		  repaint();
		  }
	  }
  }
import java.awt.*;

import javax.swing.*;


public class RunningFan extends JApplet {
	public RunningFan() {
	    add(new FanControl());
	    
	  }

	  public static void main(String[] args) {
		  FanControl applet = new FanControl();
	    JFrame frame = new JFrame();
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frame.setTitle("Exercise 17.11");
	    frame.getContentPane().add(applet, BorderLayout.CENTER);
	    frame.setSize(500, 150);
	    frame.setVisible(true);
	  }
	}

Your problem is in RunFan. You make it a JFrame, which you cannot add. Instead make it a JComponent and you should be good.

Please remember code tags when posting code.

My fan shrunk on my panel, can anybody offer any help as I can get it bigger?

Attachments
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
	public class FanControl extends JPanel {
		  private RunFan fan = new RunFan();
		  private JButton jbtStart = new JButton("Start");
		  private JButton jbtStop = new JButton("Stop");
		  private JButton jbtReverse = new JButton("Reverse");
		  private JScrollBar jsbDelay = new JScrollBar();

		  public FanControl() {
		    // Group buttons in a panel
		    JPanel panel = new JPanel();
		    panel.add(jbtStart);
		    panel.add(jbtStop);
		    panel.add(jbtReverse);
		    panel.add(jsbDelay);
		    
		    
		    jsbDelay.setOrientation(JScrollBar.HORIZONTAL);
		    setLayout(new BorderLayout());
		    add(panel,BorderLayout.NORTH);
		    add(jsbDelay, BorderLayout.SOUTH);
		    add(fan, BorderLayout.CENTER);
		    jbtStart.addActionListener(new ActionListener() {
		        public void actionPerformed(ActionEvent e) {
		        	fan.start();
		        }
		      });
		      jbtStop.addActionListener(new ActionListener() {
		        public void actionPerformed(ActionEvent e) {
		          fan.stop();
		        }
		      });
		      jbtReverse.addActionListener(new ActionListener() {
		        public void actionPerformed(ActionEvent e) {
		          fan.reverse();
		        }
		      });
		    }

		
			
		


	}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RunFan extends JPanel {
	Timer RunningFanTimer;
  public RunFan() {
	  
   // setTitle("RunningFan");
    add(new ArcsPanel()); 
   
  }
  public void stop() {
	  RunningFanTimer.start(); // Suspend timer
	  }

	  public void start() {
		  RunningFanTimer.start();	 // Resume timer
	  }

	  public void reverse() {
		  RunningFanTimer.start();	 // Resume timer
	  }
 

}


class ArcsPanel extends JPanel {
	public static int increment=0;
	
	
	//Creates and sets the value for the timer.
	public ArcsPanel(){
	  Timer RunningFanTimer = new Timer(50, new TimerListener());
	}
	
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    int xCenter = getWidth() / 10;
    int yCenter = getHeight() / 10;
    int radius =5;// (int)(Math.min(getWidth(), getHeight()) * 0.4);
    System.out.println(WIDTH+HEIGHT);
    int x = xCenter - radius;
    int y = yCenter - radius;
    
 
    increment = increment + 10;
   
    //The start of the arc is initially set to zero.
    
    g.fillArc(x, y, 2 * radius, 2 * radius, 0+increment, 30);
    g.fillArc(x, y, 2 * radius, 2 * radius, 90+increment, 30);
    g.fillArc(x, y, 2 * radius, 2 * radius, 180 + increment, 30);
    g.fillArc(x, y, 2 * radius, 2 * radius, 270 + increment, 30);
    
    /*
    g.fillArc(x, y, 2 * radius, 2 * radius, 0- decrement, 30);
    g.fillArc(x, y, 2 * radius, 2 * radius, 90- decrement, 30);
    g.fillArc(x, y, 2 * radius, 2 * radius, 180 - decrement, 30);
    g.fillArc(x, y, 2 * radius, 2 * radius, 270 - decrement, 30); 
    */
  }

  

  //The listener class.
  class TimerListener implements ActionListener{
	  public void actionPerformed(ActionEvent e){
		  repaint();
		  }
	  }
  }
import java.awt.*;

import javax.swing.*;


public class RunningFan extends JApplet {
	public RunningFan() {
	    add(new FanControl());
	    
	  }

	  public static void main(String[] args) {
		  FanControl applet = new FanControl();
	    JFrame frame = new JFrame();
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frame.setTitle("Exercise 17.11");
	    frame.getContentPane().add(applet, BorderLayout.CENTER);
	    frame.setSize(500, 150);
	    frame.setVisible(true);
	  }
	}

I didn't check out the files you have attached, but my assumption is that you need to set the size of the fan.

Personally I would just give your panel a gridbaglayout and set the weight x and weight y to your preference.

This article has been dead for over six months. Start a new discussion instead.