Hey, I'm almost done my long program that I've been working on all week, but I'm stuck on this tiny problem where I can't seem to add more than one shape component inside a frame at the same time.

final Rectangle recComp = new Rectangle();
final Ellipse ellipseComp = new Ellipse();
frame.add(recComp,BorderLayout.CENTER);
frame.add(ellipseComp,BorderLayout.CENTER);

The "frame.add()" that comes last is the only one that appears when I run the program, so how can I include them all?

Thanks.

u can't put 2 components in the centre using border layout. what u want to do is to create a panel with a flow or grid layout and put your 2 components in that then put the panel in the centre of your frame

^^ oh I already have that working, frame is JFrame. Everything is working in the program except when I include more than one component.

u can't put 2 components in the centre using border layout. what u want to do is to create a panel with a flow or grid layout and put your 2 components in that then put the panel in the centre of your frame

Sorry, I didn't get exactly what you meant, I tried this but it didn't work:

JPanel panel = new Jpanel();
FlowLayout Layout = new FlowLayout();
final Rectangle recComp = new Rectangle();
final Ellipse ellipseComp = new Ellipse();
recComp.setLayout(Layout);
ellipseComp.setLayout(Layout);
panel.add(Layout);

The default layouts for containers is a flow layout so you don't have to explicitly define one

Rectangle recComp = new Rectangle();
Ellipse ellipseComp = new Ellipse();
JPanel panel = new JPanel();
panel.add(recComp);
panel.add(ellipseComp);
frame.add(panel, BorderLayout.CENTER);

Its still not working :( one component works if I use frame.add(component), but none if I use panel.add(component)... Here's my full code:

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


public class GraphElementViewer 
{
	public static void main (String[] args)
	{
	      JFrame frame = new JFrame();
	      
	      final int FRAME_WIDTH  = 600;
	      final int FRAME_HEIGHT = 600;

	      frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
	      frame.setTitle("Graph Draw");
	      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	      frame.setLayout(new BorderLayout());
	      
	      // Create components and add them to the frame
	      final Rectangle recComp = new Rectangle();
	      final Ellipse ellipseComp = new Ellipse();
	      JPanel panel = new JPanel();

	      panel.add(recComp);
	      panel.add(ellipseComp);
	      frame.add(panel, BorderLayout.NORTH);
	           
	      
	      // Creates buttons
	      JButton RectangleButton = new JButton("Rectangle");
	      JButton EllipseButton = new JButton("Ellipse");
	      class GraphElementListener implements ActionListener
	      {
	         public void actionPerformed(ActionEvent event)
	         {
	            recComp.createRectangle();
	            ellipseComp.createEllipse();
	         }            
	      }
	      ActionListener listener = new GraphElementListener();
	      RectangleButton.addActionListener(listener);
	      EllipseButton.addActionListener(listener);
	   }

}

I'm guessing that you have created your own component classes Ellipse and Rectangle.
Lets forget about your code for now; what exactly is the problem u are trying to solve?

If you are trying to draw Rectangles and Ellipse then you don't need to create components use the java.awt.Graphics object's drawing methods to create them

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