Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java
I want to display 2 objects on JFrame.
 
I tried by adding objects to JPanel and then add JPanel to JFrame but it also did not work.
 
I also tried to add ball and ball1 objects directly to JFrame but it shows the last added object only. I want to show both objects on JFrame at a time.
 
The below given code only shows ball1 object.
JFrame f = new JFrame("Moving"); 
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//making 2 objects 
Ballbewegung2 ball = new Ballbewegung2();
Ballbewegung3 ball1 = new Ballbewegung3(); 
JPanel contentPane = new JPanel(new BorderLayout());
JPanel contentPane1 = new JPanel(new BorderLayout());                                  
//adding objects to JPanel
contentPane.add(ball, BorderLayout.CENTER);                 
contentPane1.add(ball1, BorderLayout.CENTER);                 
//Adding JPanel to JFrmae
f.getContentPane().add(contentPane);
f.getContentPane().add(contentPane1);
f.setSize(500, 500);
f.setVisible(true);
Posted 3-Oct-12 6:08am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You're adding both Objects to the same area in the Borderlayout. That's why the first Object gets overwritten.
 
Try this:
JFrame f = new JFrame("Moving"); 
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//making 2 objects 
Ballbewegung2 ball = new Ballbewegung2();
Ballbewegung3 ball1 = new Ballbewegung3(); 
// JPanel contentPane = new JPanel(new BorderLayout());
// JPanel contentPane1 = new JPanel(new BorderLayout());                                  
//adding objects to JPanel
//contentPane.add(ball, BorderLayout.CENTER);       
contentPane.add(ball, BorderLayout.NORTH);                 
contentPane1.add(ball1, BorderLayout.CENTER);                 
//Adding JPanel to JFrmae
f.getContentPane().add(contentPane);
f.getContentPane().add(contentPane1);
f.setSize(500, 500);
f.setVisible(true);
It would be nice to have Ballbewegung as a direct extension to Jpanel - that would solve 2 problems at once.
 
Additional:
This is a simple demonstration to show the function of the Borderlayout.
Please note that every JPanel is added to another region - except the "another South" Panel, which overwrites the original "South" Panel.
public class MultiplePanels extends JFrame {
	
	public static void main(String[] args) {
		MultiplePanels oFrame = new MultiplePanels();
		oFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		oFrame.setSize(600, 300);
		oFrame.setVisible(true);
		
		oFrame.add(new MyPanel("North"),  BorderLayout.NORTH); 
		oFrame.add(new MyPanel("Center"), BorderLayout.CENTER);
		oFrame.add(new MyPanel("West"),   BorderLayout.WEST);
		oFrame.add(new MyPanel("East"),   BorderLayout.EAST);
		oFrame.add(new MyPanel("South"),  BorderLayout.SOUTH);
		// this one will overwrite the original south Panel:
		oFrame.add(new MyPanel("another South"),  BorderLayout.SOUTH); 
	}
}
public class MyPanel extends JPanel {
	
	public MyPanel(String strName) { // I'm passing an argument to the JPanel
		this.setLayout(new BorderLayout()); // The panel got a layout too
		// Components can be declared and initialized on the fly - as long as they do not need to be changed later.
		this.add(createLabel(strName), BorderLayout.CENTER);
	}
	private Component createLabel(String strName) {
		JLabel lblPanelnumber = new JLabel("This is the Panel " + strName); 
		lblPanelnumber.setHorizontalAlignment(SwingConstants.CENTER);
		return lblPanelnumber;
	}
}
 
Second Additional:
 
public class MultiplePanels extends JFrame {
	
	public MultiplePanels() {
		
	}
	
	public static void main(String[] args) {
		MultiplePanels oFrame = new MultiplePanels();
		oFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		oFrame.setSize(400, 400);
		oFrame.setBackground(Color.WHITE);
		oFrame.setVisible(true);
		oFrame.setLayout(new GridLayout(2, 1));
 
		oFrame.getContentPane().add(new Ballbewegung(10, 40)); 
		oFrame.getContentPane().add(new Ballbewegung(10, 80)); 
	}
}
 
class Ballbewegung extends JPanel implements Runnable {
	// Initialisierung der Variablen
	int x_pos = 10; // x - Position des Balles
	int y_pos = 100; // y - Position des Balles
	int radius = 20; // Radius des Balles
	
	public Ballbewegung(int x_pos, int y_pos){
		this.x_pos = x_pos;
		this.y_pos = y_pos;
		init();
	}
 
	public void init() {
//		setBackground(Color.blue);
		this.setOpaque(false);
		this.start();
	}
 
	public void start() {
		Thread th = new Thread(this);
		th.start();
	}
 
	@Override
	public void run() {
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
		while (true) {
			x_pos++;
			if (x_pos >= 400)
				x_pos = 10;
			repaint();
			try {
				Thread.sleep(200);
			} catch (InterruptedException ex) {
			}
			Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
		}
	}
 
	@Override
	public void paint(Graphics g) {
		g.setColor(Color.red);
		g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
	}
}
  Permalink  
v4
Comments
naeemashfaq at 3-Oct-12 15:10pm
   
Thanks for replying. How can I have direct extension to Jpanel? I have also tried with default layout but all in vain. I am also unable to view the code
TorstenH. at 3-Oct-12 15:19pm
   
are you American or German? cause those class names are German.
 
You can extend the c lass just by adding an "extends JPanel" to your object Ballbewegung declaration. Then you treat your Ballbewegung directly like a JPanel.
naeemashfaq at 3-Oct-12 15:57pm
   
I am from Sweden. I am new to Java programming. I am using the same Border Layout but the coordinates where I am displaying the objects are different. I tried this code but it also shows last object. My classes are extending JPanel.then how can I show both objects?
TorstenH. at 4-Oct-12 1:39am
   
I have added some code to show you what happens. You might want to try this one.
naeemashfaq at 4-Oct-12 6:56am
   
This is my class that creates a circle shape with specific x and y coordinates. Similarly I have another same class but with different x and y coordinate positions. I create 1 object of each class and want to display them on specific positions. I tried the code, it works well with your example but not working with my classes.
class Ballbewegung2 extends JPanel implements Runnable
{
// Initialisierung der Variablen
int x_pos = 10; // x - Position des Balles
int y_pos = 100; // y - Position des Balles
int radius = 20; // Radius des Balles
public void init()
{
setBackground (Color.blue);
}
public void start ()
{
Thread th = new Thread (this);
th.start ();
}
public void run ()
{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true)
{
x_pos ++;
if(x_pos >= 400)
x_pos = 10;
repaint();
try
{
Thread.sleep (20);
}
catch (InterruptedException ex)
{}
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void paint (Graphics g)
{
g.setColor (Color.red);
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}
}
TorstenH. at 4-Oct-12 7:44am
   
see 2. Additional for solution. I switched the layout, Borderlayout is relative.
Normally a good thing, here it's not working.
 
Where did you get that German code?
naeemashfaq at 4-Oct-12 13:47pm
   
I got it from the internet. I am really grateful to you for giving me your time.
I tried your code but it also overwrites the 1st object. When I tried the different layouts of JFrame, it shows both objects but the second object disappears after 2 or 3 seconds. I am copying the code.
 
public class DrawRect extends JFrame{
 
public static void main(String[] args)
{

JFrame f = new JFrame();
Ballbewegung ball = new Ballbewegung(10,100);
Ballbewegung ball1 = new Ballbewegung(15,300);
f.getContentPane().add(ball, BorderLayout.CENTER);
f.getContentPane().add(ball1, BorderLayout.WEST);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600, 500);
f.setVisible(true);
}
}
TorstenH. at 4-Oct-12 13:54pm
   
please use another Layout. The BorderLayout is not recommendable for this. E.g. GridLayout is fixed, that will work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 Zoltán Zörgő 99
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 4 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100