Hello,

I am trying to display text(JLabel) above a 3 x3 tic tac toe grid. The JLabel is not displayed. Only the grid shows up. I would be grateful for any help.

Thank you!

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[1] = new JPanel(); 
        panel[1].setLayout (new FlowLayout());

        label[1] = new JLabel ("----------------- | TIC TAK TOE | -------------------"); 
        panel[1].add (label[1]); 


        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[1]);  
    } 
} 

Recommended Answers

All 11 Replies

Did you setLayout the box layout for your applet?

Hello,

I missed that. When I added it, I get the run-time error that "BoxLayout cannot be shared".

If possible, please let me know.

Thank you!

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
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
1. Why re-post all that code?
2. Are you saying that you can't add two JPanels to a JApplet?

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.

Do you have any authoratative links (eg from Oracle) for that, as opposed to just adding two JPanels to the content pane?

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

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

}

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

}

Hello James,

No worries. Your second message solved my problem completely! Thanks a lot!

Regards

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.