Did you setLayout the box layout for your applet?
JamesCherrill
... trying to help
8,492 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 29
I don't use box layout myself, but I believe the message is a misleading one, and is caused by the layout being set for for a different component type than the "target" parameter (content pane vs JApplet) (the setLayout is forwarded to the JApplet's content pane). A quick Google for that error message will get you a full discussion, but I think one solution may look like this:
getContentPane().setLayout(new BoxLayout(getContentPane(), etc
JamesCherrill
... trying to help
8,492 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 29
package com.daniweb;
import java.awt.*;
import javax.swing.*;
import java.applet.Applet;
public class Game extends JApplet
{
LayoutManager layout;
private JPanel panel [] = new JPanel [5];
private GridLayout grid;
private JButton button [] = new JButton [10];
private JLabel label [] = new JLabel [5];
private JTextField field [] = new JTextField [10];
public void init()
{
setBackground(Color.GRAY);
layout = new BoxLayout (this,BoxLayout.Y_AXIS);
grid = new GridLayout (3, 3, 5, 5);
panel[2] = new JPanel();
panel[2].setLayout (grid);
for (int i = 1; i <= 9; ++i)
{
button[i] = new JButton();
panel[2].add (button[i]);
}
// add (panel[2]);
add (Box.createRigidArea(new Dimension (100, 100)));
add (panel[2]);
}
}
It is not the problem of setLayout.
The problem is of adding two JPanel.
One way if you want to add two JPanel is to use JFrame and add two JPanel to JFrame or you can use JSplitPane and add two JPanel to it.
IIM
Practically a Master Poster
636 posts since Jun 2011
Reputation Points: 127
Solved Threads: 136
Skill Endorsements: 7
@IIM
1. Why re-post all that code?
2. Are you saying that you can't add two JPanels to a JApplet?
JamesCherrill
... trying to help
8,492 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 29
We can add 2 JPanel but in that case you have to use a master JPanel and then add two JPanel to that.It is a proper way to do.
IIM
Practically a Master Poster
636 posts since Jun 2011
Reputation Points: 127
Solved Threads: 136
Skill Endorsements: 7
Do you have any authoratative links (eg from Oracle) for that, as opposed to just adding two JPanels to the content pane?
JamesCherrill
... trying to help
8,492 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 29
With the 1.4.2 release we provided two new look and feels for Swing: XP and GTK. Rather than taking a break, in 5.0 we're providing two more look and feels: Synth, a skinnable look and feel, and Ocean, a new theme for Metal. Beyond look and feels, we've added printing support to JTable, which makes it trivial to get a beautiful printed copy of a JTable. Lastly, after seven years, we've made jFrame.add equivalent to jFrame.getContentPane().add().
Please refer this
both are in some way similar thing
Read this blog
Hope you understand the reason
IIM
Practically a Master Poster
636 posts since Jun 2011
Reputation Points: 127
Solved Threads: 136
Skill Endorsements: 7
They seem to be references to the JFrame add method in Java 1.5 (just calls the content pane add method). JApplet does the same thing (at least in Java 1.7).
What has that got to do with adding two JPanels, as in (just run it):
import javax.swing.JApplet;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
public class AppletTest extends JApplet {
public AppletTest() {
JPanel panel1 = new JPanel();
add(panel1, BorderLayout.NORTH);
panel1.add(new JLabel("New label in panel 1"));
JPanel panel2 = new JPanel();
add(panel2, BorderLayout.SOUTH);
panel2.add(new JLabel("New label in panel 2"));
}
}
JamesCherrill
... trying to help
8,492 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 29
Hi javaprog200
Sorry about the digression there. Here's a simple small runnable test case that shows the solution working:
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JApplet;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JLabel;
public class AppletTest extends JApplet {
public AppletTest() {
// gives "BoxLayout cannot be shared" ...
// setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
// works...
getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
JPanel panel1 = new JPanel();
panel1.setBackground(Color.yellow);
add(panel1, BorderLayout.NORTH);
panel1.add(new JLabel("New label in panel 1"));
add(Box.createRigidArea(new Dimension(100, 100)));
JPanel panel2 = new JPanel();
panel2.setBackground(Color.cyan);
add(panel2, BorderLayout.SOUTH);
panel2.add(new JLabel("New label in panel 2"));
}
}
JamesCherrill
... trying to help
8,492 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 29
Question Answered as of 2 Months Ago by
JamesCherrill
and
IIM