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();
		        }
		      });
		    }

Recommended Answers

All 4 Replies

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.

Code tags?

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

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.

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.